简体   繁体   中英

Updating Texture During Movement

I'm new to this so not completely sure whether I am posting this correctly, but I have been having a few issues when creating my game. My main goal is to create a topdown shooter styled game, using movement where the 'player' rotates based on the current position of the mouse and can press 'w' to move towards it at a set speed.

The main issue is, when the game loads, the movement works exactly how I want it to, but the texture itself is not moving, but only the drawRectangle.

Game1.cs:

player = new Player(Content, @"graphics\Player1", 500, 500, spritePosition);
        spritePosition = new Vector2(player.CollisionRectangle.X, player.CollisionRectangle.Y);

protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        KeyboardState keyboard = Keyboard.GetState();
        MouseState mouse = Mouse.GetState();
        IsMouseVisible = true;

        distance.X = mouse.X - spritePosition.X;
        distance.Y = mouse.Y - spritePosition.Y;

        //Works out the rotation depending on how far away mouse is from sprite
        rotation = (float)Math.Atan2(distance.Y, distance.X);

        // TODO: Add your update logic here
        spritePosition = spriteVelocity + spritePosition;
        spriteOrigin = new Vector2(player.DrawRectangle.X / 2, player.DrawRectangle.Y / 2);

        if (Keyboard.GetState().IsKeyDown(Keys.W))
        {
            spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
            spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
        }
        else if(spriteVelocity != Vector2.Zero)
        {
            Vector2 i = spriteVelocity;

            spriteVelocity = i -= friction * i;
        }

This is the main movement code from the Update function as well as where the new player has been created.

Player.cs:

class Player
{
    Texture2D sprite;
    Rectangle drawRectangle;

    int health = 100;

    public Player(ContentManager contentManager, string spriteName, int x , int y, Vector2 velocity)
    {
        LoadContent(contentManager, spriteName, x, y, velocity);
    }

    public Rectangle CollisionRectangle
    {
        get { return drawRectangle; }
    }

    public Rectangle DrawRectangle
    {
        get { return drawRectangle; }
        set { drawRectangle = value; }
    }

    public int Health
    {
        get { return health; }
        set {
            health = value;
                if (health <= 0)
                health = 0;

            if (health > 100)
                health = 100;
            }
    }

    public Vector2 Velocity
    {
        get { return Velocity; }
        set { Velocity = value; }
    }

    public void Update(GameTime gameTime, KeyboardState keyboard, MouseState mouse)
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(sprite, drawRectangle, Color.White);
    }

    private void LoadContent(ContentManager contentManager, string spriteName, int x, int y, Vector2 velocity)
    {
        sprite = contentManager.Load<Texture2D>(spriteName);
        drawRectangle = new Rectangle(x - sprite.Width / 2, y - sprite.Height / 2, sprite.Width, sprite.Height);
    }

}

I didn't know what to include in the Update function of the player.cs, whether the code for movement should go in there or the main Game1.cs.

Hopefully this is enough code for you guys to be able to help. Sorry for there being quite a lot of code, but I'm just unsure where the error is occurring.

Thanks in advance

For the rotation you would want to use 'another' kind of spriteBatch.Draw, because the draw function can have more parameters than your currently using.

The full draw function is as followed:

SpriteBatch.Draw(Texture2D texture, Vector2 position, Rectangle sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerdepht)

As you can see, now we have a parameter for rotation that can be used. So your code will look something like this:

spriteBatch.Draw(sprite, drawRectangle, null, Color.White, rotation, Vector2.Zero, 1, SpriteEffects.None, 0);

As for your question for where your code should go. It is good to learn yourself to put the code where it belongs, in this case the player. Because when your programs become bigger, it would be very messy to do it this way.

To call the update function in your player class, you can just simply call player.Update(gameTime, keyboard, mouse) in the Update of Game1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM