我正在尝试使用粒子系统为纹理加载粒子。由于这只是一个练习,我可以“手动”生成纹理。所以在这种情况下,我只是分配一个数据缓冲区并用红色(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”是:
我还设置了一些其他属性,但它仍然不起作用:黑屏。