How do I add background in cocos2d? - iphone

I'm a total noob. Starting my first app. I'm using cocos2d and there's isn't a .xib file so how do I add a background image behind the sprites I have in my scene?

In Cocos2d you don't use xibs you have to place all the objects by code. Check the samples that come with Cocos2d to get a sense of how it works.
If you already know how to add sprites, adding a background is done the same way, it would be just another sprite just add it with a lower z-order.
[self addChild:myBackground z:0];
"self" would be the actual CCLayer
"myBackground" the sprite you created with the bakcground iamge.
and "z" would have to be lower than the z used for the other elements.

Related

how to embeed multiple sprite sheet CCSpriteFrameCache animation in cocos2d

all
I want to embeed multiple spriteFrame cache using the following code.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"abc.plist"];
// Create a sprite sheet with the Happy Bear images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"abc.png"];
[self addChild:spriteSheet];
the problem is that after 1st animation is cover I want to do another animation through another ccsprite fram cache, and add another sprite sheet, but when I add another sprite sheet it will give me sigerbat error.
how can I do multiple animation after one animation cover then the second animation will start, remember that there are 4 CCSpriteFrameCache file (ie,. 4 plist file)
All CCSprite added to a CCSpriteBatchNode must use the same texture. So what you're trying to accomplish is not possible because you're trying to add images from 4 different textures. If you check the Console (in Debug builds) you'll find a message stating something to that effect.
The solution: use one CCSpriteBatchNode for each texture (loaded via CCSpriteFrameCache plist). You'll have to write more code but that's the only way you can do it, apart from not using CCSpriteBatchNode at all.
You might want to do a performance test to check if you really, really need the CCSpriteBatchNode. For example, if you only display ONE sprite from the same texture on screen at any one time, you don't need a CCSpriteBatchNode. It's only improving performance if you have multiple (and many) sprites on screen and all are using the same texture.

How do I draw a new image without making a new UIImageView

I have a character in an iPod Touch game I am making that moves around the screen. That I have under control... However I want him able to shoot fireballs, and I don't want to have to make a new UIImageView for every fire ball he shoots. I have the image and everything... I think i need to use quartz core or openGL ES, but I can't make sense of the apple documentation.
Another option would be to implement drawRect in your view, and draw the fireballs there.

Playing 3D Animation in iPhone App (Possibly using a looping video)

Is there a relatively easy was to include a 3D animation into an iPhone app? We have the animations already made up for another project and our client has asked if they can be placed inside an iPhone app. We could perhaps include a low-res looping video of the animation (it's just a 3D component rotating on a single axis), or would it be better to look into getting the 3D animation directly onto a view?
Cheers,
Dan
You could either split it up into a set of frames and use the UIView animationImages property like so:
http://appsamuck.com/day2.html
Or assuming its already an OpenGL animation you could port the code to openGLES.
Here are good tutorials for openGL ES:
http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

The best way to remove a sprite from a batch in Cocos2D

I'm using Cocos2D in my project and I'm quite new to this library. And I don't understand one thing.
I have many sprites on the scene which are added and removed constantly. So at certain moment a sprite becomes useless and I have to remove it form a batch node.
In the comments of the removeChild method of CCSpriteBatchNode class is said:
#warning Removing a child from a CCSpriteBatchNode is very slow
Does anybody know what the best method of removing a sprite?
Thanks!
Instead of adding and removing sprites why not re use them, then you won't have any slow down caused by adding or removing sprites.
Setting a sprite not to be visible saves the render cost and when you need to add it again just move it to position and the texture frame if you need to and then turn the visibility back on.
I subclass a ccSprite and then add them to an array to keep track of active and inactive sprites.
Dave.

IPhone and Cocos2d Sprites/Layers

I am using cocos2d-iphone to place Sprites onto a Layer to setup a game playfield. At certain points in the game, certain Sprites need to be removed based upon game conditions. What I would like to do is setup an array of Sprite pointers, but I have two questions:
What's the best way to place Sprite pointers in an array?
How does one remove the Sprite in cocos2d with only a pointer to the Sprite? I know how to do it from its parent layer, but that is too runtime intensive for the main game loop.
Thanks in advance!
The Sprite class inherits from CocosNode, so you should be able to call spritePointer.parent.remove(spritePointer)
I figured it out. If anyone else is interested, the way to do it is to declare an array of Sprite pointers, such as:
Sprite * mySprites[10][10]; // assuming a 10x10 playfield where obstacles get placed
Then, when setting up your Sprites:
mySprites[0][0] = [Sprite spriteWithFile: #"obstacle.png"];
[myLayer add:mySprites[0][0]];
To remove the Sprite:
[myLayer remove:mySprites[0][0]];
There's also [mySprite removeFromParentAndCleanup:YES].