Sprite kit 如何在视图中设置有关其位置的触摸
我正在用SpriteKit做一个游戏。我有一个左街区和一个右街区。起初,我想用nodeAtPoint与方块交互,但在我的例子中,这对玩家来说太不舒服了。我想这样做,如果玩家触摸屏幕左侧的任何地方,它会触发左侧块,屏幕右侧相应地触发右侧块。怎么能这样呢? 目前,我使用节点进行交互的代码如下所示:Sprite kit 如何在视图中设置有关其位置的触摸,sprite-kit,location,Sprite Kit,Location,我正在用SpriteKit做一个游戏。我有一个左街区和一个右街区。起初,我想用nodeAtPoint与方块交互,但在我的例子中,这对玩家来说太不舒服了。我想这样做,如果玩家触摸屏幕左侧的任何地方,它会触发左侧块,屏幕右侧相应地触发右侧块。怎么能这样呢? 目前,我使用节点进行交互的代码如下所示: override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { /* Called whe
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
}
}
}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
开关自身节点点(位置){
case self.leftblock:
println(“closeleft”)
leftblock.runAction(SKAction.animateWithTextures(leftArray,timePerFrame:0.03,resize:true,restore:false))
case self.rightblock:
println(“右关闭”)
rightblock.runAction(SKAction.animateWithTextures(rightArray,timePerFrame:0.03,resize:true,restore:false))
默认值:中断
}
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
开关自身节点点(位置){
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))
默认值:中断
}
}
}
触摸屏左侧意味着触摸位置的x坐标将小于宽度/2
。至于右边,
触摸位置的x坐标将大于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");
}
}
}
override func touchesend(touch:Set,withEvent-event:UIEvent?){
接触{
让接触点=touch.locationInView(self.view)
让屏幕中间=(self.view?.frame.width)!/2
打印(自选框)
打印(屏幕中间)
打印(接触点)
如果触摸点.x<屏幕中间{
打印(“单击左侧”);
}否则{
打印(“单击右侧”);
}
}
}
谢谢!这正是我所期望的!:)