How can I test sprite collision in cocos2d?

瘦欲@ 提交于 2020-01-01 12:25:08

问题


How do I start to implement a class for sprite collision?


回答1:


As Eric pointed out, CGRectIntersectsRect is the method to test two bounding rects for overlapping. Use the boundingBox method of the CCNode classes to get the correct bounding box for each sprite (or other node).

See my answer here: Collision Detection in Cocos2d game?




回答2:


I would look into b2ContactListener

You can do some searches on that, and you'll easily get some results




回答3:


You can also perform very simple collision detecting by checking if two rectangles intersect. You can use the CGRectIntersectsRect function to do this. Basically, both rects are based on the position and dimensions of sprites you want to check for collisions. If CGRectIntersectsRect returns true, you know a collision occurred.

More info here: [http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/][1]




回答4:


You want to use the chipmunk's physics engine, which is part of the cocos2d engine.

Example (iPhone)



来源:https://stackoverflow.com/questions/1338569/how-can-i-test-sprite-collision-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!