2015-08-22 2 views
2

В новой игре, которую я пытаюсь построить, я хочу знать, когда пользователь коснулся BOTH правой и левой стороны экрана, чтобы сделать некоторую логику.Sprite-kit: обнаружение левого и правого штрихов

Мой код:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 

    for touch in (touches as! Set<UITouch>) { 
     let location = touch.locationInNode(self) 
     var isRight : Bool = false 
     var isLeft : Bool = false 

     if(location.x < self.size.width/2){ 
      isLeft = true 
      println("Left") 
     } 

     if(location.x > self.size.width/2){ 
      // 
      isRight = true 
      println("Right") 
     } 
     if (isRight && isLeft){ 
      println("Both touched") 
      // do something.. 
     } 
    } 
} 

консоли:

Right 
Left 

Мой код не работает. Что я здесь делаю неправильно? У кого-нибудь есть лучшее решение?

ответ

3

ДЕЛАТЬ это;

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 

    var isRight : Bool = false 
    var isLeft : Bool = false 

    for touch in (touches as! Set<UITouch>) { 
     let location = touch.locationInNode(self) 

     if(location.x < self.size.width/2){ 
      isLeft = true 
      println("Left") 
     } 

     if(location.x > self.size.width/2){ 
      // 
      isRight = true 
      println("Right") 
     } 
    } 

    if (isRight && isLeft){ 
     println("Both touched") 
     // do something.. 
    } 

} 

Обновлено: Swift 2,2

Для SKView:

class skView:SKView { 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 

     var isRight : Bool = false 
     var isLeft : Bool = false 

     for touch in touches { 
      let location = touch.locationInView(self); 

      if(location.x < self.frame.size.width/2){ 
       isLeft = true 
       print("Left") 
      } 

      if(location.x > self.frame.size.width/2){ 
       // 
       isRight = true 
       print("Right") 
      } 
     } 

     if (isRight && isLeft){ 
      print("Both touched") 
      // do something.. 
     } 

    } 
} 

Для SKNode:

class skNode: SKNode { 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     var isRight : Bool = false 
     var isLeft : Bool = false 

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

      if(location.x < self.frame.size.width/2){ 
       isLeft = true 
       print("Left") 
      } 

      if(location.x > self.frame.size.width/2){ 
       // 
       isRight = true 
       print("Right") 
      } 
     } 

     if (isRight && isLeft){ 
      print("Both touched") 
      // do something.. 
     } 
    } 
} 
+0

Благодаря человеку. Он работает –

+0

где находится этот код; т.е. что это означает «я»? – Confused

+0

@Confused см. Обновление – Shoaib

Смежные вопросы