XNA: How to make the Vaus Spacecraft move left and right on directional keys pressed?

Posted by Will Marcouiller on Game Development See other posts from Game Development or by Will Marcouiller
Published on 2011-02-03T03:53:12Z Indexed on 2011/02/03 7:33 UTC
Read the original article Hit count: 288

Filed under:
|
|
|
|

I'm currently learning XNA per suggestion from this question's accepted answer:
Where to start writing games, any tutorials or the like?

I have then installed everything to get ready to work with XNA Game Studio 4.0.

General Objective

Writing an Arkanoid-like game. I want to make my ship move when I press either left or right keys.

Code Sample

    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
#if WINDOWS
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();
        else {
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                MoveLeft(gameTime);
        }
#endif

        // Move the sprite around.
        BounceEnergyBall(gameTime);

        base.Update(gameTime);
    }

    void MoveLeft(GameTime gameTime) {
        // I'm not sure how to play with the Vector2 object and its position here!...
        _vausSpacecraftPos /= _vausSpacecraftSpeed.X; // This line makes the spacecraft move diagnol-top-left.
    }

Question

What formula shall I use, or what algorithm shall I consider to make my spaceship move as expected left and right properly?


Thanks for your thoughts! Any clue will be appreciated. I am on my learning curve though I have years of development behind me (already)!

© Game Development or respective owner

Related posts about XNA

Related posts about algorithm