XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Bullet Shotting Problems.

Last post 10/23/2008 9:56 PM by suldier. 4 replies.
  • 9/3/2008 6:02 AM

    Bullet Shotting Problems.

    Ok, so I’m trying to make a 2d shooter like raiden (google it) I barely have any coding experience and I’m new to xna.

    After watching the 2d tutorial I decided to put what I learnt into practice, thus I started my project.

    After a day or so of coding I came to a problem. I have my player moving around etc. But the bullets he shots go in the wrong direction. For example the player is facing east and the bullets go southeast. So I changed the bullets rotation but nothing changed, for a couple of days I’ve tried but nothing works, so here I am now.

    Another problem i have is, i can only shot one bullet at a time, if i try to shot 2 the first bullet disappears. Also there is a bullet always drawn in the upper left corner of the screen.

    Here is my game.cs

    using System;  
    using System.Collections.Generic;  
    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.Net;  
    using Microsoft.Xna.Framework.Storage;  
     
    namespace FighterPilot  
    {  
        /// <summary>  
        /// This is the main type for your game  
        /// </summary>  
        public class Game1 : Microsoft.Xna.Framework.Game  
        {  
            GraphicsDeviceManager graphics;  
              
            SpriteBatch spriteBatch;  
     
            Texture2D backgroundtexture;  
            Rectangle viewportRect;  
            GameObject Player;  
            const int maxbullet = 10;  
            GameObject[ Bullets;  
            KeyboardState previousKeyboardState = Keyboard.GetState();  
            const float playervelocity = 5.0f;  
            const float bulletvelocity = 15.0f;  
            const float bulletrotation = 4.7f;  
     
            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);  
     
                Player = new GameObject(Content.Load<Texture2D>("Sprites\\planef150005hf8r"));  
                Player.position = new Vector2(200, graphics.GraphicsDevice.Viewport.Height -300);  
                Bullets = new GameObject[maxbullet];  
                for (int i = 0; i < maxbullet; i++)  
                {  
                    Bullets[i] = new GameObject(Content.Load<Texture2D>("Sprites\\cannonball"));  
                }  
                backgroundtexture = Content.Load<Texture2D>("Sprites\\background1");  
                viewportRect = new Rectangle(0, 0,  
                    graphics.GraphicsDevice.Viewport.Width,  
                    graphics.GraphicsDevice.Viewport.Height);  
                  
     
                base.LoadContent();  
            }  
     
            /// <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  
                KeyboardState keyboardState = Keyboard.GetState();  
                if(keyboardState.IsKeyDown(Keys.Escape))  
                {  
                    this.Exit();  
                }  
                if (keyboardState.IsKeyDown(Keys.Up))  
                {  
                    Player.position.Y -= playervelocity;  
                }  
                if (keyboardState.IsKeyDown(Keys.Down))  
                {  
                    Player.position.Y += playervelocity;  
                }  
                if (keyboardState.IsKeyDown(Keys.Left))  
                {  
                    Player.position.X -= playervelocity;  
                }  
                if (keyboardState.IsKeyDown(Keys.Right))  
                {  
                    Player.position.X += playervelocity;  
                }  
                if (keyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))  
                {  
                    firebullet();  
                }  
                  
                updatebullets();  
     
                previousKeyboardState = keyboardState;  
     
                base.Update(gameTime);  
            }  
     
            public void firebullet()  
            {  
                foreach (GameObject bullet in Bullets)  
                {  
                    if (bullet.alive)  
                    {  
                        bullet.alive = true;  
                        bullet.position = Player.position;  
                        bullet.rotation = 4.7f;  
                        bullet.velocity = new Vector2(bulletvelocity);  
                        return;  
                    }  
                }  
            }  
     
            public void updatebullets()  
            {  
                foreach (GameObject bullet in Bullets)  
                {  
                    if (bullet.alive)  
                    {  
                        bullet.position += bullet.velocity;  
                        if (!viewportRect.Contains(new Point(  
                            (int)bullet.position.X,  
                            (int)bullet.position.Y)))  
                        {  
                            bullet.alive = false;  
                            continue;  
                        }  
                    }  
                }  
            }  
     
            /// <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);  
                spriteBatch.Draw(backgroundtexture, viewportRect, Color.White);  
     
                foreach (GameObject bullet in Bullets)  
                {  
                    if (bullet.alive = true)  
                    {  
                        spriteBatch.Draw(bullet.sprite,  
                            bullet.position,  
                            Color.White);  
                    }  
                }  
     
                spriteBatch.Draw(Player.sprite,  
                    Player.position,  
                    null,  
                    Color.White,  
                    Player.rotation,  
                    Player.center, 1.0f,  
                    SpriteEffects.None, 0);  
     
     
                spriteBatch.End();  
     
                base.Draw(gameTime);  
            }  
        }  
    }  
     

     

    And here is my gameobject.cs

     

    using System;  
    using System.Collections.Generic;  
    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.Net;  
    using Microsoft.Xna.Framework.Storage;  
     
    namespace FighterPilot  
    {  
        class GameObject  
        {  
     
            public Texture2D sprite;  
            public Vector2 position;  
            public Vector2 center;  
            public float rotation;  
            public bool alive;  
            public Vector2 velocity;  
     
            public GameObject(Texture2D loadedTexture)  
            {  
                  
                position = Vector2.Zero;  
                sprite = loadedTexture;  
                center = new Vector2(sprite.Width / 2, sprite.Height / 2);  
                alive = false;  
                velocity = Vector2.Zero;  
            }  
     
        }  
    }  
     

     

    Any idea's?

     

    Also i get a warning for my bullet draw code, asking me if i mean bullet.alive == true instead of bullet.alive = true. When i try to use == the game doesnt even let me shot bullets, but the bullet on the upper left side of my screen is gone.

    I think i made a bo-boo somewhere in my code.

  • 9/3/2008 6:21 AM In reply to

    Re: Bullet Shotting Problems.

    In firebullet() you check "if (bullet.alive)"  but this should probably be if (!bullet.alive) so that you are only firing bullets that aren't already being used. That explains why you only ever see one bullet.

    In your Draw() method, you call "if (bullet.alive = true)". Here you should be using two equals to check if bullet == true. In you code, you are setting every bullet to alive as you draw it! That would be why you see a bullet in the top left.

    As for the bullet direction, in firebullet(), you do "bullet.velocity = new Vector2(bulletvelocity)", which will always be the same thing... hence bullets always go in the same direction. You should pass two values to the Vector2 constructor. The first value is X (or how fast the bullet will be going east) and the second is Y (or how fast the bullet will be going south).

  • 9/3/2008 8:41 AM In reply to

    Re: Bullet Shotting Problems.

    Thanks soo much. Everythings working fine.

    Now to move on to the next Feature.

  • 9/3/2008 9:15 AM In reply to

    Re: Bullet Shotting Problems.

    No problems... glad to help.

    Just wanted to clarify for you that warning you were getting about the single vs double equals. A single equals is the assignment operator, so it means set the variable to the given value. The double equals is the equality operator, it returns true if the value on the left and right of it are equal. The compiler knows that "if (a = b)" is a common typing error so it gives you the warning you saw. Whilst technically it is a valid thing to do when your variable is a bool (the if will be checking the resulting value of your variable), it is bad practice because it is confusing. Anyway, good luck.


  • 10/23/2008 9:56 PM In reply to

    Re: Bullet Shotting Problems.

    I am fairly new to creating games with XNA as well, but it seems that it would be easier to create a bullet class. Each bullet instance would could keep track of it's own position and you could give it a direction to travel in. This would make it easier to create upgrades to weapons and such as well in the future.
Page 1 of 1 (5 items) Previous Next