2012-06-19 13 views
0

時潛入 「學習了cocos2d遊戲開發與iOS5的」,在CH08古玩約CCSpriteBatchNode的addChild方法

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; 
} 

所以批與質感 「怪物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'中,爲什麼不同的敵人仍然可以加入批處理?

回答

1

可以將多個圖像放入一個。這樣的圖像被稱爲地圖集。所以所有的怪物都有一個很大的紋理。每個圖像的訪問都是使用.plist配置字段完成的,其中存儲了有關放置在大紋理中的具體圖像的位置的信息。

它有很多意義,因爲它是一個昂貴的swtich紋理操作。將所需的所有東西放在一個紋理中並使用批處理速度要快得多。此外,由於紋理存儲在GPU上的(2^n,2^m)緩衝區中,並且提供了很大的紋理,因此可以最大限度地減少紋理中的可用空間,因此它在GPU上佔用的內存也更少。例如,加載65像素/ 65像素的圖像將創建128/128像素紋理。

+0

我的意思是:批量的紋理是'monster-a.png',但後來它的framename是'monster-b.png'的addchild,批量添加那個紋理中不包含的CCSprites? – limboy

+0

沒有。紋理取自CCSpriteFrame。多個精靈幀可以引用相同的紋理 – Andrew

+0

@Izyy:看看這裏:http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d。紋理是從多個圖像創建的,然後與CCSpriteFrameCache – Andrew