最近也一直在忙,所以也只能每周的某一天抽出时间来分享一些知识点给童鞋们,希望童鞋们体谅下~
那么废话不多说了,本篇知识点两个:
1.利用ccspeed当精灵执行ccanimate动作途中设置其播放的速度;
2.设置游戏的速率,让你自由设置整个游戏的速度;
首先介绍第一个知识点:
对于第一个知识点,精灵执行ccanimate动作途中设置播放速度,说白一点就是当主角或者怪物播放一套帧动作(动画)的时候,可能突然受到其他因素影响希望主角或者怪物等动作放慢,也就是慢动作的感觉,那么这时候我们就需要设置动作的播放速度拉,也就是今天要介绍的ccspeed这个类;可能himi这里哇哇哇的说这么多还是没亭台明白吧…=。 = 那么下面我们来看看代码等就应该明白了;
直接上一段代码如下:
1
2
3
4
5
6
7
8
9
10
[[ccspriteframecache sharedspriteframecache]addspriteframeswithfile:@"animationsframes.plist"];
ccsprite*mysprite=[ccsprite spritewithspriteframename:@"himi1.png"];
mysprite.position=ccp(120,150);
[self addchild:mysprite];
ccanimation*anim=[ccanimation animationwithframe:@"himi" framecount:12 delay:0.1];
ccanimate* animate = [ccanimate actionwithanimation:anim];
ccsequence *seq = [ccsequence actions:animate,nil];
ccrepeatforever* repeat = [ccrepeatforever actionwithaction:seq];
[mysprite runaction:repeat];
以上代码创建一个帧动画(帧资源都在animationframes.plist加载到内存中了),然后创建一个精灵并让其永久循环执行这个帧动画;
童鞋们想一想,如果在这个永久动作执行后,你想在一个任意时间设置这个动画播放的速度,那么就利用ccspeed来实现了,代码如下:
11
12
[[ccspriteframecache sharedspriteframecache]addspriteframeswithfile:@"animationsframes.plist"];
//左侧正常速度的播放
ccsprite*mysprite=[ccsprite spritewithspriteframename:@"himi1.png"];
mysprite.position=ccp(120,150);
[self addchild:mysprite];
ccanimation*anim=[ccanimation animationwithframe:@"himi" framecount:12 delay:0.1];
ccanimate* animate = [ccanimate actionwithanimation:anim];
ccsequence *seq = [ccsequence actions:animate,nil];
//让你的永久动作放入speed中
ccspeed *speed =[ccspeed actionwithaction:[ccrepeatforever actionwithaction:seq] speed:1.0f];
[speed settag:888];//设置tag能任意获取到其实例,并且对其进行操作
[mysprite runaction:speed];
这段代码和第一段代码不同点就是第二段将ccrepeatforever永久动作又包装到了ccspeed中,整个动作等同与交给了ccspeed来控制了,那么下面我还设置了[speed settag:888];这个是留出接口,当你需要设置整个ccspeed包装的动作速度的时候利用tag获取到,这个大家肯定很熟悉,那么获取动作方式如下:
ccspeed *speed=(ccspeed*)[sprite getactionbytag:88];
获取的时候是你之前runaction的精灵来利用getactionbytag来获取的!
那么下面继续添加代码,我们让一个由ccspeed包装一个帧动画并让精灵执行后的5秒后让其速度变成原有播放速度的一半,代码如下:
ccsprite *myspritebyf =[ccsprite spritewithspriteframename:@"himi1.png"];
myspritebyf.position=ccp(360,150);
[self addchild:myspritebyf z:0 tag:66];
anim=[ccanimation animationwithframe:@"himi" framecount:12 delay:0.1];
animate = [ccanimate actionwithanimation:anim];
seq =[ccsequence actions:animate, nil];
[speed settag:88];
[myspritebyf runaction:speed];
[self schedule:@selector(slowforhimi) interval:5];
-(void)slowforhimi{
[self unschedule:@selector(slowforhimi)];//解除此选择器
ccsprite*sprite=(ccsprite*)[self getchildbytag:66];
ccspeed *speed=(ccspeed*)[sprite getactionbytag:88];
[speed setspeed:0.5];//放慢原有速度的0.5倍
}
ccspeed的创建很简单,那么设置速率的方法如下:
[ccspeed* setspeed:xx];
这里的xx参数指的是倍率,传入1表示原速,大于1表示增快,小于1表示放慢速度~
下面直接给出全部测试项目代码:
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// helloworldlayer.m
// slowanimationbyhimi
// created by 华明 李 on 11-11-21.
// copyright himi 2011年. all rights reserved.
//
// import the interfaces
#import "helloworldlayer.h"
#import "ccanimationhelper.h"
// helloworldlayer implementation
@implementation helloworldlayer
+(ccscene *) scene
{
// 'scene' is an autorelease object.
ccscene *scene = [ccscene node];
// 'layer' is an autorelease object.
helloworldlayer *layer = [helloworldlayer node];
// add layer as a child to scene
[scene addchild: layer];
// return the scene
return scene;
//ccjumpto实现,抛物线
// on "init" you need to initialize your instance
-(id) init{
if( (self=[super init])) {
cclabelttf *label = [cclabelttf labelwithstring:@"暂缓动作&设置整个游戏加速/减速" fontname:@"marker felt" fontsize:24];
label.position = ccp(260,260);
[self addchild: label z:0 ];
label = [cclabelttf labelwithstring:@"正常速度的播放" fontname:@"marker felt" fontsize:12];
label.position = ccp(120,220);
[self addchild: label z:0 tag:99];
label = [cclabelttf labelwithstring:@"左侧动态放慢的速度的动作" fontname:@"marker felt" fontsize:12];
label.position = ccp(350,220);
[self addchild: label z:0 ];
[[ccspriteframecache sharedspriteframecache]addspriteframeswithfile:@"animationsframes.plist"];
//左侧正常速度的播放
[mysprite runaction:repeat];
//左侧动态放慢的速度的动作
ccsprite *myspritebyf =[ccsprite spritewithspriteframename:@"himi1.png"];
myspritebyf.position=ccp(360,150);
[self addchild:myspritebyf z:0 tag:66];
anim=[ccanimation animationwithframe:@"himi" framecount:12 delay:0.1];
animate = [ccanimate actionwithanimation:anim];
seq =[ccsequence actions:animate, nil];
ccspeed *speed =[ccspeed actionwithaction:[ccrepeatforever actionwithaction:seq] speed:1.0f];
[speed settag:88];
[myspritebyf runaction:speed];
[self schedule:@selector(slowforhimi) interval:5];
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (label)
// don't forget to call "super dealloc"
[super dealloc];
@end
运行截图如下: (图片中有个”左”写错了,应该是”右”,懒得改了,大家知道就行了 娃哈哈) :tx:
从截图中可能童鞋们看不出什么效果。等文章最后放出源码,大家运行就可以看到效果了-。 –
himi当时做的时候因为用ccspeed方式有问题一直不行,就去改了源码弄的。后来才发现ccspeed正确用法,我去了=。 =
这里himi必须强调一点!!!!!!!
很多时候你的主角的动作利用ccaction来实现,移动则是在update刷帧函数或者一些选择器的方法中进行的,那么为了让你的主角慢动作比较逼真,那么himi建议不要使用scheduleupdate函数,因为这个你无法修改每次调用update的时间默认都是每帧都调用,那么你应该自己定义一个选择器当刷逻辑的函数,这样就能配合ccspeed实现逼真慢动作拉~
下面我们来介绍第二个知识点:设置游戏速度
对于游戏速度最常见的出现在塔防游戏中,当玩家创建好防守的东东后开始出怪后,可能怪物移动速度慢,而玩家着急看到结果,那么我们就会人性化的加上加快游戏速度的按钮拉~那么这个功能在cocos2d引擎中封装好的,一句代码即可完成,如下代码即可:
[[ccscheduler sharedscheduler] settimescale:xx];
这里的xx仍然是倍率:传入1表示原速,大于1表示增快,小于1表示放慢速度~
ok,本篇就到此~