【问题标题】:Updating Texture During Movement在运动期间更新纹理
【发布时间】:2017-09-16 23:23:07
【问题描述】:

我是新手,所以不完全确定我是否正确发布了此内容,但我在创建游戏时遇到了一些问题。我的主要目标是创建一个自上而下的射击游戏,使用“玩家”根据鼠标当前位置旋转并可以按“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;
        }

这是来自更新函数的主要移动代码以及新玩家的创建位置。

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 中。

希望这些代码足以帮助你们。很抱歉有很多代码,但我不确定错误发生在哪里。

提前致谢

【问题讨论】:

  • Mosty,我使用SpriteBatch.Draw Method (Texture2D, Vector2, Rectangle, Color) 在位置绘制精灵。其中vector2是位置,矩形是纹理来源。

标签: c# xna monogame


【解决方案1】:

对于旋转,您可能希望使用“另一种” spriteBatch.Draw,因为绘图函数可以有比您当前使用的更多的参数。

完整的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 的 Update 中调用 player.Update(gameTime, keyboard, mouse) 即可。

【讨论】:

  • 感谢您的回复,它确实帮助我开始更好地理解课程。唯一让我感到困惑的另一件事是,我知道我会将移动代码放在类的更新函数中,但是如果它只是一个类,它如何获得它的当前位置?
  • 你使用 sprite.Position 作为玩家位置,对吧?你可以在你的播放器类中得到它,因为你在你的播放器类中初始化了精灵。另外我认为您的想法有些错误,这不仅仅是“一堂课”。类可以相互交流,这使得整个程序中的一切都变得井井有条。
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2012-09-08
相关资源
最近更新 更多