2016-06-30 13 views
1

Ich erstelle ein Side Scroller Mario-ähnliches Spiel in Swift und SpriteKit. Ich bewege gerade den Spieler, aber Sie müssen weiterklicken und dann bewegt es sich. Was ich mir wünsche, ist, wenn Sie es halten, wird es sich bewegen und Sie müssen nicht schnell auf den Bildschirm klicken. Vielen Dank im Voraus !!!!Continuous Touch/Moving Swift & SpriteKit

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 

        if location.y > 400{ 

      move = true 

     }else{ 


      if location.x < CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0)) 

       //tap somewhere above this to make character jump 
       if(location.y > 250) { 
        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
       } 

      } else if location.x > CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0)) 

       //tap somewhere above this to make character jump 

      } 
     } 

    } 

    } 



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    move = false 

} 
+0

Sieht so aus, als ob die Bewegung einmal verschoben wird, wird Ihr Objekt nur einmal bewegt? Möchten Sie es kontinuierlich aufrufen, solange der Benutzer den Finger nicht gehoben hat? – Shripada

+0

Ja wie in Javascript gibt es einen bool Wert namens ** mouseIsPressed ** das ist grundlegend was ich brauche. – ScarletStreak

Antwort

0

Persönlich würde ich eine Controller-Klasse erstellen, die eine Update-Funktion, die Sie hat Umfrage in Ihrer Szene zu aktualisieren, um zu bestimmen, ob eine Schaltfläche Zustand ist nach oben oder unten, und gehen Sie davon los (Touch beginnt Puts Schaltfläche im Zustand "Down", Touch-Ende bringt es in den Zustand. Update Umfragen Zustand und führt Aktionen basierend auf Zustand) in Ihrem Fall, um die Dinge einfach zu halten, ohne viel Code zu ändern, würde ich einfach SKAction.moveBy

01 verwenden
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 

     if location.y > 400{ 

      move = true 

     }else{ 
      //tap somewhere above this to make character jump 
      if(location.y > 250) { 
       player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
      } 
      else 
      { 
       let direction = (location.x > CGRectGetMidX(self.frame)) ? 30 : -30 
       let move = SKAction.moveBy(x:direction,y:0,duration:0) 
       let rep = SKAction.repeatActionForever(move) 
       player.runAction(rep,forKey:"Moving") 
      } 
     } 
    } 
} 



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    player.removeActionForKey("Moving") 
    move = false 

} 
+0

Beachten Sie, dass Sie mehrere Berührungen behandeln müssen, damit Sie auch Aktionen im Berührungsbildschirm entfernen können – Knight0fDragon

0

ich sehr gut verstehe nicht wirklich, was wünschen Sie, aber ich versuche, einige Code zu schreiben:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = true 
      }else{ 
       if location.x < CGRectGetMidX(self.frame){ 
        movePlayer(-30,dy:0) 

        //tap somewhere above this to make character jump 
        if(location.y > 250) { 
         player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
        } 
       } else if location.x > CGRectGetMidX(self.frame){ 
        movePlayer(30,dy:0) 
        //tap somewhere above this to make character jump 
       } 
      } 
     } 
    } 
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = false 
      }else { 
       if location.x < CGRectGetMidX(self.frame) || if location.x > CGRectGetMidX(self.frame) { 
        movePlayer(0,dy:0) 
       } 
      } 
     } 
    } 

    func movePlayer(dx:CGFloat,dy:CGFloat) { 
     if (player.physicsBody.resting) { 
      player.physicsBody?.applyImpulse(CGVector(dx, dy: dy)) 
     } 
    } 
Verwandte Themen