【发布时间】: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