IOS Game template collision detection details

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.