簡體   English   中英

在運動過程中更新紋理

[英]Updating Texture During Movement

我是新手,所以不能完全確定我是否正確發布,但在創建游戲時我遇到了一些問題。 我的主要目標是創建一個自上而下的射擊游戲風格的游戲,使用“玩家”根據鼠標當前位置旋轉的動作,並按“w”以設定的速度向它移動。

主要問題是,當游戲加載時,運動完全符合我的要求,但紋理本身並沒有移動,只有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;
        }

這是Update功能的主要移動代碼以及新玩家的創建位置。

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);
    }

}

我不知道在player.cs的Update函數中包含什么,移動代碼應該放在那里還是主要的Game1.cs。

希望這是足夠的代碼,讓你們能夠提供幫助。 很抱歉有很多代碼,但我只是不確定錯誤發生在哪里。

提前致謝

對於旋轉,您可能希望使用“另一種”spriteBatch.Draw,因為繪制函數可以具有比當前使用的參數更多的參數。

完整繪制功能如下:

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

如您所見,現在我們有一個可以使用的旋轉參數。 所以你的代碼看起來像這樣:

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

至於你的代碼應該去哪里的問題。 學習自己將代碼放在它所屬的位置是很好的,在這種情況下是播放器。 因為當你的程序變得更大時,這樣做會非常麻煩。

要在您的播放器類中調用更新功能,您只需在Game1的更新中調用player.Update(gameTime,keyboard,mouse)即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM