Kinggadino
Kinggadino

Reputation: 47

XNA 4 objects not moving once GameState has changed from the main menu to playing

Everything works fine when in GameState.MainMenu but when I click btnPlay (DCButton Class) it switches to GameState.Playing (loads the map/player) but nothing will animate in case GameState.Playing.

protected override void Update(GameTime gameTime)
{
    MouseState mouse = Mouse.GetState();

    switch (CurrentGameState)
    {
        case GameState.MainMenu:
            if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
            btnPlay.Update(mouse);
            if (btnOptions.isClicked == true) CurrentGameState = GameState.Options;
            btnOptions.Update(mouse);
            if (btnExit.isClicked == true) Exit();
            btnExit.Update(mouse);
            break;
        case GameState.Options:
            break;
        case GameState.Playing:
            player.Update();
            // Exit();
            break;
    }

    base.Update(gameTime);
}

DCPlayer (Class) Update() method consists of getting keyboard input and changing the position of the player. I have also tried this:

case GameState.Playing:
    player.Position.X += 1; // <- Position is a Vector2
    break;

But this doesn't animate the sprite either. Does anyone know why this might be happening?

NOTE: CurrentGameState is a enum called GameState

EDIT: Draw()

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();

    switch (CurrentGameState)
    {
        case GameState.MainMenu:
            spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
            btnPlay.Draw(spriteBatch);
            btnOptions.Draw(spriteBatch);
            btnExit.Draw(spriteBatch);
            break;
        case GameState.Options:
            spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
            break;
        case GameState.Playing:
            int progress = level.GetLevelProgress();
            level.level = progress;
            level.LoadLevel(); // <- This is where the map gets loaded from the map file
            level.Draw(spriteBatch, Content); // <- This is were the map gets rendered
            player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice); // <- Could this be the problem, I initialized it here because the grid only gets initialized when the map is loaded
            player.Draw(spriteBatch, Content); // <- Draw player in grid using map information (Vector2 DCLevel.Start)
            break;
        }
    spriteBatch.End();


    base.Draw(gameTime);
}

Upvotes: 0

Views: 26

Answers (1)

Sayse
Sayse

Reputation: 43320

In your draw method you reassign player to a new instance every time which means it never gets chance to use its actual position. By the looks of the code you've shown you can just remove this line from the Draw method

player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice); 

Upvotes: 1

Related Questions