2 Replies - 2001 Views - Last Post: 27 January 2010 - 06:34 AM

#1 bgile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-September 09

I am working on the tutorial on XNA creators club website

Post icon  Posted 26 January 2010 - 03:47 PM

I followed the videos and I got to where the cannon is suppose to fire and I get errors. Perhaps someone could help me out here. Here is my code.

 using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_2D_chapter_2
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;

		Texture2D backgroundTexture;
		Rectangle viewportRect;
		SpriteBatch spriteBatch;
		GameObject cannon;
		const int maxCannonBalls = 3;
		GameObject[] cannonBalls;
		GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
		KeyboardState previousKeyboardState = Keyboard.GetState();
		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
		}

		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize()
		{
			// TODO: Add your initialization logic here

			base.Initialize();
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		protected override void LoadContent()
		{
			// Create a new SpriteBatch, which can be used to draw textures.
			spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

			backgroundTexture = Content.Load<Texture2D>(
				"Sprites\\background");

			cannon = new GameObject(Content.Load<Texture2D>("Sprites\\cannon"));
			cannon.position = new Vector2(120, graphics.GraphicsDevice.Viewport.Height - 80);
			cannonBalls = new GameObject[maxCannonBalls];
			for (int i = 0; i < maxCannonBalls; i++)
			{ 
			   cannonBalls[i] = new GameObject(Content.Load<Texture2D>(
				   "Sprites\\cannonball"));
			
			}
				viewportRect = new Rectangle(0, 0,
					graphics.GraphicsDevice.Viewport.Width,
					graphics.GraphicsDevice.Viewport.Height);

			base.LoadContent();
		}
		public void UpdateCannonBalls()
		{
			foreach (GameObject ball in cannonBalls)
			{
				if (ball.alive)
				{
					ball.position += ball.velocity;
					if (!viewportRect.Contains(new Point(
						(int)ball.position.X,
						(int)ball.position.Y)))
					{
						ball.alive = false;
						continue;
					}
				}
			}
		}

	   
		protected override void UnloadContent()
		{
			base.UnloadContent();
		
		}

		/// <summary>
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update(GameTime gameTime)
		{
			// Allows the game to exit
			if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
			cannon.rotation += gamePadState.ThumbSticks.Left.X * 0.1f;
			if (gamePadState.Buttons.A == ButtonState.Pressed &&
				previousGamePadState.Buttons.A == ButtonState.Released)
			{
				FireCannonBall();
			}
#if !XBOX
			KeyboardState keyboardState = Keyboard.GetState();
			if(keyboardState.IsKeyDown(Keys.Left))
			{
			   cannon.rotation -= 0.1f;
			}
			if(keyboardState.IsKeyDown(Keys.Right))
			{
			 cannon.rotation += 0.1f;
			}
			if (keyboardState.IsKeyDown(Keys.Space) &&
				previousKeyboardState.IsKeyUp(Keys.Space))
			{
				FireCannonBall();
			}
#endif
			cannon.rotation = MathHelper.Clamp(cannon.rotation, -MathHelper.PiOver2, 0);
			UpdateCannonBalls();
			// TODO: Add your update logic here
			previousGamePadState = gamePadState;


#if !XBOX 
			 previousKeyboardState = keyboardState;
#endif 
			base.Update(gameTime);
		}

		public void FireCannonBall()
		{ 
		   foreach(GameObject ball in cannonBalls)
		   {
			   if(!ball.alive)
			   {
				   ball.alive = true;
				   ball.position = cannon.position - ball.center;
				   ball.velocity = new Vector2(
					   (float)Math.Cos(cannon.rotation),
					   (float)Math.Sin(cannon.rotation)
					   * 5.0f);
				   return;
			   }
		   }
		}


		}

		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Draw(GameTime gameTime)
		{
			graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

			spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

			//Draw the backgroundTexture sized to the width
			//and height of the screen.
			spriteBatch.Draw(backgroundTexture, viewportRect,
				Color.White);
			foreach (GameObject ball in cannonBalls)
			{
				if (ball.alive)
				{
					spriteBatch.Draw(ball.sprite,
						ball.position, Color.White);
				}
			}
			spriteBatch.Draw(cannon.sprite,
				cannon.position,
				null,
				Color.White,
				cannon.rotation,
				cannon.center, 1.0f,
				SpriteEffects.None, 0);
		   
			spriteBatch.End();

			base.Draw(gameTime);
		}
	}
 

		public void UpdateCannonBalls()
		{
		   foreach (GameObject ball in cannonBalls)
			{
				if (ball.alive)
				{
					ball.position += ball.velocity;
					if (!viewportRect.Contains(new Point(
						(int)ball.position.X,
						(int)ball.position.Y)))
					{
						ball.alive = false;
						continue;
					}
				}
		 }
		





these are the errors I am getting. I have never worked with C# before but I have worked with C++.

I have 3 expect class, delegate, enum, interface,or struct errors. these errors are at lines 172, 205, 212
and 1 Type or namespace definition, or end of file expected. this one is at line 219.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: I am working on the tutorial on XNA creators club website

#2 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: I am working on the tutorial on XNA creators club website

Posted 26 January 2010 - 03:50 PM

One quick scan shows that UpdateCannonBalls() is outside of the bracket for the namespace. Also a number of } brackets are missing also. I suggest you tidy those up.

This post has been edited by ShadowsEdge19: 26 January 2010 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: I am working on the tutorial on XNA creators club website

Posted 27 January 2010 - 06:34 AM

View PostShadowsEdge19, on 26 Jan, 2010 - 04:50 PM, said:

One quick scan shows that UpdateCannonBalls() is outside of the bracket for the namespace. Also a number of } brackets are missing also. I suggest you tidy those up.


This is actually the second definition of UpdateCannonBalls: it shouldn't even exist.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1