【问题标题】:How can I make it so that both player move when they are touches?我怎样才能让两个玩家在接触时都移动?
【发布时间】:2016-09-04 03:17:39
【问题描述】:

我正在制作这个游戏,你应该在其中与另一个人玩躲避球。问题是当我触摸它时我没有让桨叶移动。当我触摸桨时,它会移动,但它也会移动桨。我可以做什么或改变什么?

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first
    let touchLocation = touch!.locationInNode(self)


    let touchi = touches.first
    let touchLocationi = touchi!.locationInNode(self)
    if let body = physicsWorld.bodyAtPoint(touchLocation) {
        if body.node!.name == PaddleCategoryName {
            print("Began touch on paddle")
            isFingerOnPaddle = true
        }
    }
    if let otherBody = physicsWorld.bodyAtPoint(touchLocationi) {
        if otherBody.node!.name == PaddleyName {
            print("EL otro toco")
            isFingerOnPaddlei = true
        }
    }
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    isFingerOnPaddle = false
    isFingerOnPaddlei = false
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if isFingerOnPaddle {
        // 2
        let touch = touches.first
        let touchLocation = touch!.locationInNode(self)
        let previousLocation = touch!.previousLocationInNode(self)
        // 3
        let paddle = childNodeWithName(PaddleCategoryName) as! SKSpriteNode
        // 4
        var paddleY = paddle.position.y + (touchLocation.y - previousLocation.y)
         var paddleX = paddle.position.x + (touchLocation.x - previousLocation.x)
        // 5
        paddleY = max(paddleY, paddle.size.width/2)
        paddleY = min(paddleY, size.height - paddle.size.height/2)
        paddleX = max(paddleX, paddle.size.height/2)
        paddleX = min(paddleX, size.height - paddle.size.width/2)
        // 6
        paddle.position = CGPoint(x: paddleX, y: paddleY)

        if isFingerOnPaddlei {

        let paddlei = childNodeWithName(PaddleyName) as! SKSpriteNode


        // 4
        var paddleS = paddlei.position.y + (touchLocation.y - previousLocation.y)
        var paddleU = paddlei.position.x + (touchLocation.x - previousLocation.x)
        // 5
        paddleS = max(paddleS, paddlei.size.width / 2)
        paddleS = min(paddleS, size.height - paddlei.size.height / 2)
        paddleU = max(paddleU, paddlei.size.height / 2)
        paddleU = min(paddleU, size.height - paddlei.size.width / 2)
        // 6
        paddlei.position = CGPoint(x: paddleU, y: paddleS)

        }




    }


}

}

【问题讨论】:

  • 尝试将 if isFingerOnPaddlei {..} 移到另一个 if 语句之外。

标签: swift sprite-kit skspritenode


【解决方案1】:

if isFingerOnPaddle 在声明 if isFingerOnPaddle 的 if 语句中。第二个 if 语句永远不会被调用,因为它是嵌套的,并且两者永远都不应该同时为真。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 2021-12-19
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多