1. 程式人生 > >cocos2dx之MoveTo,MoveBy

cocos2dx之MoveTo,MoveBy

MoveTo:把某一CCSprite移動到某個位置
MoveBy:把某一CCSprite移動一段距離,它有一個方法reverse,它讓物件按原路徑返回


void MyActionMoveToLayer::initLayer() {
    /*
    *@MoveTo,移動到某個Point
    */
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCSprite *spriteMoveTo = CCSprite::create("snow.png");
    spriteMoveTo->setPosition(ccp(size.width / 2.0f, size.height / 2.0f));
    this->addChild(spriteMoveTo, 1);

    CCActionInterval *forward = CCMoveTo::create(4, ccp(300, 300));
    spriteMoveTo->runAction(forward);


    /*
    *MoveBy,移動一段距離
    */
    CCSprite *spriteMoveBy = CCSprite::create("snow.png");
    spriteMoveBy->setPosition(ccp(size.width / 4.0f, size.height / 4.0f));
    this->addChild(spriteMoveBy, 1);

    CCActionInterval *forwardBy = CCMoveBy::create(2, ccp(200, 300));
    CCActionInterval *backBy = forwardBy->reverse();
    CCAction *action = CCRepeat::create(dynamic_cast<CCFiniteTimeAction *>(CCSequence::create(forwardBy, backBy, NULL)), 4);
    spriteMoveBy->runAction(action);
}