iPhone游戏开发中Cocos2d的Actions介绍是本文要介绍的内容,Action就好像给一个cocosNode对象的命令。这些动作通常用来改变物体的属性,例如位置,旋转,缩放等。如果这些属性在一段时间只能被修改的话,那么这中叫做 IntervalAction 的Action。否则,它们叫做InstantAction 的动作。
例如:MoveBy 动作,在一段时间之内,改变了位置这个属性 ,也就是说它是一个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)]];
- 1.
- 2.
IntervalAction 有一些很有趣的属性
它们可以通过时间改变来进行加速
EaseIn
EaseOut
EaseInOut
Speed
Etc. (See the EaseActionsTest.m example for more info)
- 1.
- 2.
- 3.
- 4.
- 5.
所有相对的动作(以By结尾的)和一些绝对的动作(以 To结尾的)都有一个翻转的动作,执行了一个相反方向的操作。 你可以使用pause/resume 来停止和恢复action
# Pause actions
[[ActionManager sharedManager ] pauseAllActionsForTarget:sprite ] ;
# resume actions
[[ActionManager sharedManager ] resumeAllActionsForTarget:sprite ] ;
- 1.
- 2.
- 3.
- 4.
- 5.
以下的每一个动作,除了极为简单的,我都会加入一个简单的事例,以及描述下将会发生的情况。毕竟,都是物体移动,简单上图片,很难表示清楚究竟发生了什么。尤其是那个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;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
基本的Actions
位置
MoveBy
MoveTo
JumpBy
JumpTo
BezierBy
Place
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
放大缩小
ScaleBy
ScaleTo
- 1.
- 2.
旋转
RotateBy
RotateTo
- 1.
- 2.
显示状态
Show
Hide
Blink
ToggleVisibility
- 1.
- 2.
- 3.
- 4.
透明度
FadeIn
FadeOut
FadeTo
RGB
TintBy
TintTo
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
例子
有些动作,还是需要自己实现了才知道函数是怎么个意思,对于e文的api,不如普通的那种顺利,大多都是些C#里面少使用的东西。有些陌生。
//MoveBy
d moveByAction = [MoveBy actionWithDuration:2 position:ccp(30,50)];
- 1.
- 2.
每次执行,相应精灵位置x,y增加30,和50,时间是2秒之内,移动方式,缓慢移动
//MoveTo
id moveToAction = [MoveTo actionWithDuration:3 position:[[Director sharedDirector]convertCoordinate:point]];
- 1.
- 2.
每次执行,相应精灵移动到触摸位置,3秒之内,移动过去
//JumpBy
d jumpByAction = [JumpBy actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];
- 1.
- 2.
每次执行,在3秒之内,相对移动100,100,移动方式,以20作为跳跃高度,3秒之内,20次跳跃
//JumpTo
d jumpToAction = [JumpTo actionWithDuration:3 position:ccp(100,100) height:20 jumps:20];
- 1.
- 2.
使用方式,同上。不同的是移动到100,100
//BezierBy
d bezierByAction = [BezierBy actionWithSize:2];
//ScaleBy
d scaleByAction = [ScaleBy actionWithDuration:3 scaleX:0.5 scaleY:0.5];
- 1.
- 2.
- 3.
- 4.
每次执行,3秒之内,精灵逐渐变为原来长宽的一半
//ScaleTo
d scaleToAction = [ScaleTo actionWithDuration:3 scaleX:0.4 scaleY:0.5];
//RotateBy
d rotateByAction = [RotateBy actionWithDuration:3 angle:30.0];
- 1.
- 2.
- 3.
- 4.
3秒之内,逐渐向右旋转30度。
//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];
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
回滚Actions
基本上都是以"reverse"开头的方法。就是实现某个Action的相反的动作。
id move = [MoveBy actionWithDuration:2 position: ccp(80,80)];
id move_reverse = [move reverse];
- 1.
- 2.
上面的move_reverse Action是指将MoveBy Action在2秒钟移动到ccp(-80,-80)的位置。
小结:iPhone游戏开发之Cocos2d中Actions介绍的内容介绍完了,希望本文对你有所帮助!