Swift game practice-Parkon 11 welcome to the physical world

Source: Internet
Author: User

 

Physical Simulation is a wonderful thing. This famous game has Angry Birds. In this section, we will take a look at how to set the gravity, set the physical surrounding body, and perform collision detection.

Key points:

Set the proxy for physical detection:

Let the main scenario follow the skphysicscontactdelegate protocol. The didbegincontact method is used to respond to collision between objects. Naturally, we need to set the proxy of the physical system as the main scenario.

 
Self. physicsworld. contactdelegate = self

Set gravity:

Self. physicsworld. Gravity = cgvectormake (0,-5)

Because we need to detect whether the pandatv falls out of the scene, set the surrounding body of the scene to the "edge" type.

 
Self. physicsbody = skphysicsbody (edgeloopfromrect: Self. Frame)

Set an identifier:

 
Self. physicsbody. categorybitmask = bitmasktype. Scene

Similarly, after setting the scenario class, we also need to transform the pandatv class, because collision is between two friends after all.

  //   set pandatv to a rectangle  self. physicsbody =  skphysicsbody (rectangleofsize: texture. size ()   //   physical effect  self. physicsbody. dynamic =  true  ///   No angle change  self. physicsbody. allowsrotation =  false  ///   set friction to 0  self. physicsbody. restitution =  0  ///   Set Identifier  self. physicsbody. categorybitmask =  bitmasktype. panda   //   Collision Detection with scenarios  self. physicsbody. contacttestbitmask = bitmasktype. scene 

Finally, we will go to didbegincontactWrite the Collision Response Code in

 
Func didbegincontact (contact: skphysicscontact!){//If the pandatv and the scene edge collideIf(Contact. bodya. categorybitmask | contact. bodyb. categorybitmask) = (bitmasktype. Scene |Bitmasktype. Panda) {println ("Game ended")}}
Project file address

Http://yun.baidu.com/share/link? Consumer id = 3824235955 & UK = 541995622

 

Swift game practice-game preview of the parkfun pandatv series 00 01 create a project import materials 02 create a pandatv 03 animation 04 create a pandatv hop and roll action 05 on the pedaling platform 06 platform and platform factory 07 platforms move 08 to generate a steady stream of mobile platforms 09 remove Platform 10 parallax rolling backgrounds out of scenarios

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.