Using Tecture packer image not showing properly in cocos2D V3.3 iOS - cocos2d-iphone

I am trying to add images from TexturePacker, image is showing but images appears messed up like refered image some parts coming with another image ->
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"sprites-hd.plist"];
CCSprite* lobjSprite = [CCSprite spriteWithImageNamed:#"bird.png"];
[self addChild:lobjSprite];

Related

Strange artefacts appear on CCSprite

I have a CCLayer class imported onto a game layer
strangely all the the sprites have "artefacts" that seem to appear from nowhere since I have checked and re-exported all of the files
Is some setting or something else that could cause this to happen?
I'm new at this
but I have checked so far:
set to PixelFormat_RGBA8888
PVRImagesHavePremultipliedAlpha:YES
png's are clear from artefact (28bit with transparency)
Textures are made with texture packer with "pre-multiplied"
The Background is a CCLayer
The Mine is a CCLayer
both are added to the game layer (cclayer also) as "addChild"
backgroundManager = [[BackGround alloc] init];
[self addChild:backgroundManager z:0];
myShip = [[Ship alloc]init];
[self addChild:myShip z:5];
Yes it was the settings in texture packer
after a few changes in the settings they now seem to load fine.
with no artifacts
use pre multiple
trim not crop
and give a little more inner padding
hope it helps someone else (since it was driving me a little crazy)

How to change the image of a CCSprite in cocos2d v3.x

In cocos2d 2.x we change the image of a CCSprite using CCTexture. But in cocos2d 3.x CCTextureCache seems to be deprecated as Xcode warns me : "undeclared identifier 'CCTextureCache'". Or may be am I miss something as I'm new to cocos.
So how can we change the image of a CCSprite in v3 ??
Thank you.
I think I know how to do.
We have to use a spriteSheet built with TexturePacker [note : may be it's wrong to speak about external resources like it on SO] for example (let's say we have 2 images : monster_01.png and monster_02.png).
We add the .plist and the .png into xCode
We put the spritesheet in cache
and then we can create a CCSprite with a frame using a item of the spritesheet.
This image can be changed.
Some code :
3) We put in cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"monsterSpriteSheet.plist"];
4) We create the sprite
CCSprite * mySprite = [CCSprite initWithSpriteFrame: [CCSpriteFrame frameWithImageNamed: #"monster_01.png"]];
5) To change image :
[mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed: #"monster_02.png"]];
This works perfectly with cocos2d v3.
I spent 6 hours to have this process. Sometimes I feel stupid.
You can do it by using this
CCSpriteFrameCache and after that you can change you sprite by using function setSpriteFrame of ccsprite.

Which APIs should we use to add spritesheets and sprites to a cocos2d game?

While creating a cocos2d iOS game, there are several options to add spritesheets - CCTextureCache::addImageAsync, CCSpriteFrameCache::addSpriteFramesWithFile, etc. - what is the difference between using these different ways to add a spritesheet?
Similarly, to load a sprite, we can call CCSprite::spriteWithSpriteFrameName or CCSprite::spriteWithFile or CCSpriteBatchNode::batchNodeWithTexture, etc. What is the difference between using these techniques?
Thanks
Anand
Load sprite frames, this also loads the textures:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"file.plist"];
Use sprite frames:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:#"frame.png"];
Add the sprite to a batch node:
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithTexture:sprite.texture];
[batchNode addChild:sprite];
The batchNodeWithFile works too if you use the same image file of the sprite. If the sprite was initialized with a spriteframe, it'll be the texture atlas image (ie "file.png").
addImageAsync is only needed if you want to load your texture on another thread, usually to animate the loading screen. You'll still have to add the sprite frames afterwards.
CCSprite spriteWithFile creates a sprite from a single image file. Those can be batched too but it's better to use a texture atlas with sprite frames.

How do I load a CCSpriteBatchNode from the documents directory?

I have a file in the documents directory that I verify does exist before I call this method:
CCSpriteBatchNode *batchNode = [[CCSpriteBatchNode alloc] initWithFile:filePath capacity:9];
The error reported is:
-[CCFileUtils fullPathFromRelativePath:resolutionType:] : cocos2d: Warning: File not found: decompressed_file.png
cocos2d: CCTexture3d: Can't create Texture. cgImage is nil;
Using Cocos2d 2.0
You can use CCSpriteBatchNode and pull files from the Documents directory (or any other directory you want) by setting the searchPath, e.g. like so:
NSString *documentDirectory = aDirectory;
NSMutableArray * loadSearchPath = [[CCFileUtils sharedFileUtils].searchPath mutableCopy];
[loadSearchPath addObject:documentDirectory];
[CCFileUtils sharedFileUtils].searchPath = [loadSearchPath copy];
This adds aDirectory to the searchable paths where Cocos2D will be looking for the file with the name you specify in [CCSpriteBatchNode batchNodeWithFile:#"aFile"];
The cocos2d functions file methods assume you are loading from your bundle resources. I do not think you can load the file directly but what you can do is load it into a UIImage and then create a CCTexture2D with a CGImage. With your CCTexture2D you can create a CCSpriteBatchNode.
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
CCTexture2d *tex = [[CCTextureCache sharedTextureCache] addCGImage:[img CGImage] forKey:#"myTexture"];
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithTexture:tex capacity:9];
These steps are whats going on in the background anyway when you create a CCSpriteBatchNode using the normal methods. Except for the bundle assumption.
If you are making the sprite batch node more than once for some reason, you can check if the texture already exists in the cache before reloading the UIImage.

Get CGImageRef from CCSprite, CCTexture2D or even directly from CCSpriteFrameCache?

I have written some code that takes a UIImage.CGImageRef and puts it into a context so that I can analyze it. This all works great. BUT, I now wish to implement this process in a Cocos2D app.
All of my graphics for the app are done as sprite sheets using Texture Packer so I am looking for any way to get the CGImageRef that I require out of the sprite sheet.
My theory is that you can init a CCSprite with a CGImageRef so why can't I simply get it back out again?
Maybe I am missing something simple like does Cocos2D have a CGImageRef equivalent?
you can try smth like this
CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
[renderer begin];
CCSpriteBatchNode *spriteSheet = [m_sprite batchNode];
if (spriteSheet != nil)
[spriteSheet visit];
else
[m_sprite visit];
[renderer end];
NSData* uidata = [renderer getUIImageAsDataFromBuffer:kCCTexture2DPixelFormat_RGBA8888];
UIImage* uiimage = [UIImage imageWithData:uidata];
CGImageRef image = [uiimage CGImage];
You can get image data from OpenGL via glReadPixels. It needs some adjustments, but it would be wrong solution in performance aspect. OpenGL is designed as one-way conveyer (you pass data and don't read it back). Depending on your concrete task you may preload image data when loading textures from file.
As example we use alpha channel of textures for per-pixel touch test. So, when we load image we store its bitmask of alpha threshold in memory.
no
cctexture direct from [CCTextureCache sharedTextureCache] read from photo file
ccspriteframe direct from [CCSpriteFrameCache sharedSpriteFrameCache] read from ".plist" file