我正在尝试检测我的精灵节点是否被触摸过,我不知道从哪里开始。
let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)
答案 0 :(得分:62)
首先将name
的{{1}}属性设置为字符串。
SKSpriteNode
然后在pineapple.name = "pineapple"
pineapple.userInteractionEnabled = false
touchesBegan
函数中
Scene
这是一种方法。
您也可以继承override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch:UITouch = touches.anyObject()! as UITouch
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if name == "pineapple"
{
print("Touched")
}
}
}
并覆盖其中的SKSpriteNode
。
touchesBegan
然后做
class TouchableSpriteNode : SKSpriteNode
{
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
print("touched")
}
}
答案 1 :(得分:16)
如果您只查找可以触摸的几个节点(例如,游戏UI中的“继续”或“退出”标签),这可能是一种替代但非常简单的解决方案:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if myNode.containsPoint(touch.locationInNode(self)) {
print("touched")
}
}
答案 2 :(得分:10)
更新Swift Swift版本 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)和XCode版本 8.2.1 (8C1002):
“Set”类型的值没有成员“ anyObject ”
' locationInNode '已重命名为'location(in:)'
' nodeAtPoint '已重命名为'atPoint(_:)'
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node : SKNode = self.atPoint(location)
if node.name == "myNodeName" {
print("Hello")
}
}
}
答案 3 :(得分:7)
这将检测 Xcode 9.2 Swift 4.0
中的触摸{{1}}
答案 4 :(得分:6)
Swift 3 在SKSpriteNode
的子类中嵌入触控功能的答案:
class SpriteSub: SKSpriteNode {
init() {
super.init(texture: nil, color: UIColor.red, size: CGSize(width: 50, height: 50))
isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch!")
}
}
答案 5 :(得分:5)
实施触摸开始时调用的touchesBegan
方法。或者,您也可以在touchesEnded
中执行此操作。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
let nodes = self.nodesAtPoint(location)
for node in nodes
{
if node.name == "youNodeName"
{
// Node tapped
// Do something
break
}
}
}
答案 6 :(得分:4)
使用这段代码检测SKSpriteNode上的触摸
if(nodeAtPoint(location) == node){
}
答案 7 :(得分:1)
Swift 3.0和XCode 7.3.1的更新。我有一个SKShapeNode,我已经将它导出到一个新类并将其插入到Scene中。当我想检测这个对象时,我检查如下:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let nodes = self.nodesAtPoint(location)
for node in nodes
{
if node is SKNodeDerivedNode
{
NSLog("Touch a SKNodeDerivedNode")
break
}
}
}
}
答案 8 :(得分:0)
这是我在Swift 4中用于查找特定类型的节点是否接触的方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let touchPosition = touch.location(in: self)
let touchedNodes = nodes(at: touchPosition)
for node in touchedNodes {
if let nodoTouched = node as? YourNodeType {
// touched!
}
}
}
答案 9 :(得分:0)
如果即使子类化SKSpriteNode
后,仍然没有使精灵工作,那么您很可能在初始化时忘记添加node.isUserInteractionEnabled = true
!
这允许调用touchesBegan(_:with:)
,因为您现在可以与节点进行交互了。
示例:
node = MySprite(texture: texture, size: size)
node.isUserInteractionEnabled = true
答案 10 :(得分:0)
迅速5 更新
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.nodes(at: location)
for node in touchedNode {
if node.name == "play_button" {
startGame()
}
}
}
}