Problem with creating animations - animation

I'm working with cocos2d 0.99.5. I need to create animations. I have 01.png in my resources and in my project. It's ok. I don't understand why I can't create a frame. I've got error SIGABRT.
CCSprite *frame = [CCSprite spriteWithSpriteFrameName:#"01.png"];
I've tried to use this
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:#"01.png"];
But I've got the same error.

That's because you must have your sprite in the spriteSheet. Here is the tutorial about creating animations: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:animation

Related

Rendering NSView to image: cacheDisplayInRect draws transparent areas differently from usual drawRect call

Greetings! I have a problem and Googling brought no results...
I implemented the drawRect method for my NSView (subclass) to draw some shadows and semi-transparent fills. Everything looks great! But now I need to create an NSImage from my NSView (make snapshot) for drag&drop purposes.
It works, but draws in some different manner: darker and not so contrast as should be.
Why? Maybe because of NSGraphicContext different options? Need help and/or advice!
Here is the code for getting NSImage from NSView:
- (NSImage *)makeImageSnapshot {
NSSize imgSize = self.bounds.size;
NSBitmapImageRep * bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
[bir setSize:imgSize];
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
NSImage* image = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[image addRepresentation:bir];
return image;
}
And here are the images to compare visually:
Normal - drawn by drawRect usual call: http://cl.ly/image/213C1Y1V0v2H
Bad - captured into NSImage: http://cl.ly/image/183q442S2J14
Thought the difference might seem very small, believe me - it is obvious while working with application. I don't undertand why that is happening. And hope someone can help...
Thanks in advance!
I believe the transparency problem is caused by not having other things to blend it against when it's cached on its own.
Try -dataWithPDFInsideRect: or -dataWithEPSInsideRect: and get your image reps from there.

Sprite Sheet Issues?

I created sprite sheets with Zwoptex and generated the .plist file. I am having issues linking these sprite sheets to my iOS game. I loaded the .plist and .png files created on Zwoptex to Xcode, but the changes are not being recognized. I don't know why this is happening. The game was created with cocos.
Thanks!
Here is an example of how to use a spriteSheet in cocos2d im not sure if this is what your looking for as to the lack of detail but
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"TreeSequance.plist"];
CCSpriteBatchNode *treeSheet = [CCSpriteBatchNode batchNodeWithFile:#"TreeSequance.png"];
CCSprite *smallerTree = [CCSprite spriteWithSpriteFrameName:#"Tree1.png"];
smallerTree.position = ccp(500,500);
[treeSheet addChild:smallerTree];
[self addChild:treeSheet];

How to use sprite sheets effectively?

What I want to do:
I want to use sprite sheets to load all my enemies in the game. They would have to be removed once they are either destroyed by the good guy or when they go off screen. I have 6-7 enemies some of which are animations. I will be reusing them multiple times. I wan to load and unload them effectively from the memory.
What I am doing:
I first load the spritesheets:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"Obstacles.plist"];
CCSpriteBatchNode *obstaclesspriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"Obstacles.pvr.ccz"];
[self addChild:obstaclesspriteSheet];
I have a class called BadBoys which handles the bad guys. Every time I want to create a bad guy I create an instance of the class. Inside the class I create the sprite and add it to the layer.
baddies[x] = [[BadBoys alloc] init];
//INSIDE THE CLASS
baddie = [CCSprite spriteWithSpriteFrameName:#"cannonball-hd.png"];
I remove the sprite from the layer when it gets destroyed and then release the instance of the class.
[self removeChild:[baddies[x] getBaddie] cleanup:YES]; //getBaddie returns the sprite
[baddies[x] release];
I know this is a good way to do it. What I want to know is if this is the most efficient way of doing this? I thought of another way of loading the image:
Load the image asynchronously to the CCTextureCache.
Then create a sprite using the texture from the cache.
Add it to a NSMutableArray which will hold all enemies that are alive
Then when I dont need it anymore I can destroy it the following way:
CCTexture2D * texture = spriteName.texture;
[spriteName.parent removeChild:spriteName cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTexture:texture];
[backgroundSprites removeObject:spriteName];
Is this a better method? Please share your views and suggestions. Thanks.
What you're doing is good, as you said.
Your alternative is a terrible idea. I'll tell you why: you will want to avoid loading textures into memory and removing them from memory frequently during gameplay. Loading a texture from flash memory is slow. Removing the texture from memory also takes a little time, and is rather pointless if you have enough free memory available anyway.
Furthermore if you're using a spritesheet and you're removing one obstacle but try to remove the obstacle's texture (which is Obstacles.pvr.ccz) from memory as well, then the texture cache won't remove the texture from memory anyway. Because it's still being used by the spritesheet.
Lastly: premature optimization is the root of all evil.

AQGridview - Loading images from web (URL)

I'm new in iPad development. I'm using AQGridView to build my first ipad app. With the following code, I'm trying to load images from web URLs into each cell of the grid but, when I scroll down and come back up the images are not the same like they should, so they reload to display the good one.
Here's the code I use to load my images.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgPath]]];
dispatch_async(dispatch_get_main_queue(), ^{
_imageView.image = img;
[self setNeedsLayout];
});
});
I think the problem comes from here, when calling the dispatch_get_main_queue(). But I dunno how to fix that. If someone has an idea it would for sure help me!
Much thanks in advance!
Regards,
Apple has a demo of a clean solution to exactly this problem in LazyTableImages

Replacing image in sprite - cocos2d game development of iPhone

I want to change the sprite image.
Say for example:
mainSprite=[Sprite spriteWithFile:#"redFile.png"];
[self addChild:mainSprite];
Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.
If I change
[mainSprite setOpacity:150];
it works perfectly. But here I want to change the sprite image instead of opacity.
But I don't know how.
#sagar:
In cocos2d 0.99.x I use
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"sprite.png"]];
It works. The next question is, how can I get back to my previous sprite? Thanks
Ok. Damn Simple.
I find it by R & D.
Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:#"box-purple-dark.png"]];
[mainSprite setTexture:x];
Replace image in sprite :
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"image.png"]];
Replace image in sprite :
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:#"new-image.png"];
[mainSprite setTexture:tex1];

Resources