点击时播放器不动

时间:2015-09-11 12:11:34

标签: swift sprite-kit

我进行了设置,以便我的播放器可以跳到我点击屏幕的任何地方,但是,它不会让步。我三重检查我的代码,找不到任何错误。有人可以帮忙吗?

这是我的GameScene的代码:

import SpriteKit

var player: Player!
var snowflake: Snowflake!
var isFingerOnPlayer = false
var gameOver = false
var playerTouched = false
var touching: Bool = false
var lastTouch: CGPoint? = nil
let xPlayerForce: CGFloat = 30
let yPlayerForce: CGFloat = 40
var touchPoint: CGPoint = CGPoint()

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
   player = Player()
    addChild(player)
    backgroundColor = UIColor.whiteColor()
    snowflake = Snowflake()
    addChild(snowflake)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        lastTouch = location
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    lastTouch = nil
}

override func update(currentTime: NSTimeInterval) {
    if let touch = lastTouch {
    var xForce: CGFloat = 0.0
    var yForce: CGFloat = 0.0
    let xTouchOffset = (touch.x - player.position.x)

    let yTouchOffset = (touch.y - player.position.y)

    if xTouchOffset > 0.0 
    {
        xForce = xPlayerForce
    } 
    else if xTouchOffset < 0.0
    {
        xForce = -xPlayerForce
    } // else we do nothing

    if yTouchOffset > 0.0 
    {
        yForce = yPlayerForce
    } // here you can choose whether you want it to push
    // the player node down, using similar code from the
    // above if statement

    let impulseVector = CGVector(dx: xForce, dy: yForce)
    player.physicsBody?.applyImpulse(impulseVector)
    }
}

1 个答案:

答案 0 :(得分:0)

你是对的,说你开始时并不需要触摸代码,但是从你所说的你应该开始。我会尝试在触摸开始时设置对象的位置,然后在移动的触摸中更新它。