0

最近我将我的代码从非动画精灵更改为动画。现在我在图像的角度上遇到了一些问题。

当我转身时,我可以看到角度有效,因为火球朝正确的方向射击,但动画精灵却没有。我曾尝试在SpriteBatch.Draw方法中使用 base.Angle,但它不需要 5 个参数。

这是我的代码:

class Dragon : MovingGameObj
{
    public Dragon()
    {
        framerate = 14;
        timetolive = 3000;
        repeat = 1;
        timer = 0;
        framesX = 1;
        framesY = 2;
        frameSize = 100;
        alive = true;
        activeframe = 0;
        MaxSpeed = 2.5F;
        FireballPower = 0;
        prevKs = Keyboard.GetState();
        prevKs2 = Keyboard.GetState();
        Life = 100F;
        Kills = 0;
        Angle = -(float)(Math.PI / 2);    
    }

    public bool alive;
    private int activeframe;
    int tmpTid = 0;
    public int framerate { set; get; }
    public int timetolive { set; get; }
    public int repeat { set; get; }
    public int framesX { set; get; }
    public int framesY { set; get; }
    public int timer { set; get; }
    public int frameSize { set; get; }
    public bool Enemy { get; set; }
    public float MaxSpeed { get; set; }
    public float FireballPower { get; set; }
    public int WeaponType { get; set; }
    public bool FireballFired { get; set; }
    public float Life { get; set; }
    public int Kills { get; set; }

    protected KeyboardState prevKs;

    protected KeyboardState prevKs2;
    public void Respawn()
    {
        Life = 100F;
        Random randomerare = new Random();
        Position = new Vector2(randomerare.Next(1000), randomerare.Next(1000));
        Angle = 0F;
    }

    public override void Update(GameTime gameTime)
    {
        KeyboardState ks = Keyboard.GetState();
        KeyboardState ks2 = Keyboard.GetState();
        #region Player 1
        if (Enemy == false)
        {
            if (ks.IsKeyDown(Keys.Up))
            {
                if (Speed < 0) Speed = 0;
                if (Speed < MaxSpeed) Speed = Speed * 1.005F + 0.01F;
                else Speed = MaxSpeed;
            }
            if (ks.IsKeyDown(Keys.Down))
            {
                if (Speed > -1.0F) Speed -= 0.04F;
                else Speed = -1.0F;
            } 
            if (ks.IsKeyUp(Keys.Down) && ks.IsKeyUp(Keys.Up) && Speed > 0)
            {
                Speed -= 0.01F;
                if (Speed <= 0) Speed = 0;
            }
            if (ks.IsKeyUp(Keys.Down) && ks.IsKeyUp(Keys.Up) && Speed < 0)
            {
                Speed += 0.01F;
                if (Speed >= 0) Speed = 0;
            }

            if (ks.IsKeyUp(Keys.Left))
            {
                Angle += 0.02F;
            }
            if (ks.IsKeyUp(Keys.Right))
            {
                Angle -= 0.02F;
            }
            if (ks.IsKeyDown(Keys.Space))
            {
                if (FireballPower < 100)
                    FireballPower += 0.5F;
                else
                    FireballPower = 100;
            }

            if (ks.IsKeyUp(Keys.Space) && prevKs.IsKeyDown(Keys.Space))
            {
                //FireballPower = 0;
                FireballFired = true;
            }
        }
        #endregion
        #region Player 2
        if (Enemy == true)
        {
            if (ks2.IsKeyDown(Keys.W))
            {

                if (Speed < 0) Speed = 0;
                if (Speed < MaxSpeed) Speed = Speed * 1.005F + 0.01F;
                else Speed = MaxSpeed;
            }
            if (ks2.IsKeyDown(Keys.S))
            {
                if (Speed > -1.0F) Speed -= 0.04F;
                else Speed = -1.0F;
            }
            if (ks2.IsKeyUp(Keys.S) && ks2.IsKeyUp(Keys.W) && Speed > 0)
            {
                Speed -= 0.01F;
                if (Speed <= 0) Speed = 0;
            }
            if (ks2.IsKeyUp(Keys.S) && ks2.IsKeyUp(Keys.W) && Speed < 0)
            {
                Speed += 0.01F;
                if (Speed >= 0) Speed = 0;
            }

            if (ks2.IsKeyUp(Keys.A))
            {
                Angle += 0.02F;
            }
            if (ks2.IsKeyUp(Keys.D))
            {
                Angle -= 0.02F;
            }
            if (ks2.IsKeyDown(Keys.LeftControl))
            {
                if (FireballPower < 100)
                    FireballPower += 0.5F;
                else
                    FireballPower = 100;
            }

            if (ks2.IsKeyUp(Keys.LeftControl) && prevKs2.IsKeyDown(Keys.LeftControl))
            {
                //FireballPower = 0;
                FireballFired = true;
            }
        }
        #endregion
        prevKs = ks;
        prevKs2 = ks2;

        Direction = new Vector2((float)Math.Cos(Angle), (float)Math.Sin(Angle));

        timer += gameTime.ElapsedGameTime.Milliseconds;
        if (timer > timetolive)
        {
            alive = false;
        }
        tmpTid += gameTime.ElapsedGameTime.Milliseconds;
        if (tmpTid > (1000 / framerate))
        {
            activeframe += 1;
            tmpTid = 0;
            if (activeframe > (framesX * framesY))
            {
                activeframe = 0;
            }
        }
        base.Update(gameTime);
    }

    public override void Draw(SpriteBatch spriteBatch)
    {
        Rectangle tmp = new Rectangle(activeframe%framesX*frameSize,activeframe/framesY*frameSize,frameSize,frameSize);
        spriteBatch.Draw(base.Gfx,base.Position, tmp,Color.White);
    }                 
}
4

1 回答 1

0

尝试使用这个SpriteBatch.Draw()重载:

spriteBatch.Draw(base.Gfx, base.Position, tmp, Color.White, Angle, Vector2.Zero, 1f, SpriteEffects.None, 0f);
于 2012-12-13T09:31:15.803 回答