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

来源:互联网 发布:西部数码域名管理 编辑:程序博客网 时间:2024/06/05 00:36
void HelloWorld::onEnter(){Scene::onEnter();Size visibleSize = Director::getInstance()->getVisibleSize();char*s_pathGrossini = "grossini.png";//1.CrashTestauto 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.LogicTestauto 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 workingnode->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.StopActionTestauto 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.StopAllActionsTestauto 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.ResumeTestauto 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.StopActionsByFlagsTestconst 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. Issue14050Testclass 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);}

0 0