Action就好像給一個cocosNode對象的命令。這些動作通常用來改變物體的屬性,例如位置,旋轉,縮放等。如果這些屬性在一段時間只能被修改的話,那麼這中叫做 IntervalAction 的Action。否則,它們叫做InstantAction
的動作。
例如:MoveBy 動作,在一段時間之內,改變了位置這個屬性 ,也就是說它是一個IntervalAction的Action。
Example:
# 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
有一些很有趣的屬性
-
它們可以通過時間改變來進行加速
-
EaseIn
-
EaseOut
-
EaseInOut
-
Speed
-
Etc. (See the
EaseActionsTest.m
example for more info)
-
-
所有相對的動作(以By結尾的)和一些絕對的動作(以 To結尾的)都有一個翻轉的動作,執行了一個相反方向的操作。
(
[action reverse]
)
你可以使用pause/resume 來停止和恢復action
# Pause actions [[ActionManager sharedManager ] pauseAllActionsForTarget:sprite ] ; # resume actions [[ActionManager sharedManager ] resumeAllActionsForTarget:sprite ] ;
以下的每一個動作,除了極爲簡單的,我都會加入一個簡單的事例,以及描述下將會發生的情況。畢竟,都是物體移動,簡單上圖片,很難表示清楚究竟發生了什麼。尤其是那個jump函數。。動作好多。。呵呵。
簡單應用,對一個box精靈進行移動測試:
-(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]];
//動作的定義
//position
//MoveBy
id moveByAction = [MoveBy actionWithDuration:2 position:ccp(30,50)];
//動作的執行
[boxSprite runAction:rotateByAction];
return YES;
}
有些動作,還是需要自己實現了才知道函數是怎麼個意思,對於e文的api,不如普通的那種順利,大多都是些C#裏面少使用的東西。有些陌生。
-
position
-
//MoveBy
id moveByAction = [MoveBy actionWithDuration:2 position:ccp(30,50)];
每次執行,相應精靈位置x,y增加30,和50,時間是2秒之內,移動方式,緩慢移動
-
//MoveTo
id moveToAction = [MoveTo actionWithDuration:3 position:[[Director sharedDirector]convertCoordinate:point]];
每次執行,相應精靈移動到觸摸位置,3秒之內,移動過去
-
//JumpBy
id jumpByAction = [JumpBy actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];
每次執行,在3秒之內,相對移動100,100,移動方式,以20作爲跳躍高度,3秒之內,20次跳躍
-
//JumpTo
id jumpToAction = [JumpTo actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];
使用方式,同上。不同的是移動到100,100
-
BezierBy
//BezierBy
//id bezierByAction = [BezierBy actionWithSize:2];
這個貌似還沒有實現,不知道要幹什麼。
-
//Place
id placeAction = [Place actionWithPosition:[[Director sharedDirector]convertCoordinate:point]];
移動到直接位置,沒有時間概念。
本人認爲應用方式貌似和以下這個一致。
//MoveTo
id moveToAction = [MoveTo actionWithDuration:0 position:[[Director sharedDirector]convertCoordinate:point]];
-
-
scale
-
//ScaleBy
id scaleByAction = [ScaleBy actionWithDuration:3 scaleX:0.5 scaleY:0.5];
每次執行,3秒之內,精靈逐漸變爲原來長寬的一半
-
//ScaleTo
id scaleToAction = [ScaleTo actionWithDuration:3 scaleX:0.4 scaleY:0.5];
原理同上。
-
-
rotation
-
//RotateBy
id rotateByAction = [RotateBy actionWithDuration:3 angle:30.0];
3秒之內,逐漸向右旋轉30度。
-
//RotateTo
id rotateToAction = [RotateTo actionWithDuration:3 angle:30.0];
下面三個不再一一舉例了,無非就是對物體,可見性,透明度,以及上色的操作。
記住上面這一點。有相對的動作(以By結尾的)和一些絕對的動作(以 To結尾的)。。記住相對和絕對就行了!
-
-
visible
-
opacity
-
r, g, b
例子:
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];
Almost all actions have the reverse
method implemented. Basically it creates a new action with the reverse behavior.
Example:
id move = [MoveBy actionWithDuration:2 position: ccp(80,80)]; id move_reverse = [move reverse];
The move_reverse
action will be a MoveBy
action of duration 2, but with the position value of ccp(-80,-80)
.
Attetion plz:
本系列文章原形來自於cocos2d的官方wiki,本人進行翻譯整理,並加入個人心得,及其很多額外代碼補充實現。
原文網址:http://www.cnblogs.com/AlexLiu/archive/2010/01/24/1655222.html