Collision detection leading to color detection? [duplicate]

和自甴很熟 提交于 2019-12-08 09:34:32

问题


How can I check if, when I collide with an SKSpriteNode in the middle of two rectangles with a ball, the ball and the two rectangles are the same color?

For example, if my two rectangles are red, and my ball is blue, I want it to detect that the colors of my rectangle and the ball are different. If they are the same color, then I don't want anything to happen. If they aren't, I'll write the code that makes the player lose. But how can I detect the color difference when the ball collides the skspritenode located in the middle and in between the two rectangles?


回答1:


My suggestion is that your change your "pair" of walls to become a trio of walls instead. This third wall should have a different categoryBitMask. (PhysicsCategory.wallSpace seems to fit with your current naming scheme.)

This "wallSpace" needs to be positioned between the two existing walls. It should be given the same color as its siblings through your color-change logic, but here comes the trick: set it's alpha to 0.

This way you can check for collisions between this invisible wall and your ball and perform actions based on the color-information.



来源:https://stackoverflow.com/questions/37849475/collision-detection-leading-to-color-detection

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