开发者

Setting the texture of a particle using CCTextureCache

I am giving my first steps with cocos2d-iphone.

I 开发者_Python百科am using:

CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"textures.plist"];

to use my zwoptex file. With that set, I am creating my CCSprites using the frameCache like this:

[CCSprite spriteWithSpriteFrameName:@"filename.png"];

So far, so good. Now I am creating my own particle system and I need to set the texture. I guess it would make sense to read from the zwoptex but I couldn't find a way to do it. I also checked the examples and they do something like:

emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"fire.pvr"];

So my questions are:

  • If I am going to have more than a particle system with different textures. Should I create a zwoptext with them? If so, how?
  • Why do the examples do [[CCTextureCache sharedTextureCache] addImage: @"file"];? it's because it's not added two times?

EDIT: I just posted this in the cocos2d's forum.


If I am going to have more than a particle system with different textures. Should I create a zwoptext with them? If so, how?

A texture sheet might improve the performance for rendering, because it would not change OpenGL ES state (binding textures). Please take a look at "How to Create and Optimize Sprite Sheets in Cocos2D with Texture Packer and Pixel Formats". I strongly recommend you to use TexturePacker to create texture sheets.

Why do the examples do [[CCTextureCache sharedTextureCache] addImage: @"file"];? it's because it's not added two times?

It is easy to use for loading OpenGL texture. You must not use OpenGL ES API such as glTexImage2D or so forth. Also, of course, CCTextureCache always caches textures. It returns immediately CCTexture2D instance if the specified file is already cached.

EDIT:

You can get CCTexture2D instance from CCSpriteFrameCache with zwoptext plist.

CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"textures.plist"];

CCSpriteFrame *frameFire = [frameCache spriteFrameByName:@"particle_fire"];
CCTexture2D *texFire = frameFire.texture;
...
glBindTexture(GL_TEXTURE_2D, texFire.name);
/* You may need to use frameFire.rectInPixels and frameFire.rotated for the texture coordinates. */
...

CCSpriteFrame *frameWater = [frameCache spriteFrameByName:@"particle_water"];
CCTexture2D *texWater = frameWater.texture;
...
glBindTexture(GL_TEXTURE_2D, texWater.name);
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜