为什么我的 SKSpriteNode 可以工作但只是暂时的

Why does my SKSpriteNode work but only temporarily

我正在通过有关简单纸牌游戏的教程学习 SpriteKit。我有一个场景,我在上面放了两张卡片,即节点。我应该能够点击其中一张卡片,将其拾起、移动并放下。奇怪的是,我可以这样做,但通常只有一次。如果我再次尝试这样做,它有时会起作用,但很多时候我得到了旋转的沙滩球并且必须弹起模拟器才能让它再次起作用。这是简单的代码,但我就是不明白为什么要这样做。

enum CardName: Int {
    case CreatureWolf = 0,
    CreatureBear
}

class Card : SKSpriteNode {
let frontTexture: SKTexture
let backTexture: SKTexture
var largeTexture: SKTexture?
let largeTextureFilename: String

required init(coder aDecoder: NSCoder) {
    fatalError("NSCoding not supported")
}

init(cardNamed: CardName) {

    // initialize properties
    backTexture = SKTexture(imageNamed: "card_back.png")

    switch cardNamed {
    case .CreatureWolf:
        frontTexture = SKTexture(imageNamed: "card_creature_wolf.png")
        largeTextureFilename = "card_creature_wolf_large.png"

    case .CreatureBear:
        frontTexture = SKTexture(imageNamed: "card_creature_bear.png")
        largeTextureFilename = "Card_creature_bear_large.png"

    default:
        frontTexture = SKTexture(imageNamed: "card_back.png")
        largeTextureFilename = "card_back_large.png"
    }

    // call designated initializer on super
    super.init(texture: frontTexture, color: UIColor.whiteColor(), size: frontTexture.size())


    // set properties defined in super
    userInteractionEnabled = true
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for _ in touches {
        zPosition = 15
        let liftUp = SKAction.scaleTo(1.2, duration: 0.2)
        runAction(liftUp, withKey: "pickup")
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(scene!)
        let touchedNode = nodeAtPoint(location)
        touchedNode.position = location
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for _ in touches {
        zPosition = 0
        let dropDown = SKAction.scaleTo(1.0, duration: 0.2)
        runAction(dropDown, withKey: "drop")
    }
  }
}

这里是将卡片放在场景中的代码:

//
//  GameScene.swift
//  PlanningPoker

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
      let wolf = Card(cardNamed: .CreatureWolf)
      wolf.position = CGPointMake(400,600)
      addChild(wolf)

      let bear = Card(cardNamed: .CreatureBear)
      bear.position = CGPointMake(600, 600)
      addChild(bear)
  }
}

任何人都可以帮助我理解为什么会发生这种情况或者我该如何调试它?我在每个 'touches*' 函数的开头和结尾添加了打印语句,以确保它按预期通过它们(确实如此),Xcode 没有给我任何警告或错误,我我根据教程仔细检查了代码 27 次,以确保它匹配(它确实匹配,除了一些我必须更改以使其达到 Swift 2.1 的地方)并且它应该可以工作。

谢谢

我最好的猜测是这是不正确的。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(scene!)
        let touchedNode = nodeAtPoint(location)
        touchedNode.position = location
    }
}

您获得了正确的位置,但您可能无法获得所需的节点。您实际上是在尝试移动调用此方法的节点。无需尝试获取不同的节点。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(scene!)
        position = location
    }
}

希望对您有所帮助。