Change CCSprite images at runtime - iphone

need to change the CCSprite images at runtime, i have to delete the previous loaded images and display new after a specific inter of NSTimer, i am using [imagesContainer removeChildByTag:0 cleanup:YES]; but not working
any help will be highly appreciated

You could remove the sprite like you're doing and add a new sprite but remember to add it to the layer with addChild:
CCSprite *theNewSprite = [CCSprite spriteWithFile:#"newImage.png"];
[LayerYouWantToAdd addChild:theNewSprite]
Or just change the texture of the sprite. This works for me.
[theSprite setTexture:[[CCSprite spriteWithFile:#"newImage.png"]texture]];

Related

Transparent layer used in Tiny wings

please can anyone show me how to implement using cocos2d, a half transparent layer that is tapped for game start like that obtained in the game called Tiny wings.
Thanks.
Add a CCSprite to the scene then set the opacity to something less than 255 to make it translucent.
Here is some psuedo-code:
CCSprite* myTranslucentImage = [CCSprite spriteWithFile:#"touchMe.png"];
myTranslucentImage.opacity = 128;
[scene addChild: myTranslucentImage];
I've never played Tiny Wings but I do know how to make a CCSprite transparent.
CCSprite *example = [CCSprite spriteWithImage:#"HelloWorld.png"];
[example setOpacity:160];
If you must use a CCLayer you should look at the Cocos2d documentation.

PNG Transparency in cocos2d iphone issue

I've just begun cocos2d development on the iphone. An issue I'm having is that when I add a CCSprite to my scene, instead of it being transparent around the edges of the .png file, its showing up as white.
So for example, I define the CCSprite in my header file -
CCSprite *foo;
Then when I initialise my scene -
foo = [CCSprite spriteWithFile:#"foo.png"];
[self addChild:player z:0 tag:1];
Is there anything wrong with this code?
Thanks for any help.
P.S. I have double checked my .png file in Photoshop and its correctly showing up as transparent where it should be.
Check the bottom portion of this page:
http://www.cocos2d-iphone.org/wiki/doku.php/faq#my_png_doesn_t_look_like_in_photoshop

UIimageView problem

Hi Friends'
i am new to using cocos2d application , and i want to build a grid of images using a single square image.
i make that in UIImageview now i want to ask that how can i add that...
if i use [self addchild: (CCNode)square] it show error .
thanks for any help in advance
You do not use UIImageView's with cocos2d.
Try this.
CCSprite *square = [CCSprite spriteWithFile:#"yourimage.png"];
[self addChild:square];
I'm presuming self is a CCLayer.
Sounds like you need up on some basics.
There are some good beginner tutorials here.
http://www.learn-cocos2d.com/

add animation in cocos2d

how can I add animation in cocos2d in a sprite, the problem is that whenever the use fire the object and hits that object, the object will remove just like an animation .. if you have any tutorial then plz send it to me...
thnx
You can use spritesheets of images and then do animation using the images.
For code examples you can try the following tutorial.
http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d
There are a lot of examples coming with cocos2d. Including animation using a set of images. Take a look at SpriteTest example - it's what you are looking for
just use this
CCSpriteSheet *mgr = [CCSpriteSheet spriteSheetWithFile:#"yourImage.png" capacity:5];
[self addChild:mgr z:0 tag:4]; //set the image and load it to the CCLayer
sprite = [CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0, 0, 30, 30)];
[sprite runAction:[CCFadeTo actionWithDuration: 1 opacity:80]];

Cocos2d adding a background image to a layer?

I just finished reading some of the cocos2d documentation, and as far as I understand to add a background image to a layer you have to do something like:
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[layer addChild:bg z:0];
Allthough as far as my testing goes you can just directly add the sprite to the scene like this:
#implementation IntroScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[bg setPosition:ccp(240, 160)];
[self addChild:bg z:0];
[self addChild:[MenuLayer node] z:1];
}
return self;
}
What is the difference between this 2 options, and what is the better way to set a background for a layer?. Any help will be greatly appreciated.
You do not need to have a layer, but they are handy. Think of the layers as a way of grouping sprites (both visually and organizationally). You may want your background images grouped as a layer and all the enemies in a game as another layer. This allows you to deal with each set more cleanly. e.g. You can remove a specific layer instead of finding all of the enemies and removing them.
If it helps, play with layers in Photoshop or a graphics editing program. Layers in cocos2d work similarly.
For a simple scene, adding the background to a layer or to a scene won't make much difference. But it doesn't take long before the benefits of layering kicks in.
Also layers control drawing order (over and above any inter-layer z ordering).
In geoSpark, for example, I have a background layer, a spark layer, a UI layer, etc. (Simplified). But this allows me to make sure my UI stuff is always above any game stuff (the spark layer) and all that is above the background.
If your game does only additive rendering, draw order won't matter much. :)
-- David
scene is a blackboard, layer is a page... news is wrote on page, but, if you want you can write on the board. If you want erase all new.
In cocos2d we can add any node on any node. But it depends on level of hierarchy.
You can add Layers on Scenes. If you want same property or look for all layers that you add on a CCScene you can add those in CCScene (as in your second case). but if you want to have different feature on each layer add those to CCLayer.
CCLayer is meant for grouping a set of nodes/sprites for some purpose. For example you can add a layer which includes options after pausing a game, or HUD layer for displaying som kind always on screen along with some scrolling layer (liken in some scrolling platform games).
Finally you can even CCNode on a CCScene, a layer on another layer, and even a CCNode on a CCSprite.
But the order of drawing in graphics context in cocos2d differs.
Hope you got the difference.
Good luck!