0

我正在尝试使用 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) );

}

}

4

1 回答 1

0

似乎它不仅不能在我的旧 LG 上运行,但在我的 Galaxy s2 和平板电脑上却能完美运行......

所以可能我的LG太旧了:P

于 2012-12-10T12:49:09.440 回答