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