我想检测我的精灵节点是否被触摸,但我不知道从哪里开始。
let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)
我想检测我的精灵节点是否被触摸,但我不知道从哪里开始。
let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)
先将SKSpriteNode
的name
属性设置为字符串。
pineapple.name = "pineapple"
pineapple.userInteractionEnabled = false
然后在Scene
中的touchesBegan
函数中
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")
}
}
那么就这样做
let pineapple = TouchableSpriteNode(imageNamed: "Pineappleimg")
pineapple.userInteractionEnabled = true
pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(pineapple)
if let name = touchedNode.name //如果被触摸的节点有名称
{
if name == "pineapple" //如果名称为"pineapple"
{
print("Touched") //打印"Touched"
}
}
}
- uplearned.com如果你只想要一些可以点击的节点(例如游戏用户界面中的“继续”或“退出”标签),那么这可能是一个替代但非常简单的解决方案:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if myNode.containsPoint(touch.locationInNode(self)) {
print("touched")
}
}
更新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")
}
}
}
这将检测到在Xcode 9.2 Swift 4.0中的触摸事件。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name
{
if name == "playLbl"
{
print("playLbl Touched")
}
}
}
Swift 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()
}
}
}
}
实现 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
}
}
}
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!")
}
}
let sprite = SpriteSub()
对我来说什么也没有,而自动完成除了coder:
init之外什么都没有显示...... - Fluidityif(nodeAtPoint(location) == node){
}
==
而是应该使用===
,对吗? - mogelbuster更新为Swift 3.0和XCode 7.3.1。我有一个SKShapeNode,我将其派生到一个新类中并将其插入到场景中。当我想要检测此对象时,我按以下方式检查:
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
}
}
}
}
SKSpriteNode
进行子类化后仍然无法使精灵工作,那么您最有可能忘记在初始化时添加node.isUserInteractionEnabled = true
!这将允许调用touchesBegan(_:with:)
,因为现在您可以与节点交互。
例子:
node = MySprite(texture: texture, size: size)
node.isUserInteractionEnabled = true