I created a Thread like the below one:
[NSThread detachNewThreadSelector:#selector(connectionFinishedThread) toTarget:self withObject:nil];
inside this method, i created one sprite and given animation for this sprite. Animation not visible.
My code inside the Thread method:
CCSprite *aniSprite = [CCSprite spriteWithSpriteFrameName:#"r_anim01.png"];
aniSprite.position = ccp(50, 50);
[self addChild:aniSprite z:22];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"r_anim1.plist"];
CCSpriteBatchNode *animSheet = [CCSpriteBatchNode batchNodeWithFile:#"r_anim1.png"];
[self addChild:animSheet];
NSMutableArray *animFrames = [NSMutableArray array];
for (int i=1; i<=6; i++) {
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"r_anim1%d.png",i]]];
}
CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:0.1f];
CCAction *spriteAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:anim]];
[sprite runAction:spriteAction];
Why it behaves like that?
Most changes to properties of CCNode classes must be done on the main thread. Cocos2D does not support multi-threading like Sprite Kit.
For example changing a sprite's texture from a background thread will certainly crash. But even subtle issues can occur because all properties are declared nonatomic.
The only way to use threads with cocos2d is to make sure whatever logic runs in the background thread does not directly change a node's properties. Ie doing some AI calculations in the background is fine, as long as AI actors aren't nodes but custom classes.
You should not try to manipulate cocos2d objects (e.g. CCNode derived object) from another thread. The container holding the object (CCLayer, CCScene, etc.) may be manipulating it at the same time and none of the typical concurrency mechanisms (mutexes) are in effect.
If you need your sprite to take some kind of update action every time the frame updates, should schedule the container to update and update the sprite then. It is very common to move sprites around, update their orientations, etc., during the CCScene update.
Related
I want to apply the same effect to a number of different sprites in my iOS game so am looking in to a general approach rather than an animation created in another program and using its images to create a UIImageView animation.
This effect is an ‘explosion’ type animation where my sprite’s image gets chopped in to different pieces, then, using the physics engine, those pieces get exploded out in different directions before falling down.
I’m new to SpriteKit, but am assuming I have to do this in Quartz? Then re-add the new images as sprites and apply the animation?
I don’t really know where to start, let alone how to continue with the rest of the steps.
Does anyone have any ideas?
Thanks.
1) Upon initialization of said exploding object, first we create a SKTextureAtlas which holds all of the images being animated. You must have the texture atlas resources available.
2) Load all of the relevant SKTextures into an array. This array should be private to the object to which the explosion is happening.
A safe way to do this:
- (void)loadContactImages {
NSMutableArray *frames = [NSMutableArray array];
SKTextureAtlas *explosionFrames = [SKTextureAtlas atlasNamed:#"explosionFrames"];
for(int i = 0; i < explosionFrames.textureNames.count; i++) {
NSString *textureName = [NSString stringWithFormat:#"explosionFrame_%d" , i];
SKTexture *texture = [explosionFrames textureNamed:textureName];
if(texture) {
[frames addObject:texture];
}
}
self.contactFrames = frames;
}
3) You will then define a method which will animate the explosion. Look into the SKAction header for more info. Plug in time per frame that makes sense to your explosion
- (SKAction *)runExplosion {
return [SKAction animateWithTextures:self.contactFrames timePerFrame:0.1];
}
4) As for the exploding pieces, you should preload them, so to limit the overhead of sending the pieces into random directions (with this we won't have the extra overhead of creating all those new sprites at the time of the explosion). Don't forget to give them physics bodies (SKPhysicsBody) and set the isAffectedByGravity property to YES!
- (SKAction *)explosionOfPieces {
return [SKAction runBlock:^ {
for(SKSpriteNode *piece in self.explodingPieces) {
piece.hidden = NO;
piece.position = self.position;
[self addChild:piece];
[piece.physicsBody applyImpulse:CGVectorMake(dx , dy)]; //specify the direction here
}
}];
}
5) Bringing it all together, expose a method to return a sequence of these actions. If you would like to have these two actions occur together, there is also a class method on SKAction called [SKAction group: (NSArray *)]
- (void)explosionSequence {
[self runAction: [SKAction sequence:#[ [self runExplosion] , [self explosionOfPieces]]]];
}
Note: Physics reactions occur in SKScene's. We expose this action so the scene can run it on your object within the scene. You will also want to conform to a protocol called SKPhysicsContactDelegate within your scene and call:
[myExplodingObject explosionSequence];
within the delegate method:
- (void)didBeginContact:(SKPhysicsContact *)contact
Within this SKPhysicsContact object lie two colliding bodies. Both of these bodies ("exploding object" and "explosion trigger") must have their SKPhysicsBody property initialized and their contactTestBitMask property set (so this object knows what it can collide with). I would also set the categoryBitMask property so we can directly introspect the bodies' type. We reference these colliding bodies like this:
- (void)didBeginContact:(SKPhysicsContact *)contact {
if(contact.bodyA.categoryBitMask == explodingObjectCategory && contact.bodyB.categoryBitMask == explodingTriggerCategory) {
[self.myExplodingObject explosionSequence];
}
}
Please look into Apple's docs for more info.
So, here is my scene problem: I start from a Menu Scene, then go into the InGame Scene, and when the character is dead, I go to the Menu Scene again, all this using:
[[CCDirector sharedDirector] replaceScene:[MainMenu scene]];
and
[[CCDirector sharedDirector] replaceScene:[InGame scene]];
After losing the game, and trying to go back to the game, my SpriteSheet crashes with the error :
'CCSprite is not using the same texture id'
Here is how I init the animation :
- (void) initSprite:(NSString *)plist andTexture:(NSString *)texture_ {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:texture_];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i=1; i<=12; i++) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"%d.png",i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.05f];
texture = [CCSprite spriteWithSpriteFrameName:#"1.png"];
walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim]];
[texture runAction:walkAction];
texture.position = position;
texture.tag = HeroType;
[spriteSheet addChild:texture];
[self addChild:spriteSheet];
}
The crash occurs when I add the texture to the spriteSheet:
[spriteSheet addChild:texture];
I believe the problem comes from the deallocation of the texture..
I don't use ARC.
You probably have a "1.png" in the cache that corresponds to another animation that was created before quit-restart sequence. Maybe you want to purge the sprite frame cache (and possibly a whole lot of other things) prior to restarting.
I completely avoid "1.png" through 'NNNN.png' since in all likelihood all your animations will have them. Instead i use things like :
walk_classKey_upNNNN.png {up,down,left,right,idle,jump ... and any other map stance i need)
fight_classKey_strikeNNNN.png {strike,hurt,dead ... for example)
classKey is {fighter,rogue, ... etc ... whichever unique soldier type I have)
and i name my plists/textures the same
walk_fighter_up-hd.plist (using texture packer, the plist embeds the texture name).
fight_rogue_cheapShot-hd.plist (cheapShot is one of my rogue's skill in my current game).
When I didn't optimize animation,it worked well.But when I optimized my animation,the program crashed.And Xcode log said:
Assertion failure in -[CCSpriteBatchNode addChild:z:tag:], /Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/cocos2d/CCSpriteBatchNode.m:183
in CCSpriteBatchNode.m:183,there is
NSAssert( child.texture.name == textureAtlas_.texture.name, #"CCSprite is not using the same texture id");
here is my code
// cache
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:#"birdAtlas.plist"];
// frame array
NSMutableArray *framesArray=[NSMutableArray array];
for (int i=1; i<10; i++) {
NSString *frameName=[NSString stringWithFormat:#"bird%d.png", i];
id frameObject=[cache spriteFrameByName:frameName];
[framesArray addObject:frameObject];
}
// animation object
id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];
// animation action
id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
animAction=[CCRepeatForever actionWithAction:animAction];
// sprite
cocosGuy = [CCSprite spriteWithFile: #"Icon.png"];//cocosGuy is CCSprite,declared earler
cocosGuy.position = ccp( 200, 300 );
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:#"birdAtlas.png"];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
[cocosGuy runAction:animAction];
UPDATE:
here is the corrected code,and it works well
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:#"birdAtlas.png"];
[cocosGuy setTexture:[batchNode texture]];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
For this to work, your Icon.png texture should be in the birdAtlas.png texture, with appropriate declaration in the .plist. Batchnodes 1) are created with one texture (and only one), and 2) only accept as children sprites that are from the SAME texture.
... and , i dont know your intent, but typically you would have
CCSprite *cocosGuy = [CCSprite spriteWithSpriteFrame:[cache spriteFrameByName:#"bird1.png"];
in that case, i think the batch node add will work.
... and , not sure using a batch node will be of any consequence for an animation, if the texture only contains the animation frames for that one animation. Frames are displayed one at a time, so i dont think you will benefit from the batched draw call.
I was able to call those methods at run time on a CCSprite object, now that I decided to batch it in a CCSpriteBatchNode it doesn't seem to work anymore:
sprt = [CCSprite spriteWithSpriteFrameName:#"sprt.png"];
sprt.anchorPoint = CGPointMake(0.5f, 0.5f);
sprt.position = CGPointMake(40.0f, 60.0f);
[batchNode addChild:sprt z:-1]; // It used to work when I was simply "adding as child" the sprt object, I guess now doesn't set the order anymore because somehow the CCSpriteBatch node doesn't allow the re-ordering of child added to it
CCCallFunc *callback = [CCCallFunc actionWithTarget:self selector:#selector(moveBackwards)];
CCCallFunc *callback2 = [CCCallFunc actionWithTarget:self selector:#selector(moveForward)];
[sprt runAction: [CCRepeatForever actionWithAction: [CCSequence actions: callback2, callback , nil]]];
-(void) moveBackwards
{
[sprt setZOrder:-1];
}
-(void) moveForward
{
[sprt setZOrder:1];
}
If you have a CCSpriteBatchNode, you have to consider all the CCSprite to be on the same layer (the spritebatch node).
This means that sprites in a sprite batch node can change their z order relative to other sprites in the sprite batch node, but you can not change the z order to make a sprite-batched sprite appear behind or in front of another node that is not a child of the sprite's CCSpriteBatchNode.
I'm pretty sure this is the problem you've run into. If you have trouble grasping this, consider the CCSpriteBatchNode has the same behavior regarding z ordering as a CCLayer (respectively any other node but devs seem to be hung up on CCLayer as the only/main layering construct). Maybe that makes it easier to understand.
Just in case someone else runs across this issue, the following will work to reorder the sprite...
[self.parent reorderChild:self z:x];
I have an animated sprite using two pngs. The animation works fine. I have another method that is run when the game is over.
//Grey mouse with Pompom
greyMousePomPom = [CCSprite spriteWithFile:#"pink_mice_pom_anime_01.png"];
greyMousePomPom.tag=132;
[self addChild:greyMousePomPom z:6];
greyMousePomPom.position = CGPointMake(550, 70);
//Grey Pom Pom Mouse animation
CCAnimation *greyMousePomPomAnimate = [CCAnimation animation];
[greyMousePomPomAnimate addFrameWithFilename:#"gray_mice_pom_anime_01.png"];
[greyMousePomPomAnimate addFrameWithFilename:#"gray_mice_pom_anime_02.png"];
id greyMousePopPomAnimationAction = [CCAnimate actionWithDuration:1.3f animation:greyMousePomPomAnimate restoreOriginalFrame:NO];
repeatAnimationPomPom2 = [CCRepeatForever actionWithAction:greyMousePopPomAnimationAction];
[greyMousePomPom runAction:repeatAnimationPomPom2];
When I run my method to change the animated sprites texture and to stop them the animation continues behind the new texture.
-(void) changePomPomMiceToSadFaceForFreeFall
{
NSLog(#"making the mice sad");
[self stopAllActions];
[greyMousePomPom setTexture:[[CCTextureCache sharedTextureCache] addImage:#"gray_mice_pom_anime_03.png"]];
}
I know this method is working because it is NSLogging and the textures are changing. But why aren't the animations stopping? I have tried to remove it by tag and by declaring the action but no success.
I know there are a lot of people out there that are smarter than me.. can you help?
What you're doing right now is stop all animations added to the current node:
self
If you added any action to self, this command would be perfectly fine to stop all of them.
Instead what you need to do is, you need to call the stopAllActions method on the object you added the actions to:
[greyMousePomPom stopAllActions];
HTH