swift - SpriteKit childNodeWithName 找不到现有节点

标签 swift sprite-kit sknode

我在 SKScene 中有这段代码:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {

    var touch: AnyObject = touches.anyObject()
    var point = getPoint(touch.locationInNode(self))
    var name  = NSStringFromCGPoint(point)

    for children in self.children {

        if (children as SKSpriteNode).name == name {

            println("exist!")
        }
    }
    var tempNode = self.childNodeWithName(name)
}

我看到“存在!”在日志中,因此子数组中有一个具有此名称的节点,但 tempNodenilself.childNodeWithName("//"+ name) 调用也返回 nil

最佳答案

以下是如何在 Swift 中完成此操作...希望对您有所帮助!

var mySprite: SKSpriteNode = childNodeWithName("mySprite") as SKSpriteNode

关于swift - SpriteKit childNodeWithName 找不到现有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25581738/

相关文章:

ios - View 消失后如何获取 AVPlayer 当前正在运行的实例?

swift - 如何在 CLKComplicationDataSource 中为两个并发症定义数据

Xcode Swift 将函数传递给 GameOver 场景

swift - 重置和更改 SKActions 的速度

ios - CMSampleBuffer 的样本呈现时间代表什么?

swift - SpriteKit + Swift - 如何使用缓动移动节点?

objective-c - SKLabelNode 定位未按预期工作

ios - 在 Swift 中触摸 SKSpriteNode 时无法激活代码

size - 如何对 SKNode 进行子类化以将其初始化为预定大小

ios - 核心数据实体唯一约束不起作用