Animating a Sprite with a Sprite Sheet gives an uknown error - animation

I am trying to get a sprite to animate itself forever. There are no problems, and it builds fine. I get past the menus and when I click on the scene that has my sprite that I want to animate on it, it crashes. I am using the following code for my animation:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"sprite_fly.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"sprite_fly.png"];
[self addChild:spriteSheet];
NSMutableArray *flapAnimFrames = [NSMutableArray array];
for(int i = 1; i<=6; ++i) {
[flapAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"Fly%d.png"]]];
}
CCAnimation *flapAnim = [CCAnimation animationWithFrames:flapAnimFrames delay:1];
CGSize winSize = [CCDirector sharedDirector].winSize;
fly = [CCSprite spriteWithSpriteFrameName:#"fly1.png"];
fly.position = ccp(winSize.width/2, winSize.height/2);
flapAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:flapAnim restoreOriginalFrame:NO]];
[fly runAction:flapAction];
[spriteSheet addChild:fly];
I think that the problem is to do with the first line of code, CCSpriteFrameCache, but I can't see anything wrong with it. Please help, or give me another way to animate my sprite.

I think this is the best example for animation in cocos2d.click here.
I am not sure but i think the problem is with your sprite images or image name. check your image name in the plist file. Make sure that all images have same size which you are using for your animation.
Hope that will help you.

Related

free up memory after scene transition

i searched for a few hours but i can´t find what i´m actually searching for..
i want to free up my memory when im switching from a gameScene to my menuScene.
as i read, it is possible if i add a viewController for each Scene and perfom Segues instead of transitions. but i want to keep it simple and use just one viewController. in addition to the first solution, it is possible to use UIImage´s instead of SKtexture. UIImage`s frees up their memory automatically by transition so i tried, and tried and i ended up here: (obviously not working)
//Load Animations
barrierUfoAtlas = [SKTextureAtlas atlasNamed:#"BarrierUfoAnimation"];
[barrierUfoAtlas preloadWithCompletionHandler:^{
NSArray *barrierUfoAtlasNames = [barrierUfoAtlas textureNames];
NSArray *sortetBarrierUfoAtlasNames = [barrierUfoAtlasNames sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSMutableArray *barrierUfoTextures = [NSMutableArray array];
for (NSString *filename in sortetBarrierUfoAtlasNames) {
NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:[barrierUfoAtlas textureNamed:filename]];
UIImage *image = [UIImage imageWithData:imageData];
SKTexture *texture = [SKTexture textureWithImage:image];
[barrierUfoTextures addObject:texture];
}
self.barrierUfoAnimation = [SKAction animateWithTextures:barrierUfoTextures timePerFrame:0.0389];}];
can somebody help?

UIImage Animation Reverting Back To Original Position

Im pretty new to coding but I'm starting to get the hang of the basics.
how to make an image stay in its new position after an animation?
Example:
I'm giving an animating object a random position, however, the animation causes the object not to animate at the random position, but instead animate at the position it was given in the view controller. This also happens when I animate a completly different object.
Code I used:
int Random1x;
int Random1y;
IBOutlet UIButton *Start;
IBOutlet UIImageview *Object2;
-(void)ObjectMoving;
-(void)Object2Animate;
-(IBAction)Start:(id)sender{
[self ObjectMoving];
[self Object2Animate];
}
-(void)Object2Animate {
Object2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed: #"3.png"],
[UIImage imageNamed: #"4.png"],
[UIImage imageNamed: #"1.png"], nil];
Object2.animationDuration = .5
[Object2 setanimationRepeatCount: 0]
[Object2 startAnimating];
}
-(void)ObjectMoving {
Random1y = arc4random() % 466;
Random1y = Random1y + 60;
Random1x = arc4random() % 288;
Object2.center = CGPointMake(Random1x, Random1y);
}
I'd greatly appreciate help, thank you!
If you go to your story board file and click on the View Controller and then file inspector you will see a box for Auto Layout
Post back if that worked.
If you do need to use auto layout then you would have to figure out a different way of moving the image.

setting sprite display duration

The delayperunit property in my code below keeps the sprite on screen for 2.5 seconds before it changes. What I actually want to do is display the sprite for 0.5 seconds so that there is 2.0 second break (no animation displayed) before the next one appears for another 0.5 seconds and so on. How can I achieve this?
// Create the intro image
CGSize screenSize = [CCDirector sharedDirector].winSize;
CCSprite *introImage = [CCSprite spriteWithFile:#"intro1.png"];
[introImage setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:introImage];
// Create the intro animation, and load it from intro1 to intro7.png
CCAnimation *introAnimation = [CCAnimation animation];
[introAnimation delayPerUnit:2.5f];
for (int frameNumber=0; frameNumber < 8; frameNumber++) {
CCLOG(#"Adding image intro%d.png to the introAnimation.",frameNumber);
[introAnimation addSpriteFrameWithFilename:
[NSString stringWithFormat:#"intro%d.png",frameNumber]];
}
I dont think you can do that with a CCAnimation. You could either embed that in a class (if this will be a recurring theme), or do this in your module that displays these frames:
in .h, some ivars;
NSUInteger _currentFrame;
NSUInteger _maxFrames;
in .m, where you are ready to begin:
_currentFrame=1;
_maxFrames = 8;
[self scheduleOnce:#selector(flashFrame) delay:0.];
and
-(void) flashFrame: {
NSString *spriteName = [NSString stringWithFormat:#"intro%d.png",_currentFrame];
CCSprite *sprite = [CCSprite spriteWithFile:spriteName];
CGSize screenSize = [CCDirector sharedDirector].winSize;
sprite.position=ccp(screenSize.width/2, screenSize.height/2);
id wait = [CCDelayTime actionWithDuration:.5];
id clean = [CCCallBlock actionWithBlock:^{
[sprite removeFromParentAndCleanup:YES];
}];
id seq = [CCSequence actions:wait,clean,nil];
[self addChild sprite];
[sprite runAction:seq];
if(_currentFrame < maxFrame) {
_currentFrame++;
[self scheduleOnce:#selector(flashFrame) delay:2.0];
} else {
// do whatever you wanted to do after this sequence.
}
}
standard disclaimer : not tested, coded from memory, mileage will vary :)
Consider having an action composed by a CCSequence of CCAnimation (the one with 0.2 delay per unit) and add a CCDelayTime of 0.5 secs in between the animations. Then Repeat the action forever. Also, use CCAnimationCache to store your animations if you are using Cocos2d 2.x onwards.

Images are not displayed and showing error like "cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha"

I am making an application using Box2D in which i am getting images from Asset Library and displaying them as sprites.
here is the code which i have done :
Getting Images from asset library :
CGImageRef imgRef = [[mutArrAssetPhotos objectAtIndex:i] thumbnail];
Creating Texture2D :
CCTexture2D *spriteTexture = [[CCTexture2D alloc]initWithCGImage:imgRef resolutionType:kCCResolutionUnknown];
Creating sprites from textures :
CCSprite *paddle = [CCSprite spriteWithTexture:spriteTexture];
This gives me warning in console like :
"cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha"
Still in simulator it works fine though warning but in device images are not being displayed.
But instead if i used :
CCSprite *paddle = [CCSprite spriteWithFile:#"img.png"];
it is working fine and is not giving any warning also.
Can anyone help please ?? Thanks in Advance.
I've solved it.
Somehow i managed to recreate the image, from which i was creating sprite.
CGImageRef imgRef = [[mutArrAssetPhotos objectAtIndex:i] thumbnail];
UIImage *image = [self updateImage:[UIImage imageWithCGImage:imgRef]];
imgRef = image.CGImage;
CCSprite *paddle = [CCSprite spriteWithCGImage:image.CGImage key:[NSString stringWithFormat:#"%d",i]];
And for updating the image, i redraw it so that i do not get any alpha warnings.The update method is like this :
-(UIImage *) updateImage:(UIImage *)image
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.frame = CGRectMake(0,50, image.size.width,image.size.height);
image = [self captureImageFrom:imgView];
[imgView release];
return image;
}
-(UIImage *) captureImageFrom:(UIImageView *)view{
UIGraphicsBeginImageContext(view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenShot;
}
I think (could not find docs to confirm tough) that the thumbnail changes the image format so you get a plain no alpha image ref.
Try using the following for creating the right image ref :
CGImageRef imgRef = [[[mutArrAssetPhotos objectAtIndex:i] defaultRepresentation] fullResolutionImage];

Cocos2D - When I animate second sprite, first stops animating

So the issue here is when I create one animated projectile, everything is fine. As soon as the user creates a second, the first stops animating.
Alright, here's how I'm setting it up in my init:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
#"acorns.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"acorns.png"];
[self addChild:spriteSheet];
NSMutableArray *flyingFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i) {
[flyingFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"acorn%d.png", i]]];
}
CCAnimation *flying = [CCAnimation
animationWithFrames:flyingFrames delay:0.5f];
self.flying = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:flying restoreOriginalFrame:NO]];
Then, in my create bullets method which is called when the user taps the screen, I do:
CCSprite *bullet = [CCSprite spriteWithSpriteFrameName:#"acorn1.png"];
bullet.position = CGPointMake(140.0f, FLOOR_HEIGHT+145.0f);
[bullet runAction:_flying];
[self addChild:bullet z:9];
[bullets addObject:bullet];
So the first time the user taps, everything works fine. The second time, an animated bullet is created, but the existing one stops animating, and so on.
I believe each sprite should have its own actions, ie you cant reuse an action while the action is in progress. Something like:
CCSprite *bullet = [CCSprite spriteWithSpriteFrameName:#"acorn1.png"];
bullet.position = CGPointMake(140.0f, FLOOR_HEIGHT+145.0f);
id _fly=[CCAnimation animationWithFrames:flyingFrames delay:0.5f];
id _flyForever = [[CCRepeatForever _fly];
[bullet runAction:_flyForever];
[self addChild:bullet z:9];
[bullets addObject:buller];
The flyingFrames can be referenced by multiple animations, so you must take care of retaining the array for reuse.

Resources