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.
- # Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds.
- sprite runAction: [MoveBy actionWithDuration:2 position:ccp(50,10)]];
IntervalAction has some interesting attributes.
They can be accelerated by time changes
- EaseIn
- EaseOut
- EaseInOut
- Speed
- 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.
- # Pause actions
- [[ActionManager sharedManager ] pauseAllActionsForTarget:sprite ] ;
-
- # resume actions
- [[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:
- -(Id) init {
- Self = [super init];
- If (nil! = Self ){
- IsTouchEnabled = YES;
- BoxSprite = [Sprite spriteWithFile: @ "box.png"];
- [BoxSprite setPosition: CGPointMake (25, 25)];
- [Self addChild: boxSprite];
- }
-
- Return self;
- }
-
- -(BOOL) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
- {
- UITouch * touch = [touches anyObject];
- CGPoint point = [touch locationInView: [touch view];
- // Action definition
- // Position
- // MoveBy
- Id moveByAction = [MoveBy actionWithDuration: 2 position: ccp (30, 50)];
- // Action execution
- [BoxSprite runAction: rotateByAction];
- Return YES;
- }
Basic Actions
Location
- MoveBy
- MoveTo
- JumpBy
- JumpTo
- BezierBy
- Place
Zoom in and out
- ScaleBy
- ScaleTo
Rotate
- RotateBy
- RotateTo
Display status
- Show
- Hide
- Blink
- ToggleVisibility
Transparency
- FadeIn
- FadeOut
- FadeTo
- RGB
- TintBy
- 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.
- //MoveBy
- 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.
- //MoveTo
- 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
- //JumpBy
- 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.
- //JumpTo
- 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
- //BezierBy
- d bezierByAction = [BezierBy actionWithSize:2];
- //ScaleBy
- 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.
- //ScaleTo
- d scaleToAction = [ScaleTo actionWithDuration:3 scaleX:0.4 scaleY:0.5];
- //RotateBy
- d rotateByAction = [RotateBy actionWithDuration:3 angle:30.0];
Within 3 seconds, gradually rotate to the right 30 degrees.
- //RotateTo
- id rotateToAction = [RotateTo actionWithDuration:3 angle:30.0];
-
- CGSize s = [[Director sharedDirector] winSize];
-
- id actionTo = [MoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];
- id actionBy = [MoveBy actionWithDuration:2 position: ccp(80,80)];
-
- [sprite1 runAction: actionTo];
- [sprite2 runAction:actionBy];
Roll back Actions
It basically starts with "reverse. Is to implement the opposite Action of an Action.
- id move = [MoveBy actionWithDuration:2 position: ccp(80,80)];
- 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!