2013-03-09 92 views
0

我正嘗試使用加載粒子的紋理的粒子系統。由於它只是一個練習,所以我可以「手動」生成紋理。所以在這種情況下,我只是分配一個數據緩衝區並填充紅色(RGBA 32位格式)。來自紋理的粒子系統無法加載

但問題是,當程序開始運行時,我只看到黑屏,並沒有我期待看到的(一些紅色的粒子繼續前進)。

我解釋什麼,我想在評論中做到:

if((self=[super init])) 
{ 
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init]; 

    uint32_t mask= 255+pow(255, 4); // This is a 32 bit red pixel 
    NSUInteger size=256; // The texture is 256x256 
    void* buffer= malloc(size*size*4); // Here I hold the data, it will be all red 
    for(NSUInteger i=0; i<size; i++) 
    { 
     for(NSUInteger j=0; j<size; j++) 
     { 
      memcpy(buffer+j+i*32, &mask, 4); 
     } 
    } 
    NSData* data=[[[NSData alloc]initWithBytesNoCopy: buffer length: size*size*4 freeWhenDone: YES]autorelease]; // I wrap it into NSData to automatically release it later. 
    CCTexture2D* texture=[[[CCTexture2D alloc]initWithData: data.bytes pixelFormat: kCCTexture2DPixelFormat_RGBA8888 pixelsWide: size pixelsHigh: size contentSize: CGSizeMake(size, size)]autorelease]; 
    CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithTotalParticles: 100]autorelease]; 
    system.texture= texture; // I set the texture in a way that it should be load to draw particles 
    system.position= ccp(200, 200); 
    system.startSize= 10; 
    system.endSize= 5; 
    system.duration= kCCParticleDurationInfinity; 
    system.life= 1; 
    [self addChild: system]; 

    [pool drain]; 
} 

使用圖像

我試圖修改代碼中,我使用的UIImage代替人工的方式創建紋理:

if((self=[super init])) 
{ 
    // Even with glClearColor() it doesn't work 
    //glClearColor(1, 0, 1, 1); 
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init]; 

    NSBundle* bundle=[NSBundle mainBundle]; 
    NSString* path=[bundle pathForResource: @"fire" ofType: @"png"]; 
    UIImage* image=[UIImage imageWithContentsOfFile: path]; 
    CCTexture2D* texture=[[[CCTexture2D alloc]initWithImage: image]autorelease]; 
    CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithTotalParticles: 100]autorelease]; 
    system.texture= texture; 
    system.startSize= 10; 
    system.endSize= 10; 
    system.life= 1; 
    system.duration= kCCParticleDurationInfinity; 
    system.position= ccp(200, 200); 
    // Even changing the start/end color it doesn't work 
    //system.startColor= (ccColor4F){1.0,0.0,0.0,1.0}; 
    //system.endColor= (ccColor4F){1.0,0.0,0.0,1.0}; 
    system.emitterMode= kCCParticleModeGravity; 
    system.gravity= ccp(200, 200); 
    system.speed=1; 
    [self addChild: system]; 

    [pool drain]; 
} 

在應用程序軟件包中的圖像 「fire.png」 是:

enter image description here

我還設置了一些其他屬性,但它仍然無法正常工作:黑屏。

+0

使用memset的(緩衝液,及掩模,尺寸*尺寸* 4)只用一個顏色的所有填充緩衝區一次,fo​​r循環和每次迭代複製4個字節是非常無效的 – LearnCocos2D 2013-03-09 23:41:34

+2

NSData是不必要的,直接將緩衝區傳遞到CCTexture2D並在該行中釋放(緩衝區)。這是確定的,因爲CCTexture2D將緩衝區中的數據複製到實際紋理中,不再需要緩衝區。 NSAutoreleasePool在這裏也是完全不必要的(因爲你沒有釋放它,所以池正在泄漏..考慮使用ARC)。 – LearnCocos2D 2013-03-09 23:44:42

+0

關於memset:它只需要第一個字節,但我有一個4字節的整數,這就是爲什麼我使用循環。關於池:我應該使用release而不是autorelease並放棄autorelease池嗎?順便說一句[游泳池排水]釋放游泳池。 – 2013-03-10 10:10:43

回答

0

我結束了使用粒子薄荷創建粒子系統,然後使用工廠方法:

CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithDictionary: [ParticleFactory makeEffect]]autorelease]; 
2

夫婦的事情,我想起(除了代碼的改進,見我的意見):

更改鮮明的色彩,只是爲了看看粒子系統,可以運行,但只產卵的黑色顆粒:

glClearColor(1, 0, 1, 1); 

粒子系統本身可能沒有正確配置(不完整的數據,屬性使用默認值,通常只是0)。有可能您未修改的屬性會導致粒子系統渲染不正確。例如,粒子系統具有startColor和endColor屬性。如果它們都被初始化爲黑色或透明顏色,因爲您沒有將它們設置爲較亮(白色)的顏色,則紋理的顏色無關緊要,這些顆粒將變成黑色。我相信這很可能是這裏發生的事情。

先嚐試用圖像文件中的常規紋理設置粒子系統。如果您已確認粒子系統創建可見的非黑色粒子,則可以切換到使用自定義紋理。

+0

我嘗試用圖像創建的紋理,它不起作用,我編輯了問題。 – 2013-03-10 10:16:43