将子项添加到来自不同类 Cocos2d 的图层?
我正在制作一个游戏。我已经成功地在同一场景上使用了多个图层,但我希望进行碰撞检测。最简单的方法是使用一层。我不知道如何将子级添加到不同类别的图层中。帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在函数中传递想要将子级添加到的层。
像这样:
这是在你的班级中:
这是在主层中:
希望有帮助..
You can pass the layer you want the child added to in a function.
Like this:
This is in your class:
This is in the main layer:
Hope that helps..
如果你想添加一些CCSprite实例作为两个不同层的子层,这是不可能的,因为cocos2d总是检查你添加为子层的对象是否没有附加到另一个对象。但是如果您的问题是某些精灵之间的碰撞检测,则不需要它们成为同一对象的子级,您可以计算每个精灵的绝对位置(我猜
[spriteboundingbox]
会这样做)然后检查两者是否碰撞,您可以使用box2d或chipmunk为您进行碰撞检查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