COCOS2D-X action

The difference between By and To

By is the current position relative to the node object, and To is the absolute position.

Basic actions

Move

Use MoveTo and MoveBy to complete the movement of the node object after a set time.

Rotate

Use RotateTo and RotateBy to complete the node object to rotate the specified angle clockwise after a set time.

Zoom

Use ScaleBy and ScaleTo to complete the scaling of node objects.

Fade in and fade out

FadeIn modifies the transparency property of the node object from completely transparent to completely opaque. FadeOut is the opposite.

Color mixing

auto mySprite = Sprite::create("mysprite.png");

// Tints a node to the specified RGB values< br />auto tintTo = TintTo::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintTo);

// Tints a node BY the delta of the specified RGB values.
auto tintBy = TintBy::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintBy);

< h4>Frame animation

Using the Animate object can easily achieve a page turning effect by replacing images at short intervals.

auto mySprite = Sprite::create("mysprite.png");

// now lets animate the sprite we moved
Vector animFrames;
animFrames.reserve(12);
animFrames.pushBack(SpriteFrame::create("Blue_Front1.png", Rect(0,0,65,81)));
animFrames.pushBack (SpriteFrame::create("Blue_Front2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Front3.png", Rect(0,0, 65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame:: create("Blue_Left2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left3.png", Rect(0,0,65,81) ));
animFrames.pushBack(SpriteFrame::create("Blue_Back1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back2 .png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back3.png", Rect(0,0,65,81)));< br />animFrames.pushBack(SpriteFrame::create("Blue_Right1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right3.png" , Rect(0,0,65,81)));

// create the animation out of the frames
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
Animate* animate = Animate::create(animation);

// run it and repeat it forever
mySprite->runAction(RepeatForever::create(animate));

Variable motion

Variable motion can make the node object have acceleration, produce smooth and relatively complex motions, which can be used to imitate some physical motions, which is more expensive than the actual use of the physics engine. Low and easy to use. The following example is a sprite falling from the top of the screen and then beating continuously.

// create a sprite
auto mySprite = Sprite::create("mysprite.png");

// create a MoveBy Action to where we want the sprite to drop from.
auto move = MoveBy::create(2, Vec2(200, dirs->getVisibleSize().height -
newSprite2->getContentSize().height));
auto move_back = move->reverse();

// create a BounceIn Ease Action
auto move_ease_in = EaseBounceIn::create(move->clone() );

// create a delay that is run in between sequence events
auto delay = DelayTime::create(0.25f);

// create the sequence of actions, in the order we want to run them
auto seq1 = Sequence::create(move_ease_in, delay, move_ease_in_back,
delay->clone(), nullptr);

// run the sequence and repeat forever.
mySprite->runAction(RepeatForever::create(seq1));

Sequence

Allows a method to be added to the CallFunc object, and then CallFunc Add to Sequence so that method calls can be triggered when the sequence is executed.

auto mySprite = Sprite::create("mysprite.png");

// create a few actions.
auto jump = JumpBy::create(0.5 , Vec2(0, 0), 100, 1);

auto rotate = RotateTo::create(2.0f, 10);

// create a few callbacks< br />auto callbackJump = CallFunc::create([](){
log("Jumped!");
});

auto callbackRotate = CallFunc::create ([](){
log("Rotated!");
});

// create a sequence with the actions and callbacks
auto seq = Sequence::create(jump, callbackJump, rotate, callbackRotate, nullptr);

// run it
mySprite->runAction(seq);

Spawn

It is very similar to Sequence. The difference is that Spawn executes all actions at the same time. The Spawn object can add any number of actions and other Spawn objects.

Clone of the action

MoveBy::create(10, Vec2(400,100));

Your heroSprite is in 10s, from (0 ,0) is moved to (400,100), heroSprite has a new position (400,100), and more importantly, the action object also has an internal state related to the position of the node. Now suppose you have an internal state related to the coordinate position. Now if you have an emenySprite with a coordinate position of (200,200), it will move to the coordinate position of (800,200), which is not the result you expect. Because when this action is applied for the second time, it already has an internal state. Use clone() to avoid this situation, and clone to obtain a new action object.

// create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");

// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));

// run it on our hero
heroSprite->runAction(moveBy);

// run it on our enemy
enemySprite->runAction(moveBy); // oops, this will not be unique!
// uses the Actions current internal state as a starting point.
/ create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");

// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));

// run it on our hero
heroSprite->runAction(moveBy);

// run it on our enemy
enemySprite->runAction(moveBy-> clone()); // correct! This will be unique

Reverse action

The function of reverse (Reverse) is also the same as the literal meaning. Calling reverse() can make a series of The action is executed in the opposite direction.

// reverse a sequence, spawn or action
mySprite->runAction(mySpawn->reverse());

The difference between By and To

By is the current position relative to the node object, and To is the absolute position.

Basic actions

Move

Use MoveTo and MoveBy to complete the movement of the node object after a set time.

Rotate

Use RotateTo and RotateBy to complete the node object to rotate the specified angle clockwise after a set time.

Zoom

Use ScaleBy and ScaleTo to complete the scaling of node objects.

Fade in and fade out

FadeIn modifies the transparency property of the node object from completely transparent to completely opaque. FadeOut is the opposite.

Color mixing

auto mySprite = Sprite::create("mysprite.png");

// Tints a node to the specified RGB values< br />auto tintTo = TintTo::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintTo);

// Tints a node BY the delta of the specified RGB values.
auto tintBy = TintBy::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintBy);

< h4>Frame animation

Using the Animate object, you can easily achieve a page turning effect by replacing images at short intervals.

auto mySprite = Sprite::create("mysprite.png");

// now lets animate the sprite we moved
Vector animFrames;
animFrames.reserve(12);
animFrames.pushBack(SpriteFrame::create("Blue_Front1.png", Rect(0,0,65,81)));
animFrames.pushBack (SpriteFrame::create("Blue_Front2.png", Rect(0,0,65,81))));
animFrames.pushBack(SpriteFrame::create("Blue_Front3.png", Rect(0,0, 65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame:: create("Blue_Left2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left3.png", Rect(0,0,65,81) ));
animFrames.pushBack(SpriteFrame::create("Blue_Back1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back2 .png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back3.png", Rect(0,0,65,81)));< br />animFrames.pushBack(SpriteFrame::create("Blue_Right1.png", Rect(0,0,65,81)));< br />animFrames.pushBack(SpriteFrame::create("Blue_Right2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right3.png", Rect(0,0,65,81)));

// create the animation out of the frames
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);< br />Animate* animate = Animate::create(animation);

// run it and repeat it forever
mySprite->runAction(RepeatForever::create(animate));< /pre>

Variable motion

Variable motion can make the node object have acceleration, produce smooth and relatively complex motions, which can be used to imitate some physical motions, which is less expensive than the actual use of the physics engine. , It's easy to use. The following example is a sprite falling from the top of the screen and then beating continuously.

// create a sprite
auto mySprite = Sprite::create("mysprite.png");

// create a MoveBy Action to where we want the sprite to drop from.
auto move = MoveBy::create(2, Vec2(200, dirs->getVisibleSize().height -
newSprite2->getContentSize().height));
auto move_back = move->reverse();

// create a BounceIn Ease Action
auto move_ease_in = EaseBounceIn::create(move->clone() );

// create a delay that is run in between sequence events
auto delay = DelayTime::create(0.25f);

// create the sequence of actions, in the order we want to run them
auto seq1 = Sequence::create(move_ease_in, delay, move_ease_in_back,
delay->clone(), nullptr);

// run the sequence and repeat forever.
mySprite->runAction(RepeatForever::create(seq1));

Sequence

Allows a method to be added to the CallFunc object, and then CallFunc Add to Sequence so that method calls can be triggered when the sequence is executed.

auto mySprite = Sprite::create("mysprite.png");

// create a few actions.
auto jump = JumpBy::create(0.5 , Vec2(0, 0), 100, 1);

auto rotate = RotateTo::create(2.0f, 10);

// create a few callbacks< br />auto callbackJump = CallFunc::create([](){
log("Jumped!");
});

auto callbackRotate = CallFunc::create ([](){
log("Rotated!");
});

// create a sequence with the actions and callbacks
auto seq = Sequence::create(jump, callbackJump, rotate, callbackRotate, nullptr);

// run it
mySprite->runAction(seq);

Spawn

It is very similar to Sequence. The difference is that Spawn executes all actions at the same time. The Spawn object can add any number of actions and other Spawn objects.

Clone of action

MoveBy::create(10, Vec2(400,100));

Your heroSprite is in 10s, from (0 ,0) has moved to (400,100), heroSprite has a new position (400,100), and more importantly, the action object also has an internal state related to the position of the node. Now suppose you have an internal state related to the coordinate position. Now if you have an emenySprite with a coordinate position of (200,200), it will move to the coordinate position of (800,200), which is not the result you expect. Because when this action is applied for the second time, it already has an internal state. Use clone() to avoid this situation and clone to obtain a new action object.

// create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");

// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));

// run it on our hero
heroSprite->runAction(moveBy);

// run it on our enemy
enemySprite->runAction(moveBy); // oops, this will not be unique!
// uses the Actions current internal state as a starting point.
/ create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");

// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));

// run it on our hero
heroSprite->runAction(moveBy);

// run it on our enemy
enemySprite->runAction(moveBy-> clone()); // correct! This will be unique

Reverse action

The function of reverse (Reverse) is also the same as the literal meaning. Calling reverse() can make a series of The action is executed in the opposite direction.

// reverse a sequence, spawn or action
mySprite->runAction(mySpawn->reverse());

Leave a Comment

Your email address will not be published.