我在Sprite Kit中有一个游戏,一旦我触摸屏幕,这个游戏就会启动。我还想在游戏中使用一个按钮,如果我触摸,我只想要调用它而不是实际游戏。这是一个例子。我已经开始接触了#34;触摸"当我触摸屏幕上的任何地方时被调用。我还有一个按钮,如果可以,它会调用"按钮触摸"。我的问题是,如果我触摸按钮并拨打"按钮触摸",它也会调用"触摸开始触及"。为什么?谢谢!
class GameScene: SKScene {
var button: SKNode! = nil
override func didMoveToView(view: SKView) {
button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
button.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(button)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touches began touched")
for touch in touches {
let location = touch.locationInNode(self)
if button.containsPoint(location) {
print("button touched")
}
}
答案 0 :(得分:0)
如果我理解正确,你想在touchesBegan被召唤时开始游戏。但无论何时触摸屏幕,都会调用此功能。所以你可以这样做:
class GameScene: SKScene {
var button: SKNode! = nil
override func didMoveToView(view: SKView) {
button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
button.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(button)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if button.containsPoint(location) {
print("button touched")
}
else {
//start the game here
}
}
这样,除非您触摸按钮,否则每次触摸屏幕时都会启动游戏。