【问题标题】:Improper animation after clicking the mouse button单击鼠标按钮后动画不正确
【发布时间】:2013-10-11 02:56:18
【问题描述】:

当我在下面的代码中运行时,我的预期输出是播放器应该连续动画,并且当用户点击屏幕时播放器应该跳跃。我的播放器的帧不同,跳帧也不同。我在鼠标左键单击时调用帧。所有帧都已处理,但速度太快,因此动画不可见。

    private void OnUpdate(object sender, GameTimerEventArgs e)
    {
        float elapsed = (float)e.ElapsedTime.TotalSeconds;
        timer5 -= elapsed;

        TouchPanel.EnabledGestures = GestureType.Tap;
        TouchCollection touches = TouchPanel.GetState();

        MouseState mousestate = Mouse.GetState();

        //Texture.position += velocity;
        //if (rect.Contains(new Microsoft.Xna.Framework.Point(mousestate.X, mousestate.Y)) && mousestate.LeftButton == ButtonState.Pressed)

        if (mousestate.LeftButton==ButtonState.Pressed)
        {
            for (int i = 0; i < 32; i++)
            {

               jump();
                jumpframe++;
                if (jumpframe > 24)
                {
                    jumpframe = 0;
                }
                System.Diagnostics.Debug.WriteLine("currentframe:"+ jumpframe);
            }
        }

        if (timer5 <= 0)
        {
            playerr();
            currentframe++;
            if (currentframe > 24)
            {
                currentframe = 0;
            }
            timer5 = TIME_BET_FRAME;
        }
    }

    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();

        //level.Draw(spriteBatch);
        rect2 = new Rectangle(0, 0, 960, 620);
        rect = new Rectangle(0, 0, 420, 300);

        spriteBatch.Draw(Texture.player[currentframe], Texture.position, Color.White);
        spriteBatch.Draw(Texture.jumpplayer[jumpframe], Texture.jumpposition,                 Color.White);
        spriteBatch.Draw(Texture.jumpbtn, Texture.rect, Color.White);
        spriteBatch.DrawString(font, "Score", new Vector2(10, 0), Color.Gold);
        spriteBatch.End();
    }

    public void jump()
    {
        Texture.jumpposition.X = 10;
        Texture.jumpposition.Y = 243;
        Texture.position.X = -400;
        Texture.position.Y = 243;    
    }

    public void playerr()
    {
        Texture.position.X = 10;
        Texture.position.Y = 243;
        Texture.jumpposition.X = -400;
        Texture.jumpposition.Y = 243;
    }

【问题讨论】:

  • 您已经说明了问题 - “太快了”。通过创建计时器分割作业来减慢速度。
  • @Sinatr 我已经将 timer5 用于播放器.......所以现在我可以将它用于 jumpplayer 吗???...thnx 用于响应
  • 也可以使用一些比较容易理解的变量名(而不是timer5、rect2、something2000),等项目长大一个月后就会迷路。

标签: c# windows-phone-7 xna


【解决方案1】:

我看到的是循环

for(int i=0;i<32;i++){...}

这是在OnUpdate中一次绘制完整的跳跃动画。

你可以做什么:

bool _jump;

private void OnUpdate(object sender, GameTimerEventArgs e)
{
    // ...
    if (mousestate.LeftButton==ButtonState.Pressed)
    {
         if(!_jump)
         {
             // start jumping
             _jump = true;
         }
         else
         {
             // jumping in progress
             _jumpframe++;
             // end of jumping check
             if(_jumpframe > 24)
             {
                 _jumpframe = 0;
                 _jump = false;
             }
         }
         jump();
    }
}

这应该以OnUpdate的速度(频率)执行跳跃动画。

【讨论】:

  • 矩形相交方法的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多