add animation in cocos2d - iphone

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]];

Related

Change CCSprite images at runtime

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]];

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.

How to add UITextView in CCLayer or CCScene in cocos2d-iphone

I want to add an instruction page where instruction written in UITextView. How to do it in Cocos2d. Because this is a game page and I want to take menu transaction effects of cocos2d like CCTransitionRadialCCW.
I wrote this code
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,60, 300,360)];
textView.backgroundColor = [UIColor clearColor];
textView.text = #"I am First enemy";
[textView setEditable:NO];
[[[CCDirector sharedDirector]openGLView]addSubview:textView];
But there is problem that it is added to the main CCDirector page, while I am writing this code
// [[[CCDirector sharedDirector]openGLView]addSubview:textView];
// replaced by :-
[self addChild:textView];
It gives me error. Please tell me how to add UITextView in CCScene or CCLayer.
I know that [self addChild:(CCNode *)node];
addChild: method needs CCNode so please tell me that how can typecast or convert UITextView object to CCNode.
If there is some alternate option for that please tell me .Like CCLabelTTF is alternate for UILabel, is there alternate for UITableView in Cocos2d-iphone.
Thanks in advance
Have a look at CCTableView: http://www.cocos2d-iphone.org/archives/943.
Combining UIKit and Cocos2D in such a way that you have the interleaving you have mentioned is likely to be very difficult, because everything done in Cocos2D is drawn in one OpenGL view. When you add a UIView, you can only add it in front of or behind Cocos2D's view.
No matter which way you're adding the UIView to cocos2d, it will not be animated by the cocos2d transition effects because simply speaking the UIView objects exist outside the OpenGL view that cocos2d uses (and animates in the case of transitions).
Adding UIViews to a cocos2d view as in your first example code is correct. But in order to position the view like a node, you should consider using the CCUIViewWrapper.

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/

iPhone Dev: Animating PNG Sequences

What is the best or recommended technique for animating PNG Sequences.
Heres what I've learned:
Do it Manually
Using MutableArrays containing Strings, you can animate a UIImageView with a timer which increments an index number
UIImage - animation methods
This works, the only problem is to find if an image has completed its animation, you have to check the isAnimating BOOL, and to do that you need a timer.
What is the best and recommended?
Looking to do Oldschool game sprite animations,
ie:
Idle Animation
Attack Animation
Walk Animation
ect...
Let me know if any one has something.
#lessfame
Example to animate arrows
UIImage* img1 = [UIImage imageNamed:#"fleche1.png"];
UIImage* img2 = [UIImage imageNamed:#"fleche2.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2, nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)];
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:100];
[imageView setAnimationDuration:3.0];
imageView.center = myView.center;
[imageView startAnimating];
[myView addSubview:imageView];
[imageView release];
Easiest way is to use Core Animation layers to do sprite animation:
Make a 'multi-cell' image strip (could be PNG or JPG) of all the various moves for the sprite. Make each 'cell' be a fixed height or width.
Put the image in a UIImageView.
Take the CALayer of the view, and adjust the contentsRect property of the layer. This acts as a clipping rectangle for the sprite. To animate sprite all you have to do is move the contentsRect over to the next cell position in the image strip.
Something like this will do it (assuming you've already calculated the newSpritePosition.
[CATransaction begin];
[CATransaction setDisableActions:YES];
spriteLayer.contentsRect = newSpritePosition;
[CATransaction commit];
(If you don't do the second line, then you'll get a default slide animation instead of a 'jump' to the next state.)
With this technique you can rotate through all the frames of the sprite -- much like a flipbook. CALAyer transactions are optimized so you should get pretty fast frame rates. You can set an animationDidStop completion handler to move to the next state of the sprite or loop back to the beginning frame.
It depends on how you are going to use the sprites. If you just need a simple looping sprite then the UIImage method is great. If you want more granular control then you will be happier loading the images into an array and cycling them using a NSTimer to handle the timing. Personally I use the array method most often because it leaves me more options in the future (like detecting when animations have completed). One more suggestion would be to check out the cocos2d project. Its sprite is alot more powerful than either of these suggestions.
As I said in another answer, I found this to be a good way: PNG Animation method by Moses DeJong
From his words:
This example implements an animation oriented view controller that simply waits to read the PNG image data for a frame until it is needed. Instead of alllocating many megabytes, this class run in about a half a meg of memory with about a 5-10% CPU utilization on a 2nd gen iPhone.
I'm still unsure exactly how it's done. I believe it basically uses UIImageView to cache up AVAudioPlayer.