Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question about errors in XNA with M. Visual Studio

    So I need to create sort of a game with XNA, inside Microsoft Visual Studio.
    We have access to the entire code, so it's almost just a copy and paste assignment.
    I'm stuck at a point, where I get 4 errors:

    1. "}" expected (Line 222)
    2. Expected Class, delegate, enum, interface, or struct (Line 253)
    3. Expected Class, delegate, enum, interface, or struct (Line 293)
    4. Type or namespace definition, or end-of-file expected. (Line 310)

    It's supposed to be a small game where you control a cannon and you have to shoot alien ships that pass by horizontally. I am stuck at the part where I'm trying to get an explosion everytime I hit an alien ship.

    Now, this is a pretty large code, so enjoy.

    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;
    
    namespace Opdracht1
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
            Texture2D backgroundTexture;
    
            Texture2D spritesheet;
            float timer = 0f;
            float interval = 1000f / 25f;
            int frameCount = 16;
            int currentFrame = 0;
            int spriteWidth = 64;
            int spriteHeight = 64;
    
            Rectangle sourceRect;
            Rectangle destinationRect;
            Rectangle viewportRect;
    
            GameObject cannon;
    
            const int maxCannonBalls = 3;
            GameObject[] cannonBalls;
            KeyboardState previousKeyboardState = Keyboard.GetState();
    
            const int maxEnemies = 3;
            GameObject[] enemies;
            const float maxEnemyHeight = 0.1f;
            const float minEnemyHeight = 0.5f;
            const float maxEnemyVelocity = 5.0f;
            const float minEnemyVelocity = 1.0f;
            Random random = new Random();
            int score;
            SpriteFont font;
            Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);
    
            class Explosion
            {
                public Texture2D sprite;
                public Vector2 position;
                public float rotation;
                public Vector2 center;
                public Vector2 velocity;
                public bool alive;
                public int currentFrame = 0;
                public Rectangle sourceRect;
                public float timer = 0f;
                public Explosion(Texture2D loadedTexture)
                {
                    rotation = 0.0f;
                    position = Vector2.Zero;
                    sprite = loadedTexture;
                    center = new Vector2(sprite.Width / 2,
                    sprite.Height / 2);
                    velocity = Vector2.Zero;
                    alive = false;
    
                    float interval = 1000f / 25f;
                    int frameCount = 16;
                    int spriteWidth = 64;
                    int spriteHeight = 64;
                }
            }
    
            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(GraphicsDevice);
    
                // TODO: use this.Content to load your game content here
    
                backgroundTexture = Content.Load<Texture2D>("Sprites\\background");
                spriteSheet = Content.Load<Texture2D>("sprite_sheet");
                destinationRect = new Rectangle(0, 0, spriteWidth, spriteHeight);
                viewportRect = new Rectangle(0, 0,
                graphics.GraphicsDevice.Viewport.Width,
                graphics.GraphicsDevice.Viewport.Height);
    
                enemies = new GameObject[maxEnemies];
                explosions = new Explosion[maxEnemies];
                for (int i = 0; i < maxEnemies; i++)
                {
                    enemies[i] = new GameObject(
                            Content.Load<Texture2D>("Sprites\\enemy"));
                    explosions[i] = new Explosion(
                            Content.Load<Texture2D>(
                            "Sprites\\sprite_sheet"));
                }
    
                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"));
                }
    
                enemies = new GameObject[maxEnemies];
                for (int i = 0; i < maxEnemies; i++)
                {
                    enemies[i] = new GameObject(
                    Content.Load<Texture2D>("Sprites\\enemy"));
                }
                font = Content.Load<SpriteFont>("Fonts\\GameFont");
    
                enemies = new GameObject[maxEnemies];
                explosions = new Explosion[maxEnemies];
                for (int i = 0; i < maxEnemies; i++)
                {
                    enemies[i] = new GameObject(
                    Content.Load<Texture2D>("Sprites\\enemy"));
                    explosions[i] = new Explosion(
                    Content.Load<Texture2D>(
                    "Sprites\\sprite_sheet"));
                }
    
                
    
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <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();
    
                // TODO: Add your update logic here
    
                KeyboardState keyboardState = Keyboard.GetState();
                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    cannon.rotation -= 0.1f;
                }
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    cannon.rotation += 0.1f;
                }
    
                cannon.rotation = MathHelper.Clamp(cannon.rotation, -MathHelper.PiOver2, 0);
    
                base.Update(gameTime);
    
                if (keyboardState.IsKeyDown(Keys.Space) &&
                previousKeyboardState.IsKeyUp(Keys.Space))
                {
                    FireCannonBall();
                }
                previousKeyboardState = keyboardState;
                UpdateCannonBalls();
                    
                UpdateEnemies();
    
                timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                if (timer > interval)
                {
                currentFrame++;
                if (currentFrame > frameCount - 1)
            {
                 currentFrame = 0;
            }
                 timer = 0f;
    }
    
            
    sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
    
                UpdateExplosions((float)gameTime.ElapsedGameTime.TotalMilliseconds);
                
                public void UpdateExplosions(float timertijd)
                {
                foreach (Explosion explosion in explosions)
                {
                if (explosion.alive)
                {
                explosion.timer += timertijd;
                if (explosion.timer > interval)
                {
                explosion.currentFrame++;
                if (explosion.currentFrame > frameCount - 1)
                {
                explosion.alive = false;
                }
                explosion.timer = 0f;
                }
                explosion.sourceRect = new
                Rectangle(explosion.currentFrame * spriteWidth, 
                0, spriteWidth, spriteHeight);
                }
                }
                }
        }
            }
    
            /// <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)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                // TODO: Add your drawing code here
                spriteBatch.Begin();
                spriteBatch.Draw(backgroundTexture, viewportRect,
                Color.White);
                spriteBatch.Draw(spritesheet, destinationRect, sourceRect, 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);
    
                foreach (GameObject enemy in enemies)
                {
                    if (enemy.alive)
                    {
                        spriteBatch.Draw(enemy.sprite,
                        enemy.position, Color.White);
                    }
                }
                spriteBatch.DrawString(font,
                "Score: " + score.ToString(),
                new Vector2(scoreDrawPoint.X * viewportRect.Width,
                scoreDrawPoint.Y * viewportRect.Height),
                Color.Yellow);
                spriteBatch.End();
    
                base.Draw(gameTime);
    
        foreach (Explosion explosion in explosions)
                {
                     if (explosion.alive)
                {
                        spriteBatch.Draw(explosion.sprite, 
                        explosion.position, explosion.sourceRect, 
                        Color.White, 0f, Vector2.Zero, 1.5f, 
                        SpriteEffects.None, 0);
                }
    } 
            }
    
    
            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;
                    }
                }
            }
    
            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;
                  }
        Rectangle cannonBallRect = new Rectangle(
    (int)ball.position.X,
    (int)ball.position.Y,
    ball.sprite.Width,
    ball.sprite.Height);
        foreach (GameObject enemy in enemies)
        {
            Rectangle enemyRect = new Rectangle(
            (int)enemy.position.X,
            (int)enemy.position.Y,
            enemy.sprite.Width,
            enemy.sprite.Height);
            if (cannonBallRect.Intersects(enemyRect))
            {
                ball.alive = false;
                enemy.alive = false;
                score += 1;
                break;
    
                explosions[enemynummer].alive = true;
                explosions[enemynummer].timer = 0;
                explosions[enemynummer].currentFrame = 0;
                explosions[enemynummer].position = enemy.position;
            }
        }
             }
        }
    }
    
            public void UpdateEnemies()
            {
                foreach (GameObject enemy in enemies)
                {
                    if (enemy.alive)
                    {
                        enemy.position += enemy.velocity;
                        if (!viewportRect.Contains(new Point(
                        (int)enemy.position.X,
                        (int)enemy.position.Y)))
                        {
                            enemy.alive = false;
                        }
                    }
                    else
                    {
                        enemy.alive = true;
                        enemy.position = new Vector2(
                        viewportRect.Right,
                        MathHelper.Lerp(
                        (float)viewportRect.Height * minEnemyHeight,
                        (float)viewportRect.Height * maxEnemyHeight,
                        (float)random.NextDouble()));
                        enemy.velocity = new Vector2(
                        MathHelper.Lerp(
                        -minEnemyVelocity,
                        -maxEnemyVelocity,
                        (float)random.NextDouble()), 0);
                    }
                }
            }
        }
    }
    Last edited by oracleguy; 02-10-2014 at 04:42 PM. Reason: Please use code tags when posting.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    The problem is with your closing braces. You have some extra ones after the Update Explosions function and you are missing one before that function.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •