ios - How do I change the direction of an object with swipe? -


i made path object , keeps repeating time. need object stop action , move @ direction swipe (up, down, right, left). after stopping , moving in swipe direction, need object resume path when touching it.

this code:

 import spritekit      class gamescene: skscene {      // functions defines happen when doing swipes     // in different directions.      // player.removeactionforkey("trailrunning") stops     // skaction.repeatactionforever path created     // object. trailrunning keyword created     // manipulate whether action should continue or not.      func swipedright(sender:uiswipegesturerecognizer){         println("swiped right")          player.removeactionforkey("trailrunning")      }      func swipedleft(sender:uiswipegesturerecognizer){         println("swiped left")          player.removeactionforkey("trailrunning")      }      func swipedup(sender:uiswipegesturerecognizer){         println("swiped up")          player.removeactionforkey("trailrunning")      }      func swipeddown(sender:uiswipegesturerecognizer){         println("swiped down")          player.removeactionforkey("trailrunning")      }      var bg = skspritenode()     var player = skspritenode()      // created startposition objects path      let startposition = cgpointmake(20, 20)      // pathrunning not relevant here, i'm using "start"     // game.      var pathrunning = 0       override func didmovetoview(view: skview) {         /* setup scene here */          // tells there no gravity          self.physicsworld.gravity = cgvectormake(0, 0)          // making swipes possible          var swiperight:uiswipegesturerecognizer = uiswipegesturerecognizer(target: self, action: selector("swipedright:"))         swiperight.direction = .right         view.addgesturerecognizer(swiperight)           var swipeleft:uiswipegesturerecognizer = uiswipegesturerecognizer(target: self, action: selector("swipedleft:"))         swipeleft.direction = .left         view.addgesturerecognizer(swipeleft)           var swipeup:uiswipegesturerecognizer = uiswipegesturerecognizer(target: self, action: selector("swipedup:"))         swipeup.direction = .up         view.addgesturerecognizer(swipeup)           var swipedown:uiswipegesturerecognizer = uiswipegesturerecognizer(target: self, action: selector("swipeddown:"))         swipedown.direction = .down         view.addgesturerecognizer(swipedown)           // adding background          var bgtexture = sktexture(imagenamed: "bg")         bg = skspritenode(texture: bgtexture)         bg.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame))         bg.size.height = self.frame.height         bg.size.width = self.frame.width          self.addchild(bg)          // adding player          var playertexture = sktexture(imagenamed: "player")         player = skspritenode(texture: playertexture)         player.position = startposition          // defining physicsbody, able         // make collision detection. density make          // possible me use applyforce().          player.physicsbody = skphysicsbody(rectangleofsize: player.size)         player.physicsbody?.density = 1.0         player.physicsbody?.allowsrotation = false          self.addchild(player)      }      // playerpath function made path     // same on every iphone size.      func playerpath() {          if frame.height == 480 {              var moveplayertobottomright = skaction.moveto(cgpointmake(300,20), duration: 1.5)             var moveplayertotopright = skaction.moveto(cgpointmake(300,460), duration: 2.5)             var moveplayertotopleft = skaction.moveto(cgpointmake(20,460), duration: 1.5)             var moveplayertostartposition = skaction.moveto(startposition, duration: 2.5)              var playertrail = skaction.repeatactionforever(skaction.sequence([moveplayertobottomright, moveplayertotopright, moveplayertotopleft, moveplayertostartposition]))              player.runaction(playertrail, withkey: "trailrunning")          } else if frame.height == 568 {              var moveplayertobottomright = skaction.moveto(cgpointmake(300,20), duration: 1.5)             var moveplayertotopright = skaction.moveto(cgpointmake(300,548), duration: 2.5)             var moveplayertotopleft = skaction.moveto(cgpointmake(20,548), duration: 1.5)             var moveplayertostartposition = skaction.moveto(startposition, duration: 2.5)              var playertrail = skaction.repeatactionforever(skaction.sequence([moveplayertobottomright, moveplayertotopright, moveplayertotopleft, moveplayertostartposition]))              player.runaction(playertrail, withkey: "trailrunning")          } else if frame.height == 667 {              var moveplayertobottomright = skaction.moveto(cgpointmake(355,20), duration: 1.5)             var moveplayertotopright = skaction.moveto(cgpointmake(355,647), duration: 2.5)             var moveplayertotopleft = skaction.moveto(cgpointmake(20,647), duration: 1.5)             var moveplayertostartposition = skaction.moveto(startposition, duration: 2.5)              var playertrail = skaction.repeatactionforever(skaction.sequence([moveplayertobottomright, moveplayertotopright, moveplayertotopleft, moveplayertostartposition]))              player.runaction(playertrail, withkey: "trailrunning")          } else if frame.height == 736 {              var moveplayertobottomright = skaction.moveto(cgpointmake(394,20), duration: 1.5)             var moveplayertotopright = skaction.moveto(cgpointmake(394,716), duration: 2.5)             var moveplayertotopleft = skaction.moveto(cgpointmake(20,716), duration: 1.5)             var moveplayertostartposition = skaction.moveto(startposition, duration: 2.5)              var playertrail = skaction.repeatactionforever(skaction.sequence([moveplayertobottomright, moveplayertotopright, moveplayertotopleft, moveplayertostartposition]))              player.runaction(playertrail, withkey: "trailrunning")           }      }       override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {         /* called when touch begins */          // here tell have press start game          if pathrunning == 0 {             playerpath()         }      }         override func update(currenttime: cftimeinterval) {         /* called before each frame rendered */      }     } 


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -