Introduction to Actions in Cocos2d for iPhone game development

Source: Internet
Author: User

IPhone gamesUnder developmentCocos2dOfActionsThis topic describes the content of this article,ActionIt is like a command for a cocosNode object. These actions are usually used to change the attributes of an object, such as position, rotation, and scaling. If these attributes can only be modified for a period of time, they are called IntervalActionAction. Otherwise, they are called InstantAction actions.

For example, a MoveBy Action changes the position attribute within a period of time, that is, it is an IntervalAction Action.

 
 
  1. # Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds.  
  2. sprite runAction: [MoveBy actionWithDuration:2 position:ccp(50,10)]];  

IntervalAction has some interesting attributes.

They can be accelerated by time changes

 
 
  1. EaseIn   
  2. EaseOut   
  3. EaseInOut   
  4. Speed   
  5. Etc. (See the EaseActionsTest.m example for more info)  

All relative actions end with By) and some absolute actions end with To) have a flip action and execute an operation in the opposite direction. You can use pause/resume to stop and restore actions.

 
 
  1.  # Pause actions  
  2. [[ActionManager sharedManager ] pauseAllActionsForTarget:sprite ] ;  
  3.  
  4. # resume actions  
  5. [[ActionManager sharedManager ] resumeAllActionsForTarget:sprite ] ;  

Each of the following actions is extremely simple. I will add a simple example and describe what will happen. After all, it's hard to figure out what happened when moving objects and simple slices. Especially the jump function.

Simple Application: perform a mobile test on a box genie:

 
 
  1. -(Id) init {
  2. Self = [super init];
  3. If (nil! = Self ){
  4. IsTouchEnabled = YES;
  5. BoxSprite = [Sprite spriteWithFile: @ "box.png"];
  6. [BoxSprite setPosition: CGPointMake (25, 25)];
  7. [Self addChild: boxSprite];
  8. }
  9.  
  10. Return self;
  11. }
  12.  
  13. -(BOOL) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
  14. {
  15. UITouch * touch = [touches anyObject];
  16. CGPoint point = [touch locationInView: [touch view];
  17. // Action definition
  18. // Position
  19. // MoveBy
  20. Id moveByAction = [MoveBy actionWithDuration: 2 position: ccp (30, 50)];
  21. // Action execution
  22. [BoxSprite runAction: rotateByAction];
  23. Return YES;
  24. }

Basic Actions

Location

 
 
  1. MoveBy   
  2. MoveTo   
  3. JumpBy   
  4. JumpTo   
  5. BezierBy   
  6. Place  

Zoom in and out

 
 
  1. ScaleBy   
  2. ScaleTo  

Rotate

 
 
  1. RotateBy   
  2. RotateTo  

Display status

 
 
  1. Show   
  2. Hide   
  3. Blink   
  4. ToggleVisibility  

Transparency

 
 
  1. FadeIn   
  2. FadeOut   
  3. FadeTo   
  4. RGB  
  5. TintBy   
  6. TintTo  

Example

Some actions need to be implemented by yourself to know how the function means. For e-text APIs, it is not as smooth as the ordinary one. Most of them are less used in C. Some strangers.

 
 
  1. //MoveBy  
  2. d moveByAction = [MoveBy actionWithDuration:2 position:ccp(30,50)];  

For each execution, the corresponding genie positions x, y increase by 30, and 50. The time is within 2 seconds. The moving mode is slow.

 
 
  1. //MoveTo  
  2. id moveToAction = [MoveTo actionWithDuration:3 position:[[Director sharedDirector]convertCoordinate:point]];  

Each execution, the corresponding genie moves to the touch location, and moves in 3 seconds

 
 
  1. //JumpBy  
  2. d jumpByAction = [JumpBy actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];  

In each execution, 100,100 is relatively moved within 3 seconds. In the moving mode, 20 is used as the jump height, and 20 hops are made within 3 seconds.

 
 
  1. //JumpTo  
  2. d jumpToAction = [JumpTo actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];  

The usage method is the same as above. The difference is that

 
 
  1. //BezierBy  
  2. d bezierByAction = [BezierBy actionWithSize:2];   
  3. //ScaleBy  
  4. d scaleByAction = [ScaleBy actionWithDuration:3 scaleX:0.5 scaleY:0.5];  

After each execution, the genie gradually becomes half of the original length and width within 3 seconds.

 
 
  1. //ScaleTo  
  2. d scaleToAction = [ScaleTo actionWithDuration:3 scaleX:0.4 scaleY:0.5];   
  3. //RotateBy  
  4. d rotateByAction = [RotateBy actionWithDuration:3 angle:30.0];  

Within 3 seconds, gradually rotate to the right 30 degrees.

 
 
  1.      //RotateTo  
  2. id rotateToAction = [RotateTo actionWithDuration:3 angle:30.0];   
  3.  
  4. CGSize s = [[Director sharedDirector] winSize];  
  5.  
  6. id actionTo = [MoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];  
  7. id actionBy = [MoveBy actionWithDuration:2  position: ccp(80,80)];  
  8.  
  9. [sprite1 runAction: actionTo];  
  10. [sprite2 runAction:actionBy];  

Roll back Actions

It basically starts with "reverse. Is to implement the opposite Action of an Action.

 
 
  1. id move = [MoveBy actionWithDuration:2  position: ccp(80,80)];  
  2. id move_reverse = [move reverse]; 

The above move_reverse Action refers to moving the MoveBy Action to the ccp (-80,-80) position in 2 seconds.

Summary:IPhone gamesDevelopmentCocos2dMediumActionsThe introduction is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.