首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在cocos2d中同步精灵动画

在cocos2d中同步精灵动画
EN

Stack Overflow用户
提问于 2013-04-20 09:34:32
回答 2查看 260关注 0票数 1

每隔一段时间,我就会在场景中添加一些跳动的精灵,如下所示:

代码语言:javascript
复制
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];

我想让我所有的精灵同步跳动,我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2013-04-20 10:50:31

嗯..。这可不容易。我能想到的唯一方法就是安排一个'flasherRamp‘,如下所示:

在.h中

代码语言:javascript
复制
NSMutableArray *flashers;

在.m中,初始化方法

代码语言:javascript
复制
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中,您可以在其中创建精灵

代码语言:javascript
复制
foo2.visible=NO;
[flashers addObject foo2];

最后

代码语言:javascript
复制
-(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秒的延迟的“异步”游戏事件之间存在某种因果关系,这可能不是一个好主意。

如图所示。:从内存中编码,未测试,但应接近。

票数 2
EN

Stack Overflow用户

发布于 2013-04-23 02:24:43

在这种情况下,我会避免使用CCRepeatForever。

创建一个定义当前色调状态(tintGray、tintWhite、tintDone)的枚举,然后创建一个检查状态的调度选择器。

状态完成后,对batchnode的每个子节点重复这些操作(假设这些子节点是唯一的子节点)。

要调度选择器,请在init或其他加载方法中放置以下内容:

代码语言:javascript
复制
// be sure to schedule the interval at a fast enough rate
[self schedule:@selector(tick:) interval:0.1f];

然后将该方法定义如下:

代码语言:javascript
复制
-(void)tick:(ccTime)dt
{
    if(tintState == tintDone)
    {
        [self unschedule:@selector(tick:)];
        [self tinter];
    }
}

然后为所有精灵安排染色操作:

代码语言:javascript
复制
-(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“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16115622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档