2015-10-15 77 views
0

我在SpriteKit中制作游戏。我在那里有一个左边的街区和一个右边的街区。起初我以为使用nodeAtPoint与块进行交互,但在我的情况下,对于玩家来说太不舒服。我想这样做,如果玩家触摸屏幕左侧的任何地方 - 它会触发左侧屏蔽,屏幕右侧会相应地触发右侧屏蔽。一个人怎么能这样做呢? 现在我使用节点点交互的代码如下所示:如何在视图中设置触摸位置

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     switch self.nodeAtPoint(location) { 
     case self.leftblock: 
      println("closeleft") 
      leftblock.runAction(SKAction.animateWithTextures(leftArray, timePerFrame: 0.03, resize: true, restore: false)) 
     case self.rightblock: 
      println("closeright") 
      rightblock.runAction(SKAction.animateWithTextures(rightArray, timePerFrame: 0.03, resize: true, restore: false)) 
     default: break 
     } 
    } 
} 


override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     switch self.nodeAtPoint(location) { 
     case self.leftblock: 
      println("openleft") 
      leftblock.runAction(SKAction.animateWithTextures(reversedLeftArray, timePerFrame: 0.03, resize: true, restore: false)) 
     case self.rightblock: 
      println("openright") 
      rightblock.runAction(SKAction.animateWithTextures(reversedRightArray, timePerFrame: 0.03, resize: true, restore: false)) 
     default: break 
     } 
    } 
} 

回答

0

在屏幕左侧的触摸意味着该x coordinate of the location of the touch将小于width/2。而对于右侧, 的x coordinate of the location of the touch将大于width/2

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

    for touch in touches { 

     let touchPoint = touch.locationInView(self.view) 

     let middleOfScreen = (self.view?.frame.width)!/2 

     print(self.frame) 

     print(middleOfScreen) 
     print(touchPoint) 

     if touchPoint.x < middleOfScreen { 
      print("Clicked Left Side"); 
     } else { 
      print("Clicked Right Side"); 
     } 
    } 
} 
+0

谢谢!这就是我的预期! :) – TimurTim