gpt4 book ai didi

swift - 雪碧包 : How do you call touches began & in touches separately?

转载 作者:行者123 更新时间:2023-11-30 13:27:36 24 4
gpt4 key购买 nike

我在 Sprite Kit 中有一个游戏,只要我触摸屏幕,这个游戏就会开始。我还想要游戏中的一个按钮,如果我触摸它,我只想调用它而不是实际的游戏。这是一个例子。当我触摸屏幕上的任何地方时,我都会调用“触摸开始触摸”。我还有一个按钮,如果可以的话,它称为“触摸按钮”。我的问题是,如果我触摸按钮并调用“触摸按钮”,它也会调用“触摸开始触摸”。为什么?谢谢!

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")

}
}

最佳答案

如果我没理解错的话,你想在调用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
}
}

这样,每次您触摸屏幕时,游戏都会开始,除了触摸按钮时。

关于swift - 雪碧包 : How do you call touches began & in touches separately?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com