In this section, we will implement pandatv rollback. The idea is to record the Y coordinates of the pandatv when it starts, and record the Y coordinates of the pandatv when it falls onto the platform. If the difference between the two coordinates is greater than a certain value, it is determined that it will be rolled and buffered from the height. So far, the pandatv is like a game.
Key points:
YCoordinates:
var jumpStart = 0.0
YCoordinates:
var jumpEnd = 0.0
In didbegincontactMethod
panda.jumpEnd = panda.position.yif panda.jumpEnd-panda.jumpStart <= -70 { panda.roll()}
Overall code: the key parts of this section are highlighted in red and bold.
Gamescene:
Import spritekitclass gamescene: skscene, protocolmainscene, skphysicscontactdelegate {@ lazy var panda = Panda () @ lazy var platformfactory = platformfactory () @ lazy var BG = background () var movespeed: cgfloat = 15 var lastdis = 0.0 func didbegincontact (contact: skphysicscontact !) {If (contact. bodya. categorybitmask | contact. bodyb. categorybitmask) = (bitmasktype. Platform | bitmasktype. Panda) {panda. Run ()Panda. jumpend= Panda. position. Y if panda. jumpEnd-panda.jumpStart <=-70{Panda. Roll ()}} If (contact. bodya. categorybitmask | contact. bodyb. categorybitmask) = (bitmasktype. Scene | bitmasktype. Panda) {println ("the game is over! ")} Func didendcontact (contact: skphysicscontact !) {Panda. jumpstart=Panda. position. Y} Override func didmovetoview (View: skview) {Let skycolor = skcolor (RED: 113/255, Green: 197/255, Blue: 207/255, Alpha: 1) self. backgroundcolor = skycolor // background self. addchild (BG) BG. zposition = 20 self. physicsworld. contactdelegate = self. physicsworld. gravity = cgvectormake (0,-5) self. physicsbody = skphysicsbody (edgeloopfromrect: Self. frame) self. physicsbody. categorybitmask = bitmasktype. scene self. physicsbody. dynamic = false panda. position = cgpointmake (200,400) panda. zposition = 40 self. addchild (PANDA) self. addchild (platformfactory) platformfactory. scenewidth = self. frame. size. width platformfactory. delegate = self platformfactory. zposition = 30 platformfactory. createplatform (3, X: 0, Y: 200)} override func touchesbegan (touches: nsset, withevent event: uievent) {panda. jump ()} override func Update (currenttime: cftimeinterval) {BG. move (movespeed/5) lastdis-= movespeed if lastdis <= 0 {println ("generate new platform") // platformfactory. createplatform (1, x: 1500, Y: 200) platformfactory. createplatformrandom ()} platformfactory. move (movespeed)} func ongetdata (Dist: cgfloat) {self. lastdis = DIST;} protocol protocolmainscene {func ongetdata (Dist: cgfloat )}
PandaClass
Import spritekitenum status: int {Case run = 1, jump, jump2, roll;} class panda: skspritenode {Let runatlas = sktextureatlas (named: "run. atlas ") Let runframes = [sktexture] () Let jumpatlas = sktextureatlas (named:" jump. atlas ") Let jumpframes = [sktexture] (); let rollatlas = sktextureatlas (named:" roll. atlas ") Let rollframes = [sktexture] (); var status = status. run // y coordinate var jumpstart = 0.0 // y coordinate V Ar jumpend = 0.0 Init () {Let texture = runatlas. texturenamed ("panda_run_01") Let size = texture. size () super. init (texture: texture, color: skcolor. whitecolor (), size: Size) var I: int for I = 1; I <= runatlas. texturenames. count; I ++ {Let tempname = string (Format: "panda_run _ %. 2d ", I) Let runtexture = runatlas. texturenamed (tempname) If runtexture {runframes. append (runtexture) }}for I = 1; I <= jumpatlas. t Exturenames. count; I ++ {Let tempname = string (Format: "panda_jump _ %. 2d ", I) Let jumptexture = jumpatlas. texturenamed (tempname) If jumptexture {jumpframes. append (jumptexture) }}for I = 1; I <= rollatlas. texturenames. count; I ++ {Let tempname = string (Format: "panda_roll _ %. 2d ", I) Let rolltexture = rollatlas. texturenamed (tempname) If rolltexture {rollframes. append (rolltexture)} self. physicsbo DY = skphysicsbody (rectangleofsize: texture. size () self. physicsbody. dynamic = true self. physicsbody. allowsrotation = false // friction self. physicsbody. restitution = 0 self. physicsbody. categorybitmask = bitmasktype. panda self. physicsbody. contacttestbitmask = bitmasktype. scene | bitmasktype. platform Self. physicsbody. collisionbitmask = bitmasktype. platform run ()} func run () {self. removeallactions () Self. Status =. Run self. runaction (skaction. repeatactionforever (skaction. animatewithtextures (runframes, timeperframe: 0.05)} func jump () {self. removeallactions () If status! = Status. jump2 {self. runaction (skaction. animatewithtextures (jumpframes, timeperframe: 0.05) self. physicsbody. velocity = cgvectormake (0,450) If status = status. jump {status = status. jump2Self. jumpstart=Self. position. Y;} Else {status = status. jump }}func roll () {self. removeallactions () status =. roll self. runaction (skaction. animatewithtextures (rollframes, timeperframe: 0.05), completion: {() in self. run ()})}}
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 of mobile 08 generate a steady stream of mobile platforms 09 remove platforms outside of the scene 10 parallax rolling background 11 welcome to the physical world 12 collision with the Platform 13 paragraph jump implementation