1

Hey guys I'm stuck trying to implement a jump when the up key is pressed with c++ in Visual Studio.I would like momentum as in while in air you can still use the right and left keys to move eg Super mario :D

This is Basically What I Have for my Main Class

(MyProjectMain):

void MyProjectMain::KeyDown(int iKeyCode)
{
    switch ( iKeyCode )
    {
    case SDLK_ESCAPE: // End program when escape is pressed
        SetExitWithCode( 0 );
        break;
    case SDLK_SPACE: // SPACE Pauses
        break;
    case SDLK_UP:
        Player::JumpPlayer();
        break;
    }
}

(Player class):

void Player::DoUpdate(int iCurrentTime)
{
        /*
        // Change speed if player presses a key 
        if ( GetEngine()->IsKeyPressed( SDLK_UP ) )
        m_iCurrentScreenY -= 5; 
        if ( GetEngine()->IsKeyPressed( SDLK_DOWN ) ) 
        m_iCurrentScreenY += 5;
        */
        if ( GetEngine()->IsKeyPressed( SDLK_LEFT ) ) 
        m_iCurrentScreenX -= 3; 
        if ( GetEngine()->IsKeyPressed( SDLK_RIGHT ) ) 
        m_iCurrentScreenX += 3; 
        if ( m_iCurrentScreenX < 0 ) 
        m_iCurrentScreenX = 0; 
        if ( m_iCurrentScreenX >= GetEngine()->GetScreenWidth() - m_iDrawWidth ) 
        m_iCurrentScreenX = GetEngine()->GetScreenWidth() - m_iDrawWidth; 
        if ( m_iCurrentScreenY < 0 ) 
        m_iCurrentScreenY = 0; 
        if ( m_iCurrentScreenY >= GetEngine()->GetScreenHeight() - m_iDrawHeight) 
        m_iCurrentScreenY = GetEngine()->GetScreenHeight() - m_iDrawHeight; 

        // Ensure that the object gets redrawn on the display, if something changed 


        RedrawObjects(); 
}


void Player::JumpPlayer(void)
{   

}

I must Mention That I have successfully made the sprite move left and right its just this jump that has been taking me so long. I would love if you can include your solution in the jump Function

Thanks A whole Lot !!!

ipalibowhyte
  • 1,553
  • 2
  • 22
  • 34
  • `m_iCurrentScreenY -= 3;` assuming the coordinate system has its origin at top-left corner (That is if you want to jump straight up) – avmohan Mar 22 '14 at 06:26
  • also u should check whether player is on the floor – avmohan Mar 22 '14 at 06:36
  • @v3ga Thats just going to shift the sprite to the top and nothing will happen after that. I am looking for a simple jump like in mario like when you press space bar, it jumps up and then comes back to the floor Thanks – ipalibowhyte Mar 22 '14 at 06:41
  • Some hints to find here: http://answers.unity3d.com/questions/151191/problem-with-jumping-and-frames-per-second.html – deviantfan Mar 22 '14 at 07:01
  • And some more here: http://stackoverflow.com/questions/13023652/game-jump-logic – Jongware Mar 22 '14 at 11:26

1 Answers1

1

Not sure what game engine or physics engine you're using, and if you're wanting to create your own, then you can ignore this answer :)

Anyways, no sense in reinventing the wheel. Cocos2D is an open source C++ game engine with Box2D physics engine support. I'd highly recommend checking it out.

Then, it becomes as simple as creating a CCSprite to represent your "mario" dude ;) and add a CCJumpBy action to him to simulate a parabolic jump movement.

If you want him to move left and right while in the air, check out this guy's post here

Good luck!

Community
  • 1
  • 1
ryrich
  • 2,164
  • 16
  • 24