1

When I run below code my expected output is that the player should continuosly animating and when user taps on screen the player should jump. My player's frames are diffrent and jump frames are different. I am calling frames on mouse left button click. All frames are processed but its too fast so the animation is not visible.

    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;
    }
3
  • 1
    You already stated the problem - "too fast". Slow down by creating timer divided job.
    – Sinatr
    Commented Oct 10, 2013 at 14:40
  • @Sinatr i have already used the timer5 for player.......so now can i use the same for jumpplayer???...thnx for responding
    – pravin
    Commented Oct 10, 2013 at 14:42
  • 2
    also use some more understandable variable names (instead of timer5, rect2, something2000), you will get lost after a month when project will grow up. Commented Oct 11, 2013 at 6:57

1 Answer 1

2

What I see is the loop

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

which is drawing a complete jump animation at once in the OnUpdate.

What you can do instead:

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

This should perform animation of jumping with the speed (frequency) of OnUpdate.

1
  • problem with intersect method of rectangle
    – pravin
    Commented Oct 14, 2013 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.