我正在尝试使用Cocos2d-x制作类似MegaMan的游戏并试图让它在Android设备上运行。 问题是,我可以单独按下按钮,没有任何问题,但当我左/右跑并按下跳跃时,播放器停止移动。
当我手动设置right = true并且jump = true时,玩家会向右侧跳转,所以if else的东西执行得很好。
这是我在init()
中所做的 //add controls
buttonLeft = CCMenuItemImage::create(
"button_left.png",
"button_left.png");
buttonLeft->setPosition( ccp(buttonLeft->getContentSize().width/2, buttonLeft->getContentSize().height/2) );
buttonRight = CCMenuItemImage::create(
"button_right.png",
"button_right.png");
buttonRight->setPosition( ccp(buttonRight->getContentSize().width/2 + buttonLeft->getContentSize().width, buttonRight->getContentSize().height/2) );
CCMenu* controlMenu1 = CCMenu::create(buttonLeft, buttonRight, NULL);
controlMenu1->setPosition(CCPointZero);
this->addChild(controlMenu1, 1);
buttonJump = CCMenuItemImage::create(
"button_jump.png",
"button_jump.png");
buttonJump->setPosition(ccp(winSize.width - buttonJump->getContentSize().width/2, buttonJump->getContentSize().height/2));
CCMenu* controlMenu2 = CCMenu::create(buttonJump, NULL);
controlMenu2->setPosition(CCPointZero);
this->addChild(controlMenu2, 1);
我每0.02秒安排一次:
void BeginScene::updatePlayerMove(float dt)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int newYpos;
int maxYpos;
int minYpos;
int newXpos;
int maxXpos;
int minXpos;
if(buttonJump->isSelected() && !maxJumpReached){
//jump
if(playerMoveAction != NULL){
player->stopAction(playerMoveAction);
playerMoveAction = NULL;
}
if(playerJumpAction != NULL){
player->stopAction(playerJumpAction);
playerJumpAction = NULL;
}
if(buttonLeft->isSelected()){
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player2.png"));
minXpos = player->getContentSize().height/2;
newXpos = player->getPosition().x - PIXELS_PLAYER_MOVES;
if(newXpos < minXpos){
newXpos = minXpos;
}
}else if(buttonRight->isSelected()){
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player1.png"));
maxXpos = winSize.width - player->getContentSize().height/2;
newXpos = player->getPosition().x + PIXELS_PLAYER_MOVES;
if(newXpos > maxXpos){
newXpos = maxXpos;
}
}else{
newXpos = player->getPosition().x;
}
maxYpos = player->getContentSize().height/2 + closestGround + MAX_JUMP_HEIGHT;
newYpos = player->getPosition().y + PIXELS_PLAYER_MOVES;
if(newYpos > maxYpos){
newYpos = maxYpos;
maxJumpReached = true;
}
CCPoint destination = ccp(newXpos, newYpos);
float lengthX = destination.x - player->getPosition().x;
float lengthY = destination.y - player->getPosition().y;
float length = sqrtf((lengthX * lengthX) + (lengthY*lengthY));
float duration = length/DURATION_DIVIDER;
playerJumpAction = player->runAction( CCSequence::create(
CCMoveTo::create(duration, destination),
CCCallFuncN::create(this, NULL),
NULL) );
}else if(!buttonJump->isSelected() || maxJumpReached){ //basicly: if not jumping -> gravity will pull always)
//descent
if(playerMoveAction != NULL){
player->stopAction(playerMoveAction);
playerMoveAction = NULL;
}
if(playerJumpAction != NULL){
player->stopAction(playerJumpAction);
playerJumpAction = NULL;
}
if(buttonLeft->isSelected()){
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player2.png"));
minXpos = player->getContentSize().height/2;
newXpos = player->getPosition().x - PIXELS_PLAYER_MOVES;
if(newXpos < minXpos){
newXpos = minXpos;
}
}else if(buttonRight->isSelected()){
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player1.png"));
maxXpos = winSize.width - player->getContentSize().height/2;
newXpos = player->getPosition().x + PIXELS_PLAYER_MOVES;
if(newXpos > maxXpos){
newXpos = maxXpos;
}
}else{
newXpos = player->getPosition().x;
}
minYpos = player->getContentSize().height/2 + closestGround;
newYpos = player->getPosition().y - PIXELS_PLAYER_MOVES;
if(newYpos < minYpos){
newYpos = minYpos;
maxJumpReached = false;
jumpButtonPressed = false;
}
CCPoint destination = ccp(newXpos, newYpos);
float lengthX = destination.x - player->getPosition().x;
float lengthY = destination.y - player->getPosition().y;
float length = sqrtf((lengthX * lengthX) + (lengthY*lengthY));
float duration = length/DURATION_DIVIDER;
playerDescentAction = player->runAction( CCSequence::create(
CCMoveTo::create(duration, destination),
CCCallFuncN::create(this, NULL),
NULL) );
}else if(buttonLeft->isSelected()){
//left
minXpos = player->getContentSize().height/2;
newXpos = player->getPosition().x - PIXELS_PLAYER_MOVES;
if(newXpos < minXpos){
newXpos = minXpos;
}
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player2.png"));
CCPoint destination = ccp(newXpos, player->getPosition().y);
float lengthX = destination.x - player->getPosition().x;
float lengthY = destination.y - player->getPosition().y;
float length = sqrtf((lengthX * lengthX) + (lengthY*lengthY));
float duration = length/DURATION_DIVIDER;
playerMoveAction = player->runAction( CCSequence::create(
CCMoveTo::create(duration, destination),
CCCallFuncN::create(this, NULL),
NULL) );
}else if(buttonRight->isSelected()){
//right
maxXpos = winSize.width - player->getContentSize().height/2;
newXpos = player->getPosition().x + PIXELS_PLAYER_MOVES;
if(newXpos > maxXpos){
newXpos = maxXpos;
}
player->setTexture(CCTextureCache::sharedTextureCache()->addImage("player1.png"));
CCPoint destination = ccp(newXpos, player->getPosition().y);
float lengthX = destination.x - player->getPosition().x;
float lengthY = destination.y - player->getPosition().y;
float length = sqrtf((lengthX * lengthX) + (lengthY*lengthY));
float duration = length/DURATION_DIVIDER;
playerMoveAction = player->runAction( CCSequence::create(
CCMoveTo::create(duration, destination),
CCCallFuncN::create(this, NULL),
NULL) );
}
}
答案 0 :(得分:0)
它似乎只是在我的旧LG上工作,但在我的Galaxy s2和平板电脑上完美运行......
所以可能我的LG太老了:P