在update函数中写跳跃函数使主角没帧跳跃一小段位移 可以让跳跃动作看起来更加的自然 加入重力加速度等变量 运动更合理@H_404_3@
首先在主角的初始化代码中进行初始起跳速度和重力加速的初始化(PS.重力加速度和初始速度不能用现实世界中的标准进行初始化)@H_404_3@
//这段代码中设置了主角的起跳速度m_speedY下落速度m_speedfall 重力加速度m_speedGrivaty 并且调用了update函数this->scheduleUpdate();@H_404_3@
@H_404_3@
bool Hero::init()@H_404_3@
{ m_speedfall=0;@H_404_3@
m_speedGrivaty=-500.0f;@H_404_3@
m_speedY=250.0f;@H_404_3@
m_isJumping=false;@H_404_3@
this->scheduleUpdate();@H_404_3@
return true;@H_404_3@
}@H_404_3@
//这段代码位于update函数中 功能是主角的相关位移 运用了运动公式 对位移和速度变化的计算 然后不断重置主角的位置@H_404_3@
if(myHero->m_isJumping==true)@H_404_3@
{ @H_404_3@
auto changeY=myHero->m_speedY*data+(1/2)*myHero->m_speedGrivaty*data*data;@H_404_3@
myHero->m_speedY=myHero->m_speedY+myHero->m_speedGrivaty*data;@H_404_3@
myHero->m_sprite->setPositionY(myHero->m_sprite->getPositionY()+changeY);@H_404_3@
}@H_404_3@
//这段代码也位于update函数中 主要用于跳跃结束后 重置主角数据并将跳跃状态设置为false@H_404_3@
if(isCollideWithHeroX()==true&&myHero->m_isJumping==true&&myHero->m_speedY<0.0f)@H_404_3@
{ @H_404_3@
myHero->m_isJumping=false;@H_404_3@
myHero->m_sprite->stopAllActions();@H_404_3@
myHero->m_sprite->runAction(Hero::createAnimate());@H_404_3@
myHero->m_speedY=250.0f;@H_404_3@
return;@H_404_3@
}@H_404_3@