【问题标题】:Why does my SKSpriteNode work but only temporarily为什么我的 SKSpriteNode 工作但只是暂时的
【发布时间】:2015-12-04 04:53:59
【问题描述】:

我正在通过一个关于简单纸牌游戏的教程来学习 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 的东西)并且它应该可以工作。

谢谢

【问题讨论】:

  • 您发布的代码似乎没有任何问题。我建议您发布创建卡片并将其添加到场景的代码。
  • 我已经添加了。谢谢。

标签: swift sprite-kit skspritenode


【解决方案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
    }
}

希望对您有所帮助。

【讨论】:

  • 谢谢。这似乎已经解决了它。本教程从场景中的触摸功能开始,后来让我将它们移至 Card.swift。它说当它们在 Card 类中时从 Began 和 Ended 方法中删除 'let touchNode = nodeAtPoint(location)' 但没有说在 Moved 方法中这样做,但它非常有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 2020-05-13
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多