【发布时间】:2015-05-25 00:12:41
【问题描述】:
我希望能够用手指移动精灵。我首先有一个变量来判断手指是否在精灵上:
var isFingerOnGlow = false
然后我有touchesBegan 函数,如果用户触摸精灵,我会在其中更改上述变量的值:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
if let body = physicsWorld.bodyAtPoint(touchLocation) {
if body.node!.name == GlowCategoryName {
println("Began touch on hero")
isFingerOnGlow = true
}
}
}
有时有效,控制台显示“开始触摸英雄”,有时无效。有没有更好的方法来编写第一部分?
那我想知道我下面touchesMoved的代码是否有效:
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if isFingerOnGlow{
var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
var previousLocation = touch.previousLocationInNode(self)
var glow = SKSpriteNode(imageNamed: GlowCategoryName)
// Calculate new position along x for glow
var glowX = glow.position.x + (touchLocation.x - previousLocation.x)
var glowY = glow.position.y + (touchLocation.y - previousLocation.y)
// Limit x so that glow won't leave screen to left or right
glowX = max(glowX, glow.size.width/2)
glowX = min(glowX, size.width - glow.size.width/2)
glow.position = CGPointMake(glowX, glowY)
}
}
...
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
isFingerOnGlow = false
}
【问题讨论】:
-
发光应该做什么?在
touchesMoved? -
获取应该移动的节点。我在raywenderlich.com/84341/create-breakout-game-sprite-kit-swift 上找到了代码,然后稍微修改了一下。但它有时会起作用。
标签: ios swift sprite-kit