How to load sprites in cocos2d correctly? - cocos2d-iphone

my problem is:
I'm making a game for iOS using cocos2d and this game has lots of levels, so I'll have to create a loading scene to load my sprites for each level. ( like new backgrounds, monsters and other stuff )
But I have no idea about this, I'm adding all the Texture Packer Files (.plist and .pvr.ccz)
on the sharedSpriteFrameCache in the GameData.m.
Does anyone knows a good tutorial for this or can help me solve this?
Thanks!

So basically you want to know how to load and unload images as you see fit. How about
#implementation Level1
- (void) loadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Add the sprite frames. This will load the texture as well
[frameCache addSpriteFramesWithFile:#"monkey.plist"];
[frameCache addSpriteFramesWithFile:#"player.plist"];
[frameCache addSpriteFramesWithFile:#"enemy.plist"];
// Load other textures that are going to be used
_myBackgroundTexture = [textureCache addImage:#"background.png"];
}
- (void) unloadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Remove textures
[textureCache removeTexture:_myBackgroundTexture];
// Remove sprite frames. This will load the texture as well
[frameCache removeSpriteFramesFromFile:#"monkey.plist"];
[frameCache removeSpriteFramesFromFile:#"player.plist"];
[frameCache removeSpriteFramesFromFile:#"enemy.plist"];
// Though normally, id use frameCache removeUnusedSpriteFrames and
// textureCache removeUnusedTextures
}
...
#end

You can use the sprite by CCMenuItem and also by the Menu as you would require to click and move to that particular level.. The below is the code for adding the menu item image
CCMenuItem *m4 =[CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
selectedSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
disabledSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
target:self selector:#selector(MoveLeft)];
The above code gives the information you can use to display the sprite for the particular state of the menu. And the "MoveLeft" is the method selector which I have used to call the particular method.
At last you can add the m4 object to the CCMenu and get the desired output...
Hope it works for you.

Related

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.

Cocos2d animations in main loop

Anyone that can give any hint of the smartest way to do a main loop animation? I don't want to use CCAnimation because I want to control the animations frame by frame.
Shall I store the sprite rect (relative to the sprite sheet) for each individual frame in an array, and then look up the suiting rect in each animation step? I tried to find out how this is done in CCAnimation, but I didn't succeed...
How to get the rect for each frame at initialization?
How to set the rect at each animation step?
Do I need to use CCSpriteBatchNode? I guess not, eh?
Cannot crealry understand, why you don't want to use CCAnimation, but anyway, to get answer for your questions you can check creation code of the CCSprite instance. Then, check creation of CCSpriteFrame instance. There you will find the answer for at least your first question.
Actually if you just want to manage animation frames differently from CCAnimate, you can just store array of CCSpriteFrames and show them as you want(in CCAnimate action these frames are just changed one by one in equal time intervals).
And if you do not want to show more than one frame of your animation, there is no difference will you use CCSpriteBatchNode or not. It saves a lot of processor time if you need to draw several parts of one texture, as it draws them in one draw call instead of send draw message to all of these sprites.
As you want animate sprite frame by frame I think using CCSpriteBatchNode would be a better option as it give you frame by frame access of animation.Making plist of sprites using any tool like "Zwoptex" will give an efficient way to animate using CCSpriteBatchNode.
Hope you know the animation using plist file with CCSpriteBatchNode.
I did the following with inspiration from Morions answer:
In the game tick function:
_animationFrames.legFrame = (_animationFrames.legFrame + 1) % _animationFrames.legFrames.count;
[_legs setDisplayFrame: [_animationFrames.legFrames objectAtIndex: _animationFrames.legFrame]];
And in the init function:
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"Player.png"];
[self addChild:spriteSheet];
_animationFrames.legFrames = [[NSMutableArray array] retain];
for(int i = 0; i <= 15; ++i)
{
[_animationFrames.legFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"Player_legs-%d.png", i]]];
}
_legs = [CCSprite spriteWithSpriteFrameName:#"Player_legs-0.png"];
[_sprite addChild: spriteSheet];
[spriteSheet addChild:_legs z:1];

Issues with adding an array of moving sprites to screen?

In my program, I have created an NSMutable Array which contains a series of sprites that move across the screen (to the left)
However, while implemented in my code, they do not seem to appear on the screen simulator when tested.
Any ideas?
-(void) addBlocks
{
NSMutableArray *_blocks; blocktest=[CCSprite spriteWithFile:#"blocksquare.png"];
blocktest.tag = 1;
// add the block to the array of blocks
[_blocks addObject:blocktest];
blocktest.position=ccp(500,100);
id repeat2 =[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:7 position:ccp(-180,100)],nil]];
[blocktest runAction:repeat2];
}
That's because you haven't added blocktest to your scene ( [self addChild:blocktest] ).
Adding it to _blocks, will work as a reference to fetch your sprites later, but will not add the sprites on any layer or scene, so you won't see them anywhere

Cocos2d show only a part of a CCSprite

Is there any possibility to show only a part of an CCSprite?
It seams that contentSize property doesn't have a good result.
I think you might have to create a new sprite for this. The general pseudo code is this.
CCTexture2D *origTexture = originalSprite->getTexture();
CGRect rect = {0, 0, 20, 20};
CCSprite *destSprite = CCSprite::spriteWithTexture(origTexture, CGRect);
Both doc_180's and James' answers work by creating new CCSprite using a portion of the texture, but if you are using clipping method, you will get CCSprite that uses the full texture but have the ability to only draw a portion of it on screen. One advantage of this method is you are able to modify how big or small the portion that you want shown or hidden on the fly rather than having to re-create the CCSprite again and again (or replacing the texture again and again).
So, to use the clipping method, simply download the ClippingNode class from here, and add the CCSprite you want clipped to that ClippingNode. Then you call one of its methods to specify which region to limit the drawing to. I'm currently using it to create a progress bar so I know for sure it works great.
Get the [sprite displayedFrame], change the frame of that, and create a new sprite with that spriteframe: CCSprite *sprite2 = [CCSprite spriteWithSpriteFrame:frame]

How to add animated background

i have a gif image and i want to put it in my iphone game background, the image is a moving scenery but when i put in my background its not moving ..How can i keep my background moving like the gif image?
FOr that you have to use the imageview and set its frame as background view.
Insert all your images used for creating GIF in Resource folder
Then use the following code
UIImageView *imgBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,480)];
NSArray *myImages = [NSArray arrayWithObjects:#"1.png",#"2.png",#3.png"#4.png", nil];
imgBackView.animationImages = myImages;
imgBackView.animationDuration = 0.5;// OR WHATEVER TIME YOU WANT
imgBackView.animationRepeatCount = 0; // 0 = loops forever
[imgBackView startAnimating];
[self.view addSubview:imgBackView];
[imgBackView release];
hAPPY iCODING...
As far as I know, animated gifs does not work in iPhone. However it is possible to have the same effect using animationImages property of UIImageView. Instead of setting single image, you can set array of UIImages as animationImages of the view. Then you can start the animation any time by calling startAnimating method.
Of course to have an array of images, you will have to extract the frames of the animated gif as different images and add them to your resources.