Reputation: 778
I am making a game. I have been successful with more than one layer on the same scene, but I want there to be collision detection. And the simplest way to do this is to have ONE layer. I don't know how to add a child to a layer from a different class. HELP!
Upvotes: 0
Views: 1912
Reputation: 969
You can pass the layer you want the child added to in a function. Like this:
This is in your class:
-(void)makeChild:(CCLayer*)layer {
CCSprite *sprite =..**** //Create sprite
[layer addChild:sprite];
}
This is in the main layer:
-(void)init {
[classInstance makeChild:self];
}
Hope that helps..
Upvotes: 4
Reputation: 3411
if you want to add some CCSprite instance as two diffrent layer's child, that's impossible since cocos2d always check if the object you are adding as a child doesn't attached to another object. but if your problem is collision detection between some sprites it doesn't need them to be child of the same object, you can calculate absolute position of each of them (i guess [sprite boundingbox]
does so) and then check if those two collide, you can use box2d or chipmunk to do collision checkings for you
Upvotes: 0