GameCorder.net

このエントリーをはてなブックマークに追加

left to right animation with forever

When want use animation forever.
Like left to right move.
Use RepeatForever class and MoveTo and Sequence.
In this case I want to show left to right animation.
Let's see example.


// back arrow
backArrow = ui::Button::create();
backArrow->setAnchorPoint(Vec2(0,0.5));
backArrow->loadTextures("res/back.png", "res/back.png");
backArrow->addTouchEventListener(CC_CALLBACK_2(RankingScene::buttonTouchEventListener, this));
backArrow->setPosition(cocos2d::Point(8,DISPLAY_HEIGHT / 2));
backArrow->setScale9Enabled(true);
backArrow->setPressedActionEnabled(true);
backArrow->setZoomScale(0.05f);
backArrow->setTag(PAGE_BACK_BTN_TAG);
backArrow->setVisible(false);
base->addChild(backArrow);
// animation
// 1.MoveTo
MoveTo* leftToRight = MoveTo::create(ARROW_ANIMATE_TIME, Vec2(backArrow->getPosition().x + 10,backArrow->getPosition().y));
MoveTo* rightToLeft = MoveTo::create(ARROW_ANIMATE_TIME, Vec2(backArrow->getPosition().x,backArrow->getPosition().y));
// 2.sequense
auto sequense = Sequence::create(leftToRight, rightToLeft, NULL);
// 3.repeat action
RepeatForever *pAction = RepeatForever::create(sequense);
backArrow->runAction(pAction);

// forward arrow
forwardArrow = ui::Button::create();
forwardArrow->setAnchorPoint(Vec2(0,0.5));
forwardArrow->loadTextures("res/forward.png", "res/forward.png");
forwardArrow->addTouchEventListener(CC_CALLBACK_2(RankingScene::buttonTouchEventListener, this));
forwardArrow->setPosition(cocos2d::Point(DISPLAY_WIDTH - 8 - forwardArrow->getContentSize().width,DISPLAY_HEIGHT / 2));
forwardArrow->setScale9Enabled(true);
forwardArrow->setPressedActionEnabled(true);
forwardArrow->setZoomScale(0.05f);
forwardArrow->setTag(PAGE_FORWARD_BTN_TAG);
base->addChild(forwardArrow);
// animation
leftToRight = MoveTo::create(ARROW_ANIMATE_TIME, Vec2(forwardArrow->getPosition().x,forwardArrow->getPosition().y));
rightToLeft = MoveTo::create(ARROW_ANIMATE_TIME, Vec2(forwardArrow->getPosition().x + 10,forwardArrow->getPosition().y));
sequense = Sequence::create(rightToLeft, leftToRight, NULL);
pAction = RepeatForever::create(sequense);
forwardArrow->runAction(pAction);

		

1.MoveTo class

MoveTo is create animation want to move.
In this part move to +10 against current position.

2.Sequence

Next,sequence class.
sequence ofcource it self.
In this part right next left sequence action.

3.repeat forever

When we want to create infinity action use RepeatForever
. In this case create sequence action forever left to right move.

Finally run this action by button.

This is available in cocos2dx 3.15.1