Cocos2d iphone 在cocos2d中同步精灵动画
我不时地在我的场景中添加一些跳动的精灵,如下所示:Cocos2d iphone 在cocos2d中同步精灵动画,cocos2d-iphone,Cocos2d Iphone,我不时地在我的场景中添加一些跳动的精灵,如下所示: CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [scene getChildByTag: foo1]; sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(0, 0, 128, 128)]; sprite.position = foo2 CCTintTo *a = [CCTintTo actionWithDuration:
CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [scene getChildByTag: foo1];
sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(0, 0, 128, 128)];
sprite.position = foo2
CCTintTo *a = [CCTintTo actionWithDuration: .5 red:128 green: 128 blue: 128];
CCTintTo *b = [CCTintTo actionWithDuration: .5 red:255 green: 255 blue: 255];
[sprite runAction:[CCRepeatForever actionWithAction:
[CCSequence actionOne: a two: b]]];
[batch addChild: sprite];
我想让我所有的精灵同步跳动,我该怎么做呢?嗯。。。不容易。我认为这样做的唯一方法是安排一个“flashrramp”,如下所示: in.h in.m,init方法
flashers = [[NSMutableArray array] retain]; // choose your own ARC flavor, if you retain
// dont forget to release in dealloc
[self schedule:@selector(flasherRamp) interval:1.0f];
在.m中,创建精灵的位置
foo2.visible=NO;
[flashers addObject foo2];
最后
-(void) flasherRamp {
for (CCSprite *flasher in flashers) {
CCTintTo *a = [CCTintTo actionWithDuration: .5 red:128 green: 128 blue: 128];
CCTintTo *b = [CCTintTo actionWithDuration: .5 red:255 green: 255 blue: 255];
[flasher runAction:[CCRepeatForever actionWithAction:
[CCSequence actionOne: a two: b]]];
flasher.visible=YES;
}
[flashers removeAllObjects];
}
最后可能会有一些漂移,这取决于这种情况持续的时间
pps。从可用性的角度来看,如果闪烁精灵的出现与某些“异步”游戏事件之间存在某种因果关系,则这可能不是一个好主意,因为这些事件可能导致触发事件与闪烁器的实际出现之间的延迟长达1秒
ob cit:从内存中编码,未测试,但应关闭。在这种情况下,我将避免永远使用CCRepeat 创建一个定义当前着色状态的枚举(tintGray、tintWhite、tintDone),然后创建一个检查该状态的计划选择器 状态完成后,对batchnode的每个子节点重复操作(假设这些子节点是唯一的子节点) 要计划选择器,请在init或其他加载方法中放置以下内容:
// be sure to schedule the interval at a fast enough rate
[self schedule:@selector(tick:) interval:0.1f];
然后定义方法如下:
-(void)tick:(ccTime)dt
{
if(tintState == tintDone)
{
[self unschedule:@selector(tick:)];
[self tinter];
}
}
然后,要计划所有精灵的着色操作,请执行以下操作:
-(void)tinter
{
// could init and reuse this somewhere else to save on allocs
CCSequence *actions = [CCSequence actions:
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintGray;
}],
[CCTintTo actionWithDuration: .5 red:128 green: 128 blue: 128],
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintWhite;
}],
[CCTintTo actionWithDuration: .5 red:255 green: 255 blue: 255],
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintDone;
}],
nil];
CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [scene getChildByTag: foo1];
for (CCSprite *flasher in batch.children)
{
[flasher stopAllActions];
[flasher runAction:actions];
}
// reschedule tick checking
[self schedule:@selector(tick:) interval:0.1f];
}
显然这并不完美,因为第一个精灵将驱动旗子完成着色,但延迟应该可以忽略不计。如果您想确保它们全部完成,只需将标志更改为精灵数量的运行计数,这样只有当tintState等于batchnode中精灵的数量时,“tinter”才会被调用。谢谢您的建议。对于我的情况,我需要能够在任意时间添加闪光器(玩家创建它们)。我将研究动画制作的低级方法…然后我将远离CCTinto操作,并以类似于更新的预定方法实现它,使用闪光器的对象数组,并在每个周期更新数组中每个对象的颜色。仍然是棘手的代码。祝你好运。
-(void)tinter
{
// could init and reuse this somewhere else to save on allocs
CCSequence *actions = [CCSequence actions:
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintGray;
}],
[CCTintTo actionWithDuration: .5 red:128 green: 128 blue: 128],
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintWhite;
}],
[CCTintTo actionWithDuration: .5 red:255 green: 255 blue: 255],
[CCCallBlockN actionWithBlock:^(CCNode* node)
{
tintState = tintDone;
}],
nil];
CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [scene getChildByTag: foo1];
for (CCSprite *flasher in batch.children)
{
[flasher stopAllActions];
[flasher runAction:actions];
}
// reschedule tick checking
[self schedule:@selector(tick:) interval:0.1f];
}