I am confused with the objects of my class Card that is the subclass of SKSpriteNode. How do I get access to these objects when a user move them. So far I can only access SKNode objects overriding touchesEnded function.
我对我的类Card的对象感到困惑,它是SKSpriteNode的子类。如何在用户移动这些对象时访问这些对象。到目前为止,我只能访问覆盖touchesEnded函数的SKNode对象。
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch = touches.first as UITouch!
let touchLocation = touch.locationInNode(self)
touchedNode = self.nodeAtPoint(touchLocation)
touchedNode.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)
touchedNode.zPosition = 0
}
I need to know what object the user is moving but in this case when touchedNode is an object of SKNode class (not my Card object class) I am not able to figure that out.
我需要知道用户正在移动的对象,但在这种情况下,当touchNode是SKNode类的对象(不是我的Card对象类)时,我无法弄明白。
1 个解决方案
#1
1
The is
operator is exactly what you need.
运算符正是您所需要的。
The is
operator compares the types of the two operands. If they are of the same type, or one is a subclass of the other, the expression evaluates to true.
运算符比较两个操作数的类型。如果它们属于同一类型,或者一个是另一个的子类,则表达式的计算结果为true。
So you can check it like this:
所以你可以这样检查:
if touchedNode is Card {
// do stuff
}
Now what you might want to do is that if touchedNode
is really a Card
, I want to use the methods I defined in the Card
class on the touchedNode
.
现在您可能想要做的是,如果被触摸的节点实际上是一张卡片,我想使用我在touchNode上的Card类中定义的方法。
To do this, you need to cast the touchedObject
to Card
:
为此,您需要将被触摸的对象转换为卡:
let cardNode = touchedNode as! Card
Then you can call your methods on cardNode
!
然后你可以在cardNode上调用你的方法!
#1
1
The is
operator is exactly what you need.
运算符正是您所需要的。
The is
operator compares the types of the two operands. If they are of the same type, or one is a subclass of the other, the expression evaluates to true.
运算符比较两个操作数的类型。如果它们属于同一类型,或者一个是另一个的子类,则表达式的计算结果为true。
So you can check it like this:
所以你可以这样检查:
if touchedNode is Card {
// do stuff
}
Now what you might want to do is that if touchedNode
is really a Card
, I want to use the methods I defined in the Card
class on the touchedNode
.
现在您可能想要做的是,如果被触摸的节点实际上是一张卡片,我想使用我在touchNode上的Card类中定义的方法。
To do this, you need to cast the touchedObject
to Card
:
为此,您需要将被触摸的对象转换为卡:
let cardNode = touchedNode as! Card
Then you can call your methods on cardNode
!
然后你可以在cardNode上调用你的方法!