Cocos2d-X官方Demo---1.ActionManager

void HelloWorld::onEnter()
{
Scene::onEnter();

Size visibleSize = Director::getInstance()->getVisibleSize();
char*s_pathGrossini = "grossini.png";


//1.CrashTest
auto child = Sprite::create(s_pathGrossini);
child->setPosition(visibleSize/2);
addChild(child, 1, 1);


//Sum of all action's duration is 1.5 second.
child->runAction(RotateBy::create(1.5f, 90));
child->runAction(Sequence::create(
DelayTime::create(1.4f),
FadeOut::create(1.1f),
nullptr)
);


//After 1.5 second, self will be removed.
child->runAction(Sequence::create(
DelayTime::create(1.4f),
CallFunc::create(/*CC_CALLBACK_0(CrashTest::removeThis, this)*/[this]() {
auto child = getChildByTag(1);
child->removeChild(child, true);
}),nullptr));


//2.LogicTest
auto grossini = Sprite::create(s_pathGrossini);
addChild(grossini, 0, 2);
grossini->setPosition(visibleSize/2);


grossini->runAction(Sequence::create(
MoveBy::create(1, Vec2(150, 0)),
CallFuncN::create(/*CC_CALLBACK_1(LogicTest::bugMe, this))*/[](Node*node) {
node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
node->runAction(ScaleTo::create(2, 2));
}),nullptr));



//3.PauseTest 暂停测试
auto l = Label::createWithTTF("After 5 seconds grossini should move", "Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition(visibleSize.width/2, visibleSize.height - 75);


//
// Also, this test MUST be done, after [super onEnter]
//
auto grossini = Sprite::create(s_pathGrossini);
addChild(grossini, 0, 1);
grossini->setPosition(visibleSize/2);


auto action = MoveBy::create(1, Vec2(150, 0));


auto director = Director::getInstance();
director->getActionManager()->addAction(action, grossini, true);


auto callback = (std::function<void(float)>)nullptr;
callback = [this](float f) {
unschedule("callback");
auto node = getChildByTag(1);
auto director = Director::getInstance();
director->getActionManager()->resumeTarget(node);};


schedule(/*CC_SCHEDULE_SELECTOR(PauseTest::unpause)*/callback, 3,"callback");




//4.StopActionTest
auto l = Label::createWithTTF("Should not crash", "Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition(visibleSize.width/2, visibleSize.height - 75);


auto pMove = MoveBy::create(2, Vec2(200, 0));
auto pCallback = CallFunc::create(/*CC_CALLBACK_0(StopActionTest::stopAction*/[this]() {
auto sprite = getChildByTag(2);
sprite->stopActionByTag(1);
});
auto ret = MoveBy::create(1, Vec2(-200, 0));
auto pSequence = Sequence::create(pMove, pCallback, ret, nullptr);
pSequence->setTag(1);


auto pChild = Sprite::create(s_pathGrossini);
pChild->setPosition(visibleSize/2);


addChild(pChild, 1, 2);
pChild->runAction(pSequence);



//5.StopAllActionsTest
auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition(Vec2(visibleSize.width/2,visibleSize.height - 75));


auto pMove1 = MoveBy::create(2, Vec2(200, 0));
auto pMove2 = MoveBy::create(2, Vec2(-200, 0));
auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
auto pRepeatMove = RepeatForever::create(pSequenceMove);
pRepeatMove->setTag(1);


auto pScale1 = ScaleBy::create(2, 1.5f);
auto pScale2 = ScaleBy::create(2, 1.0f / 1.5f);
auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
auto pRepeatScale = RepeatForever::create(pSequenceScale);
pRepeatScale->setTag(1);


auto pRotate = RotateBy::create(2, 360);
auto pRepeatRotate = RepeatForever::create(pRotate);


auto pChild = Sprite::create(s_pathGrossini);
pChild->setPosition(visibleSize/2);


addChild(pChild, 1, 2);
pChild->runAction(pRepeatMove);
pChild->runAction(pRepeatScale);
pChild->runAction(pRepeatRotate);
this->scheduleOnce(/*(SEL_SCHEDULE)&StopAllActionsTest::stopAction*/[this](float time) {
auto sprite = getChildByTag(2);
sprite->stopAllActionsByTag(1);
}, 4,"callback");




//6.ResumeTest
auto l = Label::createWithTTF("Grossini only rotate/scale in 3 seconds", "Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition(visibleSize.width/2, visibleSize.height - 75);


auto pGrossini = Sprite::create(s_pathGrossini);
addChild(pGrossini, 0, 1);
pGrossini->setPosition(visibleSize/2);


pGrossini->runAction(ScaleBy::create(2, 2));


auto director = Director::getInstance();
director->getActionManager()->pauseTarget(pGrossini);
pGrossini->runAction(RotateBy::create(2, 360));


auto callback = [this](float time) {
unschedule("callback");


auto pGrossini = getChildByTag(1);
auto director = Director::getInstance();
director->getActionManager()->resumeTarget(pGrossini);
};
this->schedule(/*CC_SCHEDULE_SELECTOR(ResumeTest::resumeGrossini)*/callback, 3.0f,"callback");


//7.StopActionsByFlagsTest

const unsigned int kMoveFlag = 0x01;
const unsigned int kScaleFlag = 0x02;
const unsigned int kRotateFlag = 0x04;
const unsigned int kRepeatForeverFlag = 0x08; // You don't need this for the test, but it's for demonstration how to activate several flags on an action.


auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition(Vec2(visibleSize.width/2, visibleSize.height - 75));


auto pMove1 = MoveBy::create(2, Vec2(200, 0));
auto pMove2 = MoveBy::create(2, Vec2(-200, 0));
auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
auto pRepeatMove = RepeatForever::create(pSequenceMove);
pRepeatMove->setFlags(kMoveFlag | kRepeatForeverFlag);


auto pScale1 = ScaleBy::create(2, 1.5f);
auto pScale2 = ScaleBy::create(2, 1.0f / 1.5f);
auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
auto pRepeatScale = RepeatForever::create(pSequenceScale);
pRepeatScale->setFlags(kScaleFlag | kRepeatForeverFlag);


auto pRotate = RotateBy::create(2, 360);
auto pRepeatRotate = RepeatForever::create(pRotate);
pRepeatRotate->setFlags(kRotateFlag | kRepeatForeverFlag);


auto pChild = Sprite::create(s_pathGrossini);
pChild->setPosition(visibleSize/2);


addChild(pChild, 1, 1);
pChild->runAction(pRepeatMove);
pChild->runAction(pRepeatScale);
pChild->runAction(pRepeatRotate);
this->scheduleOnce(/*(SEL_SCHEDULE)&StopActionsByFlagsTest::stopAction*/[this, kMoveFlag, kScaleFlag](float time) {
auto sprite = getChildByTag(1);
sprite->stopActionsByFlags(kMoveFlag | kScaleFlag);
}, 4,"callback");

//8. Issue14050Test
class SpriteIssue14050 : public Sprite
{
public:
SpriteIssue14050()
{
log("SpriteIssue14050::constructor");
}
virtual ~SpriteIssue14050()
{
log("SpriteIssue14050::destructor");
}
};

auto sprite = new (std::nothrow) SpriteIssue14050;
sprite->initWithFile("grossini.png");
sprite->autorelease();
addChild(sprite);

auto move = MoveBy::create(2, Vec2(100, 100));
auto rotate = RotateBy::create(2, 360);
sprite->runAction(move);
sprite->runAction(rotate);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值