每隔一段时间,我就会在场景中添加一些跳动的精灵,如下所示:
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];
我想让我所有的精灵同步跳动,我该怎么做呢?
发布于 2013-04-20 02:50:31
嗯..。这可不容易。我能想到的唯一方法就是安排一个'flasherRamp‘,如下所示:
在.h中
NSMutableArray *flashers;
在.m中,初始化方法
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];
}
ps。最终可能会有一些漂移,这取决于这种情况会持续多久。
pps。从可用性的角度来看,这可能不是一个好主意,如果在闪烁的精灵的出现和一些可能在触发事件和闪光器的实际出现之间引起1秒的延迟的“异步”游戏事件之间存在某种因果关系,这可能不是一个好主意。
如图所示。:从内存中编码,未测试,但应接近。
发布于 2013-04-22 18:24:43
在这种情况下,我会避免使用CCRepeatForever。
创建一个定义当前色调状态(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等于批处理节点中的精灵数量时才会调用"tinter“。
https://stackoverflow.com/questions/16115622
复制相似问题