首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SKSpriteNode链

SKSpriteNode链
EN

Stack Overflow用户
提问于 2016-10-26 11:36:14
回答 1查看 62关注 0票数 2

我想让" aware“SKSpriteNode意识到触摸在节点之外(例如改变颜色)。

这些spries应该是独立的。

如何做到这一点?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-10-26 14:55:12

显然,您可以实现touchesBegan方法来处理触摸事件:

代码语言:javascript
复制
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}

使用当前Swift 3语法处理触摸的其他事件包括:

代码语言:javascript
复制
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}

关于这一点,如果你不想子类化你的SKSpriteNode来添加有用的属性,你可以使用userData属性来存储关于其他节点或他自己的信息,以及关于当前上下文的信息:

代码语言:javascript
复制
yourSprite.userData = NSMutableDictionary()
yourSprite.userData?.setValue(SKColor.blue, forKeyPath: "currentColor")
yourSprite.userData?.setValue(true, forKeyPath: "selected")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40253284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档