如何使用Swift将节点移动到我在SpriteKit中触摸的位置?

时间:2023-01-22 21:35:03

I am creating a game like Doodle Jump (without the accelerometer).

我正在创建像Doodle Jump这样的游戏(没有加速计)。

I have been trying to figure this out, but I can't seem to make it run as smoothly as I've been hoping.

我一直试图解决这个问题,但我似乎无法让它像我一直希望的那样顺利运行。

Here is my code for my touches functions:

这是我的触摸功能的代码:

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

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

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

override func update(currentTime: CFTimeInterval) {
    if let touch = lastTouch {
        let impulseVector = CGVector(dx: 400, dy: 400)
        player.physicsBody?.applyImpulse(impulseVector)
    }
}

1 个解决方案

#1


0  

From what I'm gathering, you want to change the impulseVector that is applied to the player based on where the touch is. I would imagine the code looking something like this:

从我正在收集的内容中,您想要根据触摸的位置更改应用于播放器的impulseVector。我会想象代码看起来像这样:

// At the top of your code
let scale = 0.5

// Update function
override func update(currentTime: CFTimeInterval) {
    if let touch = lastTouch {
        let xOffset = (touch.x - player.position.x)*scale
        let yOffset = (touch.y - player.position.y)*scale
        let impulseVector = CGVector(dx: xOffset, dy: yOffset)
        player.physicsBody?.applyImpulse(impulseVector)
    }
}

This will pull the player node up with more force the further away it is from the touch. If you are trying to pull the player with the same amount of force, no matter where they are (which may be more likely in this case), you would do something like this:

这将使玩家节点以更大的力量向上移动,距离触摸越远。如果你试图用相同的力量拉动玩家,无论他们在哪里(在这种情况下可能更有可能),你会做这样的事情:

// At the top of your code
let xPlayerForce = 20
let yPlayerForce = 30 

// Update function
override func update(currentTime: CFTimeInterval) {
    if let touch = lastTouch {
        var xForce = 0.0
        var yForce = 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  

From what I'm gathering, you want to change the impulseVector that is applied to the player based on where the touch is. I would imagine the code looking something like this:

从我正在收集的内容中,您想要根据触摸的位置更改应用于播放器的impulseVector。我会想象代码看起来像这样:

// At the top of your code
let scale = 0.5

// Update function
override func update(currentTime: CFTimeInterval) {
    if let touch = lastTouch {
        let xOffset = (touch.x - player.position.x)*scale
        let yOffset = (touch.y - player.position.y)*scale
        let impulseVector = CGVector(dx: xOffset, dy: yOffset)
        player.physicsBody?.applyImpulse(impulseVector)
    }
}

This will pull the player node up with more force the further away it is from the touch. If you are trying to pull the player with the same amount of force, no matter where they are (which may be more likely in this case), you would do something like this:

这将使玩家节点以更大的力量向上移动,距离触摸越远。如果你试图用相同的力量拉动玩家,无论他们在哪里(在这种情况下可能更有可能),你会做这样的事情:

// At the top of your code
let xPlayerForce = 20
let yPlayerForce = 30 

// Update function
override func update(currentTime: CFTimeInterval) {
    if let touch = lastTouch {
        var xForce = 0.0
        var yForce = 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)
    }
}