IOS Game template collision detection details
We will set the physical body for collision detection (we call this physical body xx below)
Xx. physicsBody = SKPhysicsBody (rectangleOfSize: CGSize (x: 100, y: 100 ))
Then set an identifier for the physical body (BitMaskType is a new class)
Xx. physicsBody ?. CategoryBitMask = BitMaskType. xx
Finally, set the physical event that will crash (for example, the event in response to a collision with yy)
Xx. physicsBody ?. ContactTestBitMask = BitMaskType. yy
Add code
Func didBeginContact (contact: SKPhysicsContact ){
Println ("Collision ")
}
We can find that there is indeed a collision (the above rough description, if you have any questions, please Baidu)
Sometimes we will find that the two physics questions have not yet collided, but the system has responded to the collision event. At this time, we may wish to set the size of the physical body to a smaller value.
Sometimes the two physical bodies have already collided, but the system has not responded to the collision event. Why?
Please pay attention to our first code, that is, the sentence for setting the physical body size. In this sentence, we set a physical body of 100*100, however, the center is not described, and the default center is CGPoint ).
As shown in:
A Red Square indicates that you want to create a physical body. In fact, the physical body created by the system is a yellow square. The physical body is not displayed on the screen, but it is actually a collision. Why is this happening? The reason is that Xcode creates a 100*100 square in the center at the lower left corner of the coordinate by default. To solve this problem, we modify the code for creating the physical body.
Xx. physicsBody = SKPhysicsBody (rectangleOfSize: CGSize (x: 100, y: 100), center: CGPoint (x: 0.5, y: 0.5 ))
Okay, in the appeal code, we created a 100*100 square for ZTE with coordinates (50, 50), that is, the Red Square we imagined.