【发布时间】:2016-11-24 20:20:05
【问题描述】:
我正在继承 SKSpritenode(在 Swift 中)来创建可以在场景中拖动的彩色块。子类是 SoundNode。
import SpriteKit
class SoundNode: SKSpriteNode {
init() {
super.init(texture: nil, color: UIColor.blue, size: CGSize(width: 100, height: 100))
self.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 began")
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touches moved")
guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)
self.position = touchLocation
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
}
}
在 GameScene.swift 中
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
addSoundBlock()
}
func addSoundBlock() {
let soundBlock = SoundNode()
soundBlock.position = CGPoint(x: 800, y: 800)
addChild(soundBlock)
}
}
这行得通。
添加了声音块,并且可以在场景中拖动。但它会闪烁,有时会消失。 我已经尝试过 touchesMoved 中的其他方法,但都没有影响到抖动。
如果我不对 touchesBegan、touchesMoved、touchesEnded 进行子类化,并在 GameScene 中实现这些动作,那么拖动就会变得平滑。但未来的计划取决于能否将这些子类化。
Xcode 8、Swift 3、iOS10
【问题讨论】:
-
嘿,Rich,你查到了这件事的真相吗?我正在考虑类似的事情。
-
我的直觉是:
let touchLocation = touch.location(in: self)应该指的是场景框架中的触摸位置,而不是精灵(自身)的框架中。 -
我只能通过不对行为进行子类化来修复它。所以我把 touchesBegan 和其他动作移到了 GameScene.swift 这行得通,但感觉很乱。不知道我是否应该保持打开状态或将其作为解决方案发布。
标签: ios swift sprite-kit skspritenode