贝塞尔曲线:如何让一个正在按照贝塞尔曲线运动的精灵减速或加速(动画结束回调)...

@implementation ActionBezier
- (void) callback
{
NSLog(@"callback");
}
-(void) onEnter
{
[super onEnter];

CGSize s = [[CCDirector sharedDirector] winSize];

//
// startPosition can be any coordinate, but since the movement
// is relative to the Bezier curve, make it (0,0)
//

// sprite 1
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(0, s.height/2);
bezier.controlPoint_2 = ccp(300, -s.height/2);
bezier.endPosition = ccp(300,100);

//结束时回调
id callback = [CCCallFunc actionWithTarget:self selector:@selector(callback)];

id bezierForward = [CCBezierBy actionWithDuration:5 bezier:bezier];
id bezierBack = [bezierForward reverse];
id seq = [CCSequence actions: bezierForward, bezierBack, callback,nil];
id rep = [CCRepeatForever actionWithAction:seq];

//设置速度
id speed = [CCSpeed actionWithAction:rep speed:2];
[speed setTag:222];

// sprite 2
//[tamara setPosition:ccp(80,160)];
ccBezierConfig bezier2;
bezier2.controlPoint_1 = ccp(100, s.height/2);
bezier2.controlPoint_2 = ccp(200, -s.height/2);
bezier2.endPosition = ccp(240,160);

id place1 = [CCPlace actionWithPosition:ccp(80,160)];
id bezierTo1 = [CCBezierTo actionWithDuration:2 bezier:bezier2];
[tamara runAction:[CCSequence actions: place1, bezierTo1, nil]];

// sprite 3
//[kathia setPosition:ccp(400,160)];
id place2 = [CCPlace actionWithPosition:ccp(400,160)];
id bezierTo2 = [CCBezierTo actionWithDuration:2 bezier:bezier2];
[kathia runAction:[CCSequence actions: place2, bezierTo2, nil]];

[grossini runAction: speed];
//设置速度
[((CCSpeed *)[[self getChildByTag:999] getActionByTag:222])setSpeed:4];
NSLog(@"%f",((CCSpeed *)[grossini getActionByTag:222]).speed);
//[tamara runAction:bezierTo1];
//[kathia runAction:bezierTo2];

}
-(NSString *) title
{
return @"BezierBy / BezierTo";
}
@end

 

素材:http://www.cocoachina.com/bbs/simple/?t73237.html

请大大们进来帮忙看下【贝塞尔曲线:如何让一个正在按照贝塞尔曲线运动的精灵减速或加速】

    ccBezierConfig config;
    config.endPosition=ccp(600,600);
    config.controlPoint_1=ccp(200,500);
    config.controlPoint_2=ccp(500,200);
    [[self getChildByTag:998] runAction:[CCSequence actions:[CCBezierTo actionWithDuration:5.0f bezier:config],[CCCallFunc actionWithTarget:self selector:@selector(moveDone:)], nil]];

一个精灵正在执行CCBezierTo 这个动作,如何让她在中途减速或者加速呢?

方法

  

     CCSprite *sprite3=[CCSprite spriteWithFile:@"giantAnt.png"];        
sprite3.position=ccp(100,100);
[self addChild:sprite3 z:999 tag:9987];

id move = [CCBezierTo actionWithDuration:5.0f bezier:config];
id speed =[CCSpeed actionWithAction:move speed:1.0f];
[speed setTag:222];
[sprite3 runAction:speed];


如何要减速的话
就执行这行代码:
[(CCSpeed*)[[self getChildByTag:9987] getActionByTag:222] setSpeed:0.5f];

加速:
[(CCSpeed*)[[self getChildByTag:9987] getActionByTag:222] setSpeed:1.5f];


[(CCSpeed*)[self getActionByTag:222] setSpeed:1.5f];




 

【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!

1.利用CCSpeed当精灵执行CCAnimate动作途中设置其播放的速度;

           2.设置游戏的速率,让你自由设置整个游戏的速度;

首先介绍第一个知识点:

对于第一个知识点,精灵执行CCAnimate动作途中设置播放速度,说白一点就是当主角或者怪物播放一套帧动作(动画)的时候,可能突然受到其他因素影响希望主角或者怪物等动作放慢,也就是慢动作的感觉,那么这时候我们就需要设置动作的播放速度拉,也就是今天要介绍的CCSpeed这个类;可能Himi这里哇哇哇的说这么多还是没亭台明白吧…=。 = 那么下面我们来看看代码等就应该明白了;

至于精灵如何利用CCAnimate实现帧集合动画教程在之前已经讲述过,那么这里就不在赘述,如果还不清楚如何利用很多帧形成动画让精灵播放的童鞋请移步到:【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!

直接上一段代码如下:

[[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来实现了,代码如下:

[[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];
CCSpeed *speed =[CCSpeed actionWithAction:[CCRepeatForever actionWithAction:seq] speed:1.0f];
[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表示放慢速度~

下面直接给出全部测试项目代码:

//
// 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

  这里备注下:除了利用CCSpeed来实现慢动作之外,还有其他的一些方法,不怕麻烦的童鞋甚至可以尝试当需要慢动作的时候,取出当前的帧下标,然后利用指定帧下标的方法创建一个新的帧动画同时增加播放时间即可;(在上一节《iOS-Cocos2d游戏开发之二十一》中Himi封装了一个指定帧下标进行创建帧动画的方法,还没有看过的童鞋请移步到这里:【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!);再或者直接去修改Cocos2d-iphone引擎的源码;

    Himi当时做的时候因为用CCSpeed方式有问题一直不行,就去改了源码弄的。后来才发现CCSpeed正确用法,我去了=。 =

这里Himi必须强调一点!!!!!!!

    很多时候你的主角的动作利用CCAction来实现,移动则是在update刷帧函数或者一些选择器的方法中进行的,那么为了让你的主角慢动作比较逼真,那么Himi建议不要使用scheduleUpdate函数,因为这个你无法修改每次调用update的时间默认都是每帧都调用,那么你应该自己定义一个选择器当刷逻辑的函数,这样就能配合CCSpeed实现逼真慢动作拉~


下面我们来介绍第二个知识点:设置游戏速度

对于游戏速度最常见的出现在塔防游戏中,当玩家创建好防守的东东后开始出怪后,可能怪物移动速度慢,而玩家着急看到结果,那么我们就会人性化的加上加快游戏速度的按钮拉~那么这个功能在Cocos2d引擎中封装好的,一句代码即可完成,如下代码即可:


[[CCScheduler sharedScheduler] setTimeScale:XX];

这里的XX仍然是倍率:传入1表示原速,大于1表示增快,小于1表示放慢速度~

 














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值