swift - SKSpriteNode 未更新

标签 swift skspritenode

我只是想让一个 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/

相关文章:

ios - ViewController、tableView 和 Mapkit

ios - 将闭包作为目标添加到 UIButton

Swift TableView 多个自定义单元格,增加单元格中的行数导致崩溃

swift - 使用 sprite 工具包垂直滚动背景的问题

swift - 随机生成一个 Spritekit 节点

ios - 裁缝强制转换冲突

ios - 为什么在延迟调用时循环迭代有时会被打乱? [代码和输出]

ios - touch.locationInView 没有像我想的那样工作

swift - 从 SKSpriteNode 发射子弹

ios - 无法在 SpriteKit 中获取 skAudioNode 的 3D 音频效果