2011-07-19 55 views
0

當我在一個循環中創建一個精靈圖表時,我不知道有多少精靈在那裏,所以我必須先計算它們,因爲動畫完成後,我必須運行別的東西。計算精靈圖表中的圖片數量 - cocos2d

那麼我該如何計算一下開始循環的圖像數量? 還是有另一種方式呢? (此功能可以得到各種精靈表。 我現在寧願不使用的plist

非常感謝。

-(void)animation 
{ 

    WeInAction=1; 




    //animation 
    CCSpriteBatchNode *spriteSheet = [ CCSpriteBatchNode batchNodeWithFile:animation]; 
    [self addChild:spriteSheet]; 

    CCSprite *dollSprite = [CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(0, 0, 320, 350)]; 
    [spriteSheet addChild:dollSprite]; 
    spriteSheet.anchorPoint=CGPointMake(0, 30); 

    CGSize s = [[CCDirector sharedDirector] winSize]; 
    dollSprite.position = ccp(s.width/2,s.height/2); 

    CCAnimation *dollAnimation = [CCAnimation animation]; 
    [dollAnimation setDelay:0.1f]; 

    int frameCount = 0; 
    for (int y = 0; y < 5; y++)  //cocos add all this frames to memery and then he starts down the action, 

    { 

     for (int x = 0; x < 6; x++) 
     { 

      CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture rect:CGRectMake(x*320,y*350,320,350)]; 
      [dollAnimation addFrame:frame]; 
      frameCount++; 

      if (frameCount == 25) 
      break; 

     } 
    } 




    CCAnimate *Action = [CCAnimate actionWithAnimation:dollAnimation]; 
    id call=[CCCallFunc actionWithTarget:self selector:@selector(finishAnimation)]; 
    id sequence=[CCSequence actions:Action,[CCHide action],call,nil]; 
    [dollSprite runAction:sequence]; 
    NSLog(@"%@",basic_pic); 

} 

回答

0

這與你讓你的spritesheets的程序做。 Cocos2D中不知道有一個以上的圖像,除非你告訴它有

編輯:

的Cocos2D如果你告訴它有圖像,只知道有圖像。如果你告訴它圖像是一個spritesheet,Cocos2D只知道圖像是一個spritesheet。這就是PLIST文件的用途。 PLIST告訴Cocos2D spritesheet中每個精靈的位置和大小。

你的問題的簡短答案是否定的。最簡單的做法是隻計算你的spritesheet中的精靈。這並不完全困難。如果您確實需要知道代碼的數量,那麼您需要編寫一兩個方法來根據圖像的所有參數對它們進行計數。這是相當基礎的數學。

(widthOfSpritesheet/widthOfIndividualSprite)*(heightOfSpritesheet/heightOfIndividualSprite) 

這是基本的數學,但你也必須要考慮,你可能沒有完全滿spritesheet所以你需要一種方法來知道最後一行是否已滿。

真的,這聽起來像更多的工作比它的價值。你應該只是在spritesheet中計算你的精靈,或者向spritesheet軟件的開發者發出一個請求,以引入一個新特性,在spritesheet中提供全部的精靈。我使用TexturePacker,它不提供我知道的這個功能,但我從來也不需要它。

+0

我不能理解你。你可以解釋嗎 ?我正在用x和y運行一個循環,而之前我不知道它們的值。有一種方法可以知道嗎?我需要運行一個通用循環,以適應任何spritesheet它.. – Curnelious

+0

Cocos2D不檢查圖像。 Cocos2D只知道Spritesheet中有多個圖像,如果你告訴它有。實際上它不知道普通圖像和Spritesheet之間的區別,除非你告訴它。如果你的所有圖像總是相同的尺寸,那麼你可以自己計算它,或者你可以根據你插入的值創建一個計算方法。 –

+0

我無法找到一種方法如何知道...你可以給我任何大方向?科科斯是「盲目的」,它運行在我給它的循環數... – Curnelious