Ios 关于CCSpriteBatchNode&x27的古玩;s-addchild方法

Ios 关于CCSpriteBatchNode&x27的古玩;s-addchild方法,ios,cocos2d-iphone,Ios,Cocos2d Iphone,当进入ch08中的“使用ios5学习cocos2d游戏开发”时 在EnemyCache.m -(id) init { if ((self = [super init])) { // get any image from the Texture Atlas we're using CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:

当进入ch08中的“使用ios5学习cocos2d游戏开发”时

EnemyCache.m

-(id) init
{
    if ((self = [super init]))
    {
        // get any image from the Texture Atlas we're using
        CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"monster-a.png"];
        batch = [CCSpriteBatchNode batchNodeWithTexture:frame.texture];
        [self addChild:batch];

        [self initEnemies];
        [self scheduleUpdate];
    }

    return self;
}
所以批处理使用纹理“monster-a.png”

EnemyEntity.m
initWithType
方法中

switch (type)
{
    case EnemyTypeUFO:
        enemyFrameName = @"monster-a.png";
        bulletFrameName = @"shot-a.png";
        break;
    case EnemyTypeCruiser:
        enemyFrameName = @"monster-b.png";
        bulletFrameName = @"shot-b.png";
        shootFrequency = 1.0f;
        initialHitPoints = 3;
        break;
    case EnemyTypeBoss:
        enemyFrameName = @"monster-c.png";
        bulletFrameName = @"shot-c.png";
        shootFrequency = 2.0f;
        initialHitPoints = 15;
        break;

    default:
        [NSException exceptionWithName:@"EnemyEntity Exception" reason:@"unhandled enemy type" userInfo:nil];
}

if ((self = [super initWithSpriteFrameName:enemyFrameName]))
{
    //...
}

因此,返回的对象可能位于3个不同的帧中。由于“monster-a.png”中显然不包含“monster-b.png”,为什么不同的敌人仍然可以添加到批次中?

可以将多个图像放在一个中。这样的图像称为地图集。所以所有的怪物都在一个大纹理中。对每个图像的访问都是使用.plist配置字段完成的,其中存储了有关混凝土图像在大纹理中放置位置的信息


这很有意义,因为切换纹理是一项昂贵的操作。更快的方法是将所有需要的东西放在一个纹理中并使用批处理。此外,由于纹理存储在GPU上的(2^n,2^m)缓冲区中,因此它在GPU上占用的内存更少,并且提供了一个大的纹理,您可以最小化纹理中的可用空间。例如,加载大小为65/65像素的图像将创建128/128像素的纹理。

我的意思是:批处理的纹理是“monster-a.png”,但后来它添加了帧名为“monster-b.png”的child,如何批处理添加不包含在该纹理中的ccsprite?不。纹理取自CCSpriteFrame。多个精灵帧可以引用同一个texture@Izyy:请看这里:。纹理是从多个图像创建的,然后与CCSpriteFrameCache一起使用。因此,只要我通过
batchNodeWithTexture
将纹理的一个图像添加到批次中,我就可以通过
addchild
在该纹理中添加任何图像。您可以使用任何纹理创建批次。但您只能将具有相同纹理的精灵添加到该批次。在您的示例中,这些精灵是使用与批处理节点参照到同一纹理的精灵帧创建的