我只是想让一个 Sprite 节点在被点击时移动。但似乎场景直到第二次单击另一个 spritenode 时才会更新。
我的错误在哪里?
override init(size: CGSize) {
super.init(size: size)
cardLayer = SKNode()
addChild(cardLayer)
sprite = SKSpriteNode()
let cardSize = CGSize(width: 80,height: 100)
sprite.size = cardSize
sprite.texture = SKTexture(imageNamed: "1.png")
sprite.position = CGPoint(x: 200, y: 200)
sprite.name = "123"
cardLayer.addChild(sprite)
sprite1 = SKSpriteNode()
sprite1.size = cardSize
sprite1.texture = SKTexture(imageNamed: "1.png")
sprite1.position = CGPoint(x: 200, y: 300)
sprite1.name = "123"
cardLayer.addChild(sprite1)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let array = Array(touches)
let touch = array[0]
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if let _ = touchedNode.name {
print("moved")
touchedNode.position = CGPoint(x: 10, y: 10)
}
}
最佳答案
这就是我做你想做的事情的方式。
我认为您应该尝试使用节点(位于:)。
这是一个使用中的示例
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
//gets location
let location = touch.location(in: self)
//gets of what nodes are at the location
let object = nodes(at: location)
// if the there is a node on that location that is the sprite I want
if object.contains(yourSprite){
//you can put what ever numbers you want to run in there. This simply
// determines the direction of the movement based on a t-Graph
yourSprite.run(SKAction.moveBy(x: 0, y: -5, duration: 1))
}
}
关于swift - SKSpriteNode 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439613/