精灵是如何跳跃的,它基于c++中的什么逻辑

How do sprites jump and what logic is it based upon in c++?

本文关键字:c++ 什么 何跳跃 跳跃 精灵      更新时间:2023-10-16

嘿,伙计们,我被困在试图实现跳跃时,向上键按下Visual Studio中的c++。我希望动量就像在空中你仍然可以使用左右键移动,例如超级马里奥:D

这就是我的Main类

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

(球员类):

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)
{   
}

我必须提到我已经成功地让精灵左右移动,只是这个跳跃花了我很长时间。我希望你能把你的解决方案包含在跳转函数

非常感谢!!

不确定你正在使用什么游戏引擎或物理引擎,如果你想创建自己的,那么你可以忽略这个答案:)

无论如何,没有必要重新发明轮子。Cocos2D是一个支持Box2D物理引擎的开源c++游戏引擎。我强烈建议你去看看。

然后,它就变得像创建一个CCSprite来代表你的"木偶"一样简单了。dude;),并添加一个CCJumpBy动作来模拟抛物线跳跃运动。

如果你想让他在空中左右移动,看看这个家伙的帖子在这里

祝你好运!