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