当w键按下时,马里奥执行跳跃动作
执行跳跃动作也是在MarioMove函数中调用的
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT4AAABOCAIAAAA2D+0SAAAGWElEQVR4nO2dS5LTMBCGfR8uyRU4xKy5B3tuwIolO1ZikSmX6Lfkjh72/1VqypFlWRL60sqQzhxlDT4+PmZ3AYCdOGZ34BOoC0ATUHcCx1d/2o+vh/iItBxp/7yF3atIYfB23fWByCqTeBt1s9a6KBUp5D6L6pKa2qtAfRdS0+2/cce+1yDgEp3BL9++18f1g5d39OMe6gYDl2tgCURdzefCBDZqinfkl2jqBp2EqO8gNKdcUXJgHAd5grriKhe9KopmRiH5Gb9ce6r1Vux8cAZAIv6cvjwUbYS6Na0h97q6JNBpRtlBm9fRwrhxLR+42LhdDTThzKYYYPnZYLkB1G3dMGtX2T0x2oxvg8UgHNkwQ+BE+tVN9Lbsr26rt8VTN9g4380WZkgwcoomi0NwdwRG/6FuFtY82nvgpjjs8hB1i26sHfR4ZBPbsU/ZCon1bZ+DTkLXd+BHXe33xvE4HGFrdePe8lN21BKtI6c6Cu04LPrf13/3FOgmOqeuqFe8LU9Slwcu7V2ivXcVN8zaLUrlvyabGLfFvombbXGw9lSAK4TmVAy8/KkRol32VbfD2/rYFokci+9s7ZbFHa92lbHrDvbNHTXIYpU5fYK6wRVsR7lIy24YFwvFfbX4dtfdxkcKwUVWmdN91QVgClAXgC2BugBsCdQFYEugLgBbAnXX4jhW+Re5Gfeb2J6PZCBfV0RcG8fx+Yi10Ly8juMQrzr+x20hUrOpS6+mOtrM7QxpObfBuTR8JKN+Sg6M4yA3UJdzLpX3qWtfFW8wd1mfrV0c0Zt6dQ/8wQTzdSPlBvdWN1a5c2Etq27KiGCvhjOS1nzdZ26Y+a74LOnbLTdtYo2aYolYmd+dHItd0vqpdVu7nB/b08L7xocWnJB96VfXUPSZUZeviitRlyzK4IWGkNdril3SarqvDomFvEviVfzp1lgj6cjXdU9pQN0B6hYzHrptNqkrtkkCo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OpOXrGuURoC4ATSBfNweoCwaTma9bHvxX7aEuGExmvu6TN8wF9oKxpOXrPjxzCN6CwSBfNweoCwaDfN0coC4YDPJ1c4C6YDCrLLit1cWnqcB4/gHWLDe2n2oYOgAAAABJRU5ErkJggg==" alt="" />
实际的跳跃则调用了mario对象的OnCtrlJump函数,来看看OnCtrlJump函数吧
void CMMario::OnCtrlJump()
{
//根据马里奥当前状态 来处理
switch(m_eCurMarioStatus)
{
case enMarioStatusStandLeft: //待机 和 跑动 时 马里奥允许跳跃
case enMarioStatusStandRight:
case enMarioStatusRunLeft:
case enMarioStatusRunRight:
{
//播放跳跃动画
switch(m_eCurMarioLevel)
{
case enMarioLevelSmall:
{
m_pCcbReader->getAnimationManager()->runAnimationsForSequenceNamed(_CCB_MARIO_SMALL_JUMP_);
}break;
case enMarioLevelBig:
{
m_pCcbReader->getAnimationManager()->runAnimationsForSequenceNamed(_CCB_MARIO_BIG_JUMP_);
}break;
case enMarioLevelMax:
{
m_pCcbReader->getAnimationManager()->runAnimationsForSequenceNamed(_CCB_MARIO_MAX_JUMP_);
}break;
} if(getActionByTag(enTagOfActionJump)==NULL)
{
//强制瞬间离地
setPositionY(getPositionY()+10.f);
//增加跳跃运动
CCJumpTo *pJump = CCJumpTo::create(_MARIO_BASIC_JUMP_TIME_,ccp(getPositionX(),getPositionY()-10.f),_MARIO_BASIC_JUMP_HEIGHT_,1);
if(pJump==NULL)
{
CCAssert(false,"Can't Create Jump!");
return;
}
//为Jump设置独立的Tag
pJump->setTag(enTagOfActionJump);
runAction(pJump); m_fMarioJumpMaxHeight = -100.f;
}
//将Mario状态设置为悬空
SetMarioStatus(m_bFaceToRight?enMarioStatusOnAirRight:enMarioStatusOnAirLeft); }break;
}
}
在代码中可以看到,如果马里奥已经在空中,就不能再跳跃了,只有在马里奥站立和行走时候可以跳跃。
当可以跳跃时,首先让马里奥对象播放跳跃动画,这个动画指马里奥本身的变化,比如马里奥的胳膊和腿,并不是马里奥跳跃的动作
跳跃的动作是pJump这个Action。
在runAction之前,先判断了这个action是否存在,如果这个action存在,则不需要再执行action了,这个也有避免连续两次跳跃的意思
和之前的状态判断差不多,去掉这个判断也是可以的,但是这样判断鲁棒性更好些吧。
最后根据马里奥的方向,设置马里奥的状态。
这里涉及新的知识点了,一个是执行动作runAction,一个是马里奥对象,我们分两个文章来介绍这两个知识。