Assertion failure when adding a CCParticleSystemQuad as child to LHSprite - objective-c

I try to add a CCParticleSystemQuad as child to LHSprite:
player = [loader spriteWithUniqueName:#"player"];
NSAssert(player != nil, #"Couldn't find the player!");
// Particles
smokeParticles = [CCParticleSystemQuad particleWithFile:#"smoke.plist"];
smokeParticles.position = ccp(-30.0, 0.0);
[player addChild:smokeParticles];
But I keep getting this error message:
2012-12-29 22:51:44.373 MyProject[15396:15203]
*** Assertion failure in -[LHSprite addChild:z:tag:],
/MyPath/MyProject/libs/cocos2d/CCSprite.m:567
Adding the CCParticleSystemQuad to the CCLayer
[self addChild:smokeParticles];
works just fine.
CCSprite.m: Line 567
NSAssert([child isKindOfClass:[CCSprite class]],
#"CCSprite only supports CCSprites as children when using CCSpriteBatchNode");
Can anyone tell me why this happens?

The reason is given in assert message. It happens because CCSprite only supports CCSprites as children when using CCSpriteBatchNode. If you use CCSpriteBatchNode to draw a sprite, you can only add CCSprites as children to this sprite.

Inside LH in the Level Navigator - drag your sprite on top of the MAIN_LAYER - this will remove the sprite from the batch node and make it self rendered - now you will be able to add children to it via code. If you use a batch sprite, you can only add children to the sprite that have the same texture as the batch node (that is, uses the same image file)
for the LHParallaxNode - this does not add the particle/sprite to the node - it just moves it. So you should also add your particle to the layer
[self addChild:particle z:100];
then add it to the parallax node.
Bogdan Vladu the creator of LevelHelper says so..

Related

Stop Camera from rotating around a rotating parent

I have a rolling marble and would like my camera to follow it. My problem is that my camera is also rotating around my object as it is rotating.
Here's my code.
-(void) setupCameraOnMarble:(SCNNode*)marble
{
[cameraNode removeFromParentNode];
[marble addChildNode:cameraNode];
SCNLookAtConstraint *marbleStare = [SCNLookAtConstraint lookAtConstraintWithTarget:marble];
marbleStare.gimbalLockEnabled = YES;
cameraNode.constraints = #[marbleStare];
}
I'm thinking that a transform constraint should work but I don't understand how it works.
Are you sure you need to make cameraNode a child of marble?
Because according to SKNode docs: when a node’s coordinate system is scaled or rotated, this transformation is applied both to the node’s own content and to that of its descendants see SKNode
So i assume you need to un-child camera from marble. And to achive that followint the object behavior from camera, you may update camera coordinates somewhere in the code, that moves the marble
I have un-childed my camera to my sphere and made my class a SCNSceneRendererDelegate protocol and added this to the rendering loop:
-(void) renderer:(id)renderer updateAtTime:(NSTimeInterval)time
{
[cameraNode setPosition: SCNVector3Make(marbleNode.presentationNode.position.x, marbleNode.presentationNode.position.y + 5, marbleNode.presentationNode.position.z + 10)];
}
If I log the coordinates of the marble.presentationNode they are changing but my camera still doesn't move!

How to hide physical body's stroke in Sprite Kit

I have a game in which i have some sprites using physical body and it has a stroke around a physical body. How to hide it?!
Somewhere in your code, you might have set
self.view.showsPhysics = YES; //Within the scene
or
skview.showsPhysics = YES; //from the viewcontroller
The showsPhysics method tells the SKView to actually draw the physicsBodies on top of the nodes they are associated with. A description of the method is given in the documentation.
Either remove the above lines or set the showsPhysics property to NO in order to solve this problem.

SpriteKit: moving sprite infinitely cause the frame sometimes incontinuous

all.
I am now developing an ios game using sprite kit.The game mechanics include: infinitely scroll sprites from top to bottom.
My game scene holds just 3 sprites,top,middle,bottom.
I have dozens of sprite images,so my solution is dynamic create 3 sprites ( so the sprites can fill the whole screen),when the bottom sprite is off screen,then destroy it;when the top sprite
goes down the top frame,create a new sprite node,then exchange the bottom,middle,top sprite.
The pseudo code:
in the interface file:
- GameScene:SKScene
{
...
SKSpriteNode *_topSprite;
SKSpriteNode *_middleSprite;
SKSpriteNode *_bottomSprite;
...
}
in the implementation file:
- (void)update:(CFTimeInterval)currentTime
{
// 1 compute time interval
// 2 update sprite node
_topSprite.position move down 100*timeInterval
_middleSprite.position move down 100*timeInterval
_bottomSprite.position move down 100*timeInterval
// 3 crop the off-screen bottom sprite node
if (_bottomSprite is offScreen)
{
[_bottomSprite removeFromParent]
}
// 4 check the highest sprite position visible
// if the highest position is below the frame top,then create new sprite
_bottomSprite = _middleSprite;
_middleSprite = _topSprite;
_topSprite = [self createNewNode]; // random a sprite image
}
the game fps is 60,and the nodes counts is not increasing.
All seems good,but i found sometimes the game just suddenly has a very shot choke causing the moving frame not continuous.
In my instinct,I thought the main reason is caused by step 4.
But I don't have any idea to solve it.
Any suggestions will be appreciated.
Here is the createNewNode method:
- (SKSpriteNode *)createNewNode {
NSString *blockName = [self randomImageName];
SKSpriteNode *sprite = [[SKSpriteNode alloc] initWithImageNamed:blockName];
sprite.position = _highestPosition;
sprite.anchorPoint = CGPointMake(0.5, 0);
NSLog(#"putAnotherNodeOnTop %#", blockName);
[self addChild:sprite];
return sprite;
}
A couple of suggestions.
Since you are using the same three images, you don't need to re-create the sprites each time. It should be less expensive to set the hidden value to YES, move the sprite somewhere, set the texture and then show it again with hidden to NO. SpriteKit doesn't draw hidden nodes, or ones off screen.
Your code basically asks for a PNG on disk every frame in update when createNewNode: is called. It should be more performant to load the textures in memory and assign them to sprites as needed. If you have just a few, making them SKTexture instance variables would be an easy way to do it.
Use spritesheets (texture atlases), it is much faster to draw an image from memory at mapped coordinates than to load it from disk each time. For example with three images in a spritesheet this can be a single draw call. From disk this is three separate draw calls.
You are returning a sprite and adding it to the scene in the createNewNode: method. Maybe that is what you want for some reason. It stood out as possible duplicate effort.

Sprite kit how to fix moving the camera?

I tried to bind the camera on my player, with this code:
-(void)didSimulatePhysics {
[self centerOnNode: player];
}
-(void)centerOnNode: (SKNode *) node {
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - camerapositionInScene.y);
}
but nothing happens except of getting output like this:
Setting the position of a SKScene has no effect.
2014-03-22 16:55:35.604 Jelly Jumper [5801:90b] SKScene:
Setting the position of a SKScene has no effect.
2014-03-22 16:55:35.753 Jelly Jumper [5801:90b] SKScene:
Setting the position of a SKScene has no effect.
2014-03-22 16:55:35.907 Jelly Jumper [5801:90b] SKScene:
You will need to create a node that holds everything but for player and user interface in it.
For example, call it worldNode. Add it as child to scene and everything on screen but for player and ui to it. Add player and ui as children to your scene directly.
Now when you need to move the player, move worldNode instead. You can calculate the amount you need to move player in update method and apply negative of this to world node. This way it will work as you want.

Why isn't my CCParticleSystem Showing up Cocos2d?

This might be related not to the particle system but more on layering, but... i have the following code for the particle system:
CCParticleExplosion *explosion = [[CCParticleExplosion alloc] init];
explosion.texture = [[CCTextureCache sharedTextureCache] addImage:#"blah.png"];
explosion.position = ccp(100,100);
[explosion setAutoRemoveOnFinish:YES];
[explosion setTotalParticles:10];
[self addChild:explosion];
[explosion autorelease];
and it renders fine if i stick that code in like a "HelloWorld" scene. However, if i put the code under a CCLayer, and i add that CCLayer to the "HelloWorld" scene, the particles don't show up. Why is that? (i see the CCLayer just fine)
the only noticeable difference is that in my "HelloWorld" scene, i have a "world" object. and i even tried to add in [self scheduleUpdate] in my CCLayer.
in my CCLayer, i also have a blank update. am i supposed to do something here?
-(void) update:(ccTime)delta{
}
any help, including good general practice (esp memory management) would be greatly appreciated. (just starting out cocos/iOS dev) Thanks!!
Check that:
texture isn't nil
texture isn't too big (max 512x512)
texture has power of two dimensions (4, 8, 16, 32, etc)
duration is positive (otherwise it would auto remove the effect)
self.visible == YES
(self.position + explosion.position) == somewhere on the screen (explosion's position is added to self's position since its position is relative to its parent)