COCOS2D方法回调,块语句回调和反转动作和特殊动作
最后更新于:2022-04-01 20:15:10
1.今天学习了有关动作的回调的内容。
1)就方法回调进行说明:简单的说 就是使用了SEL 选择器方法
~~~
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
//获取当前屏幕的大小
CGSize size = [CCDirector sharedDirector].winSize;
// 创建第一个精灵,并将其添加为当前层的子节点
apple = [CCSprite spriteWithFile:@"apple.png"];
apple.position = ccp(50,100);
apple.opacity = 0;
[self addChild:apple];
// 创建第二个精灵,并将其添加为当前层的子节点
plane = [CCSprite spriteWithFile:@"plane.png"];
plane.position = ccp(size.width/2,size.height/2);
plane.opacity = 0;
[self addChild: plane];
// 创建第三个精灵,并将其添加为当前层的子节点
ball = [CCSprite spriteWithFile:@"ball.png"];
ball.position = ccp(250,100);
ball.opacity = 0;
[self addChild:ball];
// 创建第四个精灵,并将其添加为当前层的子节点
orange = [CCSprite spriteWithFile:@"orange.png"];
orange.position = ccp(250,300);
orange.opacity = 0;
[self addChild:orange];
// 创建第五个精灵,并将其添加为当前层的子节点
tomato = [CCSprite spriteWithFile:@"tomato.png"];
tomato.position = ccp(250,200);
tomato.opacity = 0;
[self addChild:tomato];
// 第一个精灵执行序列组合动作,其中最后一个动作调用了moveSprite方法
[apple runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallFunc actionWithTarget:self selector:@selector(moveSprite)],
nil]];
// 第二个精灵执行序列组合动作,其中最后一个动作调用了removeSprite:方法,并将当前的节点对象传递给指定的方法,注意冒号不能省略
[plane runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)],
nil]];
// 第三个精灵执行序列组合动作,其中最后一个动作调用了tintSprite:data:方法,并将当前的节点对象和数据传递给指定的方法,注意冒号和参数不能省略
[ball runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallFuncND actionWithTarget:self selector:@selector(tintSprite:data:)data:(void *)2],
nil]];
// 第四个精灵执行序列组合动作,其中最后一个动作调用了tintSprite:data:方法,并将当前的节点对象和数据传递给指定的方法,注意冒号和参数不能省略
[orange runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallFuncO actionWithTarget:self selector:@selector(rotateSprite:object:) object:tomato],
nil]];
}
return self;
}
~~~
2.块语句回调:一句话就是在代码中嵌入了 block语句
~~~
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// 创建第一个精灵,并将其添加为当前层的子节点
apple = [CCSprite spriteWithFile:@"apple.png"];
apple.position = ccp(50,100);
apple.opacity = 0;
[self addChild:apple];
// 创建第二个精灵,并将其添加为当前层的子节点
plane = [CCSprite spriteWithFile:@"plane.png"];
plane.position = ccp(size.width/2,size.height/2);
plane.opacity = 0;
[self addChild: plane];
// 创建第三个精灵,并将其添加为当前层的子节点
ball = [CCSprite spriteWithFile:@"ball.png"];
ball.position = ccp(250,100);
ball.opacity = 0;
[self addChild:ball];
// 创建第四个精灵,并将其添加为当前层的子节点
orange = [CCSprite spriteWithFile:@"orange.png"];
orange.position = ccp(250,300);
orange.opacity = 0;
[self addChild:orange];
[apple runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallBlock actionWithBlock:^{
[apple runAction:[CCMoveTo actionWithDuration:2 position:ccp(50, 200)]];
}], nil]];
[plane runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallBlockN actionWithBlock:^(CCNode *node){
CCNode *currentNode = node;
[self removeChild:currentNode cleanup:YES];
}], nil]];
[ball runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCSpawn actions:
[CCFadeIn actionWithDuration:1],
[CCScaleTo actionWithDuration:1 scale:1.5],
nil],
[CCDelayTime actionWithDuration:1],
[CCCallBlockO actionWithBlock:^(id object){
CCNode *currentNode = (CCNode *)object;
[currentNode runAction:[CCSequence actions:[CCFadeIn actionWithDuration:2],[CCRotateBy actionWithDuration:3 angle:180], nil]];
} object:orange], nil]];
}
return self;
}
~~~
3.反转动作:如果一系列动作有顺序,那么反转动作就是原来动作的逆向run
与 CCReverseTime 有关
4.特殊动作
CCActionTween 主要是修改一个对象的属性,比如说 scale,width等
id scaleA = 【CCScale actionWithDuration :2 scale:3】;
id scaleB = 【CCActionTween actionWithDuration:2 key: @“Scale” form 1:to:3】;
CCSpeed 可以修改一个飞机的速度。
';