gpt4 book ai didi

ios - 对CCSpriteBatchNode的addchild方法感到好奇

转载 作者:行者123 更新时间:2023-11-29 04:35:34 26 4
gpt4 key购买 nike

当深入学习“使用ios5学习cocos2d游戏开发”时,第08章

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.minitWithType方法中

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 个不同的框架中。自 Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode ,显然,'monster-b.png'不包含在'monster-a.png'中,为什么不同的敌人仍然可以添加到批处理中?

最佳答案

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

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

关于ios - 对CCSpriteBatchNode的addchild方法感到好奇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com