官术网_书友最值得收藏!

Adding actions to sprites

We already saw Actions in action while animating and repeating the player animation. However, there are a lot more additional actions that you can perform. Further, you can play these actions together in a sequence.

Getting started

Let's first take a look at a simple action in which we will move the hero along the x axis by half the width of the screen, and move it down by one-quarter the height of the screen from the center in the y direction.

How to do it…

After we have added the hero to MainScene, we will add the following code:

CGPointfinalPos = CGPointMake(center.x + winSize.width/4, center.y - winSize.height/4);
CCActionFiniteTime* actionMove = [CCActionMoveToactionWithDuration:1.0position:finalPos];
[herorunAction:actionMove];

For convenience, I created a CGPoint called finalPos and stored the final position in it. Then, I created a variable called actionMove of the CCFiniteAction type, called the CCMoveTo function, gave it a duration of 1.0 seconds, and specified the position that I wanted to move the hero to. Finally, I called the runAction function on hero and passed in the action.

How it works…

When you run the project, the hero will be to the left of the yellow render sprite and will slowly start moving towards the lower-right corner if the render sprite is over a period of 1 second. After 1 second, when the destination location is reached, the action will stop, and the hero will be stationary again.

There's more…

Let's next create a more elaborate action and add a whole bunch of actions in a sequence and play them one after the other. Therefore, we will remove the previous code and add the following code instead:

//Actions

CGPointinitPos = hero.position;
CGPointfinalPos = CGPointMake(center.x + winSize.width/4, center.y - winSize.height/4);

CCActionFiniteTime* actionMove = [CCActionMoveToactionWithDuration:1.0position:finalPos];

CCAction *rotateBy = [CCActionRotateByactionWithDuration:2.0 angle: 180];

CCAction *tintTo=  [CCActionTintToactionWithDuration:1.0 color:[CCColorcolorWithRed:0.0fgreen:1.0blue:0.0]];

CCAction *delay = [CCActionDelayactionWithDuration:1.0];

CCAction *moveToInit = [CCActionMoveToactionWithDuration:1.0position:initPos];

CCAction *rotateBack = [CCActionRotateByactionWithDuration:2.0 angle: 180];

CCAction *tintBlue=  [CCActionTintToactionWithDuration:1.0 color:[CCColorcolorWithRed:0.0fgreen:0.0blue:1.0]];

CCAction *sequence = [CCActionSequenceactions:actionMove, rotateBy,tintTo, moveToInit, delay, rotateBack, tintBlue, nil];

[herorunAction:sequence];

Here, as I stored the final position in a variable, I will also store the initial position in a CGPoint variable called initPos, which we will use later.

The first action is the same moveTo action with which we will move the player.

Next, we will use the rotateBy action; here, we will give the duration and angle in degrees by which the object should be rotated.

After this, we will use the tintTo action, which will change the color of the object. Once again, we will give it the duration and color to which we want to the object to change. Here, we will change the color to green.

We will then call the delay action, which is used to perform an action after a delay. We will create a delay of 1 second.

Then, we will move the object back to its initial position, change the object color to blue, and rotate the object again by another 180 degrees.

We will create the CCSequence action and pass in all the actions in the order we want them to be played; at the end, we will add nil to say that is the end of the list.

At the end, we will call run the sequence action on hero.

Now, the hero will start at the initial position, but when he gets back, he will be blue in color.

The output of the code is as follows:

主站蜘蛛池模板: 芦溪县| 普宁市| 宝鸡市| 眉山市| 吉林市| 麻栗坡县| 铁岭县| 郁南县| 同江市| 昌图县| 阿拉善盟| 定安县| 茶陵县| 盐边县| 阿巴嘎旗| 滕州市| 新宁县| 抚宁县| 深圳市| 天柱县| 康保县| 漠河县| 玛曲县| 虎林市| 修武县| 始兴县| 太白县| 北宁市| 鹤山市| 台南市| 额敏县| 额尔古纳市| 彭州市| 镇远县| 凤庆县| 江门市| 十堰市| 揭阳市| 苗栗县| 延寿县| 阿鲁科尔沁旗|