【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
2011-11-27 18:31
721 查看
Himi
原创, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址:/article/1362545.html
最近也一直在忙,所以也只能每周的某一天抽出时间来分享一些知识点给童鞋们,希望童鞋们体谅下~
那么废话不多说了,本篇知识点两个:
1.利用CCSpeed当精灵执行CCAnimate动作途中设置其播放的速度;
2.设置游戏的速率,让你自由设置整个游戏的速度;
首先介绍第一个知识点:
对于第一个知识点,精灵执行CCAnimate动作途中设置播放速度,说白一点就是当主角或者怪物播放一套帧动作(动画)的时候,可能突然受到其他因素影响希望主角或者怪物等动作放慢,也就是慢动作的感觉,那么这时候我们就需要设置动作的播放速度拉,也就是今天要介绍的CCSpeed这个类;可能Himi这里哇哇哇的说这么多还是没亭台明白吧...=。 = 那么下面我们来看看代码等就应该明白了;
至于精灵如何利用CCAnimate实现帧集合动画教程在之前已经讲述过,那么这里就不在赘述,如果还不清楚如何利用很多帧形成动画让精灵播放的童鞋请移步到:【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!
直接上一段代码如下:
view plaincopy to clipboardprint?
[[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];
这段代码和第一段代码不同点就是第二段将CCRepeatForever永久动作又包装到了CCSpeed中,整个动作等同与交给了CCSpeed来控制了,那么下面我还设置了[speed setTag:888];这个是留出接口,当你需要设置整个CCSpeed包装的动作速度的时候利用tag获取到,这个大家肯定很熟悉,那么获取动作方式如下:
view plaincopy to clipboardprint?
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
view plaincopy to clipboardprint?
-(void)slowForHimi{
[self unschedule:@selector(slowForHimi)];//解除此选择器
CCSprite*sprite=(CCSprite*)[self getChildByTag:66];
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
[speed setSpeed:0.5];//放慢原有速度的0.5倍
}
这里的XX参数指的是倍率,传入1表示原速,大于1表示增快,小于1表示放慢速度~
下面直接给出全部测试项目代码:
view plaincopy to clipboardprint?
//
// 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"];
//左侧正常速度的播放
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];
//左侧动态放慢的速度的动作
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;
}
-(void)slowForHimi{
[self unschedule:@selector(slowForHimi)];//解除此选择器
CCSprite*sprite=(CCSprite*)[self getChildByTag:66];
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
[speed setSpeed:0.5];//放慢原有速度的0.5倍
}
// 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
这里的XX仍然是倍率:传入1表示原速,大于1表示增快,小于1表示放慢速度~
OK,本篇就到此~
源码下载地址:http://download.csdn.net/detail/xiaominghimi/3839063
原创, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址:/article/1362545.html
最近也一直在忙,所以也只能每周的某一天抽出时间来分享一些知识点给童鞋们,希望童鞋们体谅下~
那么废话不多说了,本篇知识点两个:
1.利用CCSpeed当精灵执行CCAnimate动作途中设置其播放的速度;
2.设置游戏的速率,让你自由设置整个游戏的速度;
首先介绍第一个知识点:
对于第一个知识点,精灵执行CCAnimate动作途中设置播放速度,说白一点就是当主角或者怪物播放一套帧动作(动画)的时候,可能突然受到其他因素影响希望主角或者怪物等动作放慢,也就是慢动作的感觉,那么这时候我们就需要设置动作的播放速度拉,也就是今天要介绍的CCSpeed这个类;可能Himi这里哇哇哇的说这么多还是没亭台明白吧...=。 = 那么下面我们来看看代码等就应该明白了;
至于精灵如何利用CCAnimate实现帧集合动画教程在之前已经讲述过,那么这里就不在赘述,如果还不清楚如何利用很多帧形成动画让精灵播放的童鞋请移步到:【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!
直接上一段代码如下:
view plaincopy to clipboardprint?
[[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];
view plaincopy to clipboardprint? [[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]; [[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获取到,这个大家肯定很熟悉,那么获取动作方式如下:
view plaincopy to clipboardprint?
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
view plaincopy to clipboardprint? 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]; 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];
view plaincopy to clipboardprint?
-(void)slowForHimi{
[self unschedule:@selector(slowForHimi)];//解除此选择器
CCSprite*sprite=(CCSprite*)[self getChildByTag:66];
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
[speed setSpeed:0.5];//放慢原有速度的0.5倍
}
view plaincopy to clipboardprint? [CCSpeed* setSpeed:XX]; [CCSpeed* setSpeed:XX];
这里的XX参数指的是倍率,传入1表示原速,大于1表示增快,小于1表示放慢速度~
下面直接给出全部测试项目代码:
view plaincopy to clipboardprint?
//
// 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"];
//左侧正常速度的播放
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];
//左侧动态放慢的速度的动作
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;
}
-(void)slowForHimi{
[self unschedule:@selector(slowForHimi)];//解除此选择器
CCSprite*sprite=(CCSprite*)[self getChildByTag:66];
CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];
[speed setSpeed:0.5];//放慢原有速度的0.5倍
}
// 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
view plaincopy to clipboardprint? [[CCScheduler sharedScheduler] setTimeScale:XX]; [[CCScheduler sharedScheduler] setTimeScale:XX];
这里的XX仍然是倍率:传入1表示原速,大于1表示增快,小于1表示放慢速度~
OK,本篇就到此~
源码下载地址:http://download.csdn.net/detail/xiaominghimi/3839063
相关文章推荐
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现动画进行时慢动作\设置游戏加减速进行
- CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【一】
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【二】 推荐
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!更新解决添加组件Cocos2d动画停止播放的BUG】
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(28种)以及设置屏幕横竖屏!
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(28种)以及设置屏幕横竖屏!