Sprite 无法移动 cocos2d-x

Sprite can not move in cocos2d-x

我是 cocos2d-x 的新手,我关注 Cocos2d-x Game Development Essentials 电子书并尝试在 iOS 中制作演示应用程序。在它的指导的帮助下,但是当我尝试按照书中所写在 GameScreen.cpp 文件中移动 Astroids 时,它们实际上并没有移动,我不明白我错过了什么?

这是我使用的代码:

bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    //Add EVENT LISTENER TO HANDLE TOUCH EVENT,
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    listener->onTouchBegan      = CC_CALLBACK_2(GameScene::ontouchBegin,this);
    listener->onTouchMoved      = CC_CALLBACK_2(GameScene::ontouchMoved, this);
    listener->onTouchCancelled  = CC_CALLBACK_2(GameScene::ontouchCanceled, this);
    listener->onTouchEnded      = CC_CALLBACK_2(GameScene::ontouchEnded, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    istouching = false;
    touchPosition = 0;

    visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();

    auto backgroundSprite = Sprite::create("Game_Screen_Background.png");
    backgroundSprite->setPosition(Point((visibleSize.width/2)+origin.x,(visibleSize.height/2)+origin.y));
    this->addChild(backgroundSprite, -1);

    auto pauseItem =
    MenuItemImage::create("Pause_Button.png","Pause_Button(Click).png",CC_CALLBACK_1(GameScene::PushToGamePauseScene, this));
    pauseItem->setPosition(Point(pauseItem->getContentSize().width-(pauseItem->getContentSize().width/4)+origin.x,visibleSize.height - pauseItem->getContentSize().height +(pauseItem->getContentSize().width / 4) + origin.y));
    auto menu = Menu::create(pauseItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu);
    for (int i = 0; i < 2; i ++){
        backgroundSpriteArray[i] = Sprite::create("Game_Screen_Background.png");
        backgroundSpriteArray[i]->setPosition(Point((visibleSize.width/2)+origin.x,(-1*visibleSize.height*i)+(visibleSize.height/2)+origin.y));
        this->addChild(backgroundSpriteArray[i],-2);
    }
    playerSprite = Sprite::create("Space_Pod.png");
    playerSprite->setPosition(Point(visibleSize.width/2,pauseItem->getPosition().y-(pauseItem->getContentSize().height/2)-(playerSprite->getContentSize().height/2)));
    this->addChild(playerSprite, 1);
    this->schedule(schedule_selector(GameScene::spawnAsteroid), 1.0);
this->scheduleUpdate();
    return true;
}

更新方法如下

void GameScene::update(float dt){
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    for (int i = 0; i < 2; i ++){
        if (backgroundSpriteArray[i]->getPosition().y >=visibleSize.height + (visibleSize.height / 2) -1){
            backgroundSpriteArray[i]->setPosition(Point((visibleSize.width / 2) + origin.x, (-1 *visibleSize.height) + (visibleSize.height / 2)));
        }
    }
    for (int i = 0; i < 2; i ++){
        backgroundSpriteArray[i]->setPosition(Point(backgroundSpriteArray[i]->getPosition().x,backgroundSpriteArray[i]->getPosition().y + (0.75 *visibleSize.height * dt)));
    }
    for (int i = 0; i < asteroids.size(); i++){
        asteroids[i]->setPosition(Point(asteroids[i]->getPosition().x,asteroids[i]->getPosition().y+(0.75 * visibleSize.height * dt)));
        if (asteroids[i]->getPosition().y > visibleSize.height * 2){
            this->removeChild(asteroids[i]);
            asteroids.erase(asteroids.begin() + i);
        }
    }
    //check for Touching
    if(istouching == true){
        //now check, which side of the screen is being touched
        if(touchPosition < visibleSize.width /2){
            //now, move the space pod to left
            float positionX = playerSprite->getPosition().x;
            playerSprite->setPositionX(positionX - (0.50* visibleSize.width * dt));
            positionX = playerSprite->getPositionX();

            //prevent space pod to getting off the screen (left side)
            if(positionX <= 0+(playerSprite->getContentSize().width/2))
            {
                playerSprite->setPositionX(playerSprite->getContentSize().width / 2);
            }
        }
        else{
            //now, move the space pod to right
            float positionX = playerSprite->getPositionX();
            playerSprite->setPositionX(positionX + (0.50 * visibleSize.width *dt));
            positionX = playerSprite->getPositionX();

            // check to prevent the space pod from going off the screen (right side)
            if(positionX >= visibleSize.width-(playerSprite->getContentSize().width/2))
            {
                playerSprite->setPositionX(visibleSize.width-(playerSprite->getContentSize().width/2));
            }
        }
    }
}

移动小行星代码:

void GameScene::spawnAsteroid(float dt){
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    int asteroidIndex = (arc4random() % 3) + 1;
    __String* asteroidString = __String::createWithFormat("Asteroid_%i.png",asteroidIndex);
    auto *tempAsteroid = Sprite::create(asteroidString->getCString());
    int xRandomPosition = (arc4random() % (int)(visibleSize.width - (tempAsteroid->getContentSize().width / 2))) + (tempAsteroid->getContentSize().width / 2);
    tempAsteroid->setPosition(Point(xRandomPosition + origin.x, -tempAsteroid->getContentSize().height +origin.y));
    asteroids.push_back(tempAsteroid);
    this->update(touchPosition);
    this->addChild(asteroids[asteroids.size() - 1], -1);

}

该方法已在 运行 时间被调用,但不知道我的小行星物体没有移动任何人都可以帮助我解决问题以及如何解决它。

编辑

好的,我将 this->scheduleUpdate() 放入 init() 中,并开始在屏幕上移动小行星,但是当我使用 touchBegin 事件移动我的精灵(不是小行星)时,小行星从屏幕上消失了。请指导我。

请把这一行放在你的初始化方法中

this->scheduleUpdate();

希望对您有所帮助

Sprite->runAction(Sequence::create(MoveTo::create(1.0f, Vec2(100,200)),NULL));