动作主要分为延时动作和瞬时动作,除了动作还有联合动作和动画等一系列的操作。
瞬时动作:
延时动作:
联合动作:
动画:
对于动作的结束监听主要通过回调函数CallFunc,它可以被看成是一个Action,因此可以被加在一个执行的动作序列中间。
C++11可以使用Lambda表达式和bind来编写函数,因此使得回到函数的功用更加灵活和丰富。
1 bool ActionScene::init(){ 2 if (!Layer::init()){ 3 return false; 4 } 5 6 auto visibleSize = Director::getInstance()->getVisibleSize(); 7 auto sprite = Sprite::create("sprite.png"); 8 sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); 9 this->addChild(sprite); 10 11 auto actionFlipX = FlipX::create(true); 12 auto actionFlipY = FlipY::create(true); 13 14 auto actionHide = Hide::create(); 15 auto actionShow = Show::create(); 16 17 auto actionMove1 = MoveTo::create(0.9f,Vec2(200,100)); 18 auto actionMove2 = MoveBy::create(0.9f, Vec2(200, 100)); 19 20 auto actionScale1 = ScaleTo::create(2.8f, 0.4f, 1.0f); 21 auto actionScale2 = ScaleBy::create(1.5f, 0.5, 1.0f); 22 23 auto actionRotate1 = RotateTo::create(2.0f, 40.0f); 24 auto actionRotate2 = RotateBy::create(2.0f, 90.0f); 25 26 auto actionFadeIn = FadeIn::create(2.0f); 27 auto actionFadeOut = FadeOut::create(2.0f); 28 29 auto actionTint1 = TintTo::create(2.0f, Color3B(0, 255.0f, 0)); 30 31 auto actionJump1 = JumpTo::create(2.0f, Vec2(200, 100), 50, 10); 32 auto actionJump2 = JumpBy::create(2.0f, Vec2(200, 100), 50, 10); 33 34 auto actionSkew1 = SkewTo::create(2.0f, 30, 0); 35 auto actionSkew2 = SkewBy::create(2.0f, 0, 30); 36 37 auto actionBlink1 = Blink::create(2.0f, 5); 38 39 /*曲线运动*/ 40 auto aarray = PointArray::create(20); 41 aarray->addControlPoint(Vec2(100, 0)); 42 aarray->addControlPoint(Vec2(200, 250)); 43 auto actionCardingSpline1 = CardinalSplineTo::create(2.0f, aarray, 10); 44 auto actionCardingSpline2 = CardinalSplineBy::create(2.0f, aarray, 10); 45 46 /*贝塞尔曲线运动*/ 47 ccBezierConfig config_bezier; 48 config_bezier.controlPoint_1 = Vec2(100, 0); 49 config_bezier.controlPoint_2 = Vec2(200, 250); 50 config_bezier.endPosition = Vec2(300, 50); 51 auto actionBezier1 = BezierTo::create(2.0f, config_bezier); 52 auto actionBezier2 = BezierBy::create(2.0f, config_bezier); 53 54 auto actionSequence1 = Sequence::create(actionScale1, actionSkew1, actionTint1, nullptr); 55 auto actionSpawn1 = Spawn::create(actionSkew2, actionScale2, actionMove2, nullptr); 56 auto actionRepeat1 = Repeat::create(actionJump2, 6); 57 auto actionRepeatForever1 = RepeatForever::create(actionJump1); 58 59 auto actionDelayTime1 = DelayTime::create(2); 60 auto actionSequence2 = Sequence::create(actionScale1, actionDelayTime1, actionTint1, nullptr); 61 62 /*动画播放*/ 63 Vector<SpriteFrame *> animFrames; 64 animFrames.pushBack(SpriteFrame::create("sprite.png", Rect(0, 0, 65, 81))); 65 animFrames.pushBack(SpriteFrame::create("sprite2.png", Rect(0, 0, 65, 81))); 66 animFrames.pushBack(SpriteFrame::create("sprite3.png", Rect(0, 0, 65, 81))); 67 auto animation = Animation::createWithSpriteFrames(animFrames, 2.0f); 68 auto animate = Animate::create(animation); 69 70 /*时间曲线*/ 71 auto actionEasy1 = EaseBounceIn::create(actionJump1); 72 73 /*演示动作完成的回调函数*/ 74 auto callFunc = CallFunc::create([](){ 75 CCLOG("World"); 76 }); 77 auto actionSequence3 = Sequence::create(actionScale1, callFunc, nullptr); 78 79 80 sprite->runAction(actionSequence3); 81 82 return true; 83 }
以上。
时间: 2024-12-27 23:43:31