0

I have a sprite that moves on the bottom of the screen from left to right if you press down left and right key.

I want to be able to shoot something (any sprite I want) from the sprite that is moving on the bottom of the screen and have that sprite just go directly up.

How could I do this?

Claud
  • 1,065
  • 3
  • 26
  • 38
  • When the shoot button is pressed you should create a new bullet object at the coordinates of the player and give it a velocity in whatever direction you desire. – RJ Hill Apr 16 '12 at 05:25
  • Could you explain this more or is there a tutorial on that? I just am starting to learn XNA and that's why Im clueless. I've googled it and examples usually show current projects they've already worked on or something with different classes. I just want to load a sprite and when I press the spacebar just make that sprite appear at the location of my moving sprite and then just have the sprite move up until it hits something or gets off the screen. – Claud Apr 16 '12 at 05:32
  • Okay so, first you have to catch that the user pressed the space bar. Have you gotten that far? – RJ Hill Apr 16 '12 at 05:32
  • if(keyboardState.IsKeyDown(Keys.Space)) then just shoot whatever I want – Claud Apr 16 '12 at 05:35
  • Okay, cool, so now you will want a Shoot() method. Inside that 'if' statement you call Shoot(). – RJ Hill Apr 16 '12 at 05:37
  • Inside the Shoot() method, you're going to want to create a new Bullet. Do you have a Bullet class? – RJ Hill Apr 16 '12 at 05:39
  • no I don't. Why do I need a bullet class? – Claud Apr 16 '12 at 05:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10109/discussion-between-claud25-and-rj-hill) – Claud Apr 16 '12 at 05:40

1 Answers1

1

Don't copy and paste this exactly but it's going to look something like this when you split those classes:

namespace SpaceInvadersGame 
{ 
    class Player : Microsoft.Xna.Framework.Game 
    { 
        Texture2D PlayerTexture; 
        Vector2 PlayerPosition; 

        public Player() 
        { 

        } 

        protected override void LoadContent() 
        { 
            PlayerTexture = Content.Load<Texture2D>(@"Images/freshman2");; 
            PlayerPosition = Vector2.Zero; 
            base.LoadContent(); 
        }

        public Vector2 GetPosition()
        {
            return this.PlayerPosition;
        }

        public void Update() 
        { 
            KeyboardState keyboardState = Keyboard.GetState(); 
            if (keyboardState.IsKeyDown(Keys.Left)) 
                freshamPos.X -= freshmanSpeed; 
            if (keyboardState.IsKeyDown(Keys.Right)) 
                freshamPos.X += freshmanSpeed;
            if(keyboardState.IsKeyDown(Keys.Space)) 
                theBullet = new Bullet(this);
        } 

        public void Draw(SpriteBatch SpriteBatch) 
        { 

        } 
    } 
}

namespace SpaceInvadersGame
{
    class Bullet : Microsoft.Xna.Framework.Game
    {
        Texture2D BulletTexture;
        Vector2 BulletPosition;
        Player thePlayer;

        public Bullet(Player player)
        {
            this.thePlayer = player;
        }

        protected override void LoadContent()
        {
            BulletTexture = Content.Load<Texture2D>(@"Images/bullet");;
            BulletPosition = thePlayer.GetPosition();
            base.LoadContent();
        }

        public void Update()
        {
            //in here is where you would just do something like:
            //BulletPosition.Y += 1;
        }

        public void Draw(SpriteBatch SpriteBatch)
        {

        }
    }
}
RJ Hill
  • 308
  • 1
  • 5