2015-09-11 80 views
0

我已將其設置爲讓我的玩家跳到我點擊屏幕上的任何位置,但是它不會讓步。我三重檢查了我的代碼,找不到任何錯誤。有人可以幫忙嗎?點擊時玩家不移動

這是我爲我的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

嗯,是的,但是當我第一次敲擊時,我的球員應該跳起來,但是它只是不動。我做錯什麼了嗎? –

+2

第一個水龍頭「touchesBegan」什麼都不做。只要你移動你的手指關於'touchesMoved',你的lastTouch屬性將只會得到一個值。你釋放的第二個,你將它設置爲無'touchesEnded'。 –

回答

0

你說你不需要的代碼觸摸開始是正確的,但是從你的意思是你或許應該。我會嘗試設置觸摸對象的位置開始,然後更新它在觸摸移動。

+0

我試試這個。更新它會是什麼樣子? –

+0

它會跟隨你的運動。如果你只是想讓它移動一次,那麼觸摸就開始了。 –