【问题标题】:CCSpriteBatchNode and CCArray, finding inactive objectsCCSpriteBatchNode 和 CCArray,查找非活动对象
【发布时间】:2012-12-15 05:17:45
【问题描述】:

对于一个简单的游戏,我有 4 个不同的平台(都在一个 spritesheet 上)。我最初将每个添加 5 个到 CCSpriteBatchNode,并将它们全部设置为不可见。当我设置我的平台时,我想从我的 CCSpriteBatchNode 中获取一个特定类型的平台并更改它以使其可见并定位它。

我无法找到不可见的特定类型平台。反之亦然?

我知道你可以使用 [batchnode getchildbytag:tag] 但据我所知,它只返回一个精灵。有什么方法可以将指向特定类型的每个平台的指针放入数组中,以便我可以遍历数组并找到所有不可见的精灵?

谢谢!

【问题讨论】:

    标签: iphone cocos2d-iphone ccsprite ccspritebatchnode


    【解决方案1】:

    正如 Drama 所建议的,您别无选择,只能“迭代”孩子。至于识别哪个精灵对应哪个平台,有几种方法。一个简单的方法是使用精灵的“标签”属性——假设您不将它用于任何其他目的。

    // some constants 
    
    static int _tagForIcyPlatform = 101;
    static int _tagForRedHotPlatform = 102;
    ... etc
    
    // where you create the platforms
    
    CCSptiteBatchNode *platforms= [CCSpriteBatchNode batchNodeWithFile:@"mapItems_playObjects.pvr.gz"];
    CCSprite *sp = [CCSprite striteWithSpriteFrameName:@"platform_icy.png"];
    sp.tag = _tagForIcyPlatform;
    [platforms addChild:sp];
    
    sp = [CCSprite striteWithSpriteFrameName:@"platform_redHot.png"];
    sp.tag = _tagForRedNotPlatform;
    [platforms addChild:sp];
    
    
    // ... etc
    
    // where you want to change properties of 
    
    -(void) setVisibilityOf:(int) aPlatformTag to:(BOOL) aVisibility {
        for (CCNode *child in platforms.children) {
            if (child.tag != aPlatformTag) continue;
            child.visible = aVisibility;
        }
    }
    

    再一次,如果您没有将平台的子标签用于其他目的,则此方法有效。如果您需要标签用于其他目的,请考虑在类中使用 NSMutableArray,每种平台类型一个,并将指向适当类型精灵的指针存储在该数组中。

    【讨论】:

    • 谢谢,通过批处理子循环运行良好。如果每次更新都必须找到每个活跃的平台来更新它的位置,这种方法会不会变得非常低效?
    【解决方案2】:

    没有一种超级简单的方法可以做到这一点。您需要遍历孩子并单独检查每个孩子。

    为了编码效率,请考虑向 CCSpriteBatchNode 添加一个类别来为您执行此功能。这样您就可以根据需要轻松复制它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多