Five hairs cocos2d-x learning notes 08-animation, cocos2d-x08-
An example is enough. Click the text label to execute the animation. I am also a little white. I encountered a problem when I wrote this demo. I clicked the text tag and the game died. Today, I did not go to bed for a night to solve this problem. I went to the study group and asked the experts to solve the problem. It turns out to be the relationship between the Animation and the Animation lifecycle. Write down it first.
1 bool HelloWorld: init () 2 {3 // 4 // 1. super init first 5 if (! Layer: init () 6 {7 return false; 8} 9 10 SpriteFrameCache * cache = SpriteFrameCache: getInstance (); 11 cache-> addSpriteFramesWithFile ("a6.plist "); 12 13 Vector <SpriteFrame *> vec; 14 char name [15]; 15 memset (name, 0, 15); 16 17 for (int I = 1; I <= 7; I ++) {18 sprintf (name, "a6_10902d.png", I); 19 vec. pushBack (cache-> getSpriteFrameByName (name); 20} 21 22 Animation * animation = Animation: createWithSpriteFrames (vec, 0.1f, 1); 23 24 Animate * animate = Animate :: create (animation); 25 26 auto sprite = Sprite: create (); 27 addChild (sprite); 28 sprite-> setPosition (Vec2 (200,200 )); 29 // sprite-> runAction (RepeatForever: create (animate); 30 31 auto label = LabelTTF: create ("Touch", "Courier", 30 ); 32 label-> setPosition (Vec2 (500,500); 33 addChild (label); 34 35 int I = 10; 36 37 EventListenerTouchOneByOne * listener = EventListenerTouchOneByOne: create (); 38 listener-> onTouchBegan = [=] (Touch * t, Event * e) {39 if (label-> getBoundingBox (). containsPoint (t-> getLocation () {40 // notification: pay attention to the life cycle of Animation and Animate41 Animation * animation = Animation: createWithSpriteFrames (vec, 0.1f, 1); 42 Animate * animate = Animate: create (animation); 43 sprite-> runAction (animate); 44 log ("I = % d", I ); 45 return true; 46} 47 return false; 48}; 49 // condition: here is "this" not "label" because if here is "label ", touch * t equals to "label" 50 ctor: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this); 51 return true; 52}Init
Running effect: