Collision detection leading to color detection? [d

2019-06-10 20:16发布

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条回答
叛逆
2楼-- · 2019-06-10 20:55

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.

查看更多
登录 后发表回答