我试图检测我的精灵节点是否被触摸,我不知道从哪里开始.
let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)
Run Code Online (Sandbox Code Playgroud) 我正在为我正在构建的精灵工具包应用程序创建主菜单.在整个项目中,我使用SKScenes来保持我的关卡和实际的游戏玩法.但是,现在我需要一个主菜单,其中包含"Play","Levels","Shop"等按钮......但是,我现在添加按钮的方式感觉不太舒服,就像这样:
let currentButton = SKSpriteNode(imageNamed: button) // Create the SKSpriteNode that holds the button
self.addChild(currentButton) // Add that SKSpriteNode to the SKScene
Run Code Online (Sandbox Code Playgroud)
我检查按钮的触摸如下:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
for node in self.nodes(at: touchLocation) {
guard let nodeName = node.name else {
continue
}
if nodeName == ButtonLabel.Play.rawValue {
DispatchQueue.main.asyncAfter(deadline: .now()) {
let transition = SKTransition.reveal(with: .left, duration: 1)
self.view?.presentScene(self.initialLevel, transition: transition)
self.initialLevel.loadStartingLevel()
}
return
}
if nodeName …Run Code Online (Sandbox Code Playgroud) 我在这个精灵中有一个菜单按钮(skSpriteNode)和一个小的播放图标
mainButton.addChild (playIcon)
mainButton.name = "xyz"
addchild (mainButton)
Run Code Online (Sandbox Code Playgroud)
好的,我给按钮起了个名字来检查是否触摸了这个名字的精灵。
当我触摸按钮内的图标子项时,touchedNode.name 为零。我为图标子项设置 isUserInteractionEnabled = false 。我想将触摸传递给父母。我怎样才能做到这一点。
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
if (touchedNode.name != nil) {
print ("node \(touchedNode.name!)")
} else {
continue
}
}
Run Code Online (Sandbox Code Playgroud)