【问题标题】:Split Screen into Two View Controllers SpritKit + Swift分屏成两个视图控制器 SpritKit + Swift
【发布时间】:2015-10-21 09:32:00
【问题描述】:

我正在使用 SpriteKit + Swift 制作一个简单的 Pong 游戏。即使手指已经在触摸显示屏,我也在尝试移动两个拨片。我得到了将屏幕分成两个视图控制器的建议。这会有帮助吗?如果是这样,这将如何完成? Link to previous question.

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

    if body?.node!.name == PaddleCategoryName {
        print("Paddle Touched!")
        fingerIsOnPaddle = true
    }

    if body?.node!.name == PaddleCategoryName2 {
        print("Paddle2 Touched!")
        fingerIsOnPaddle2 = true
    }


}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if fingerIsOnPaddle {
        let touch = touches.first as UITouch?
        let touchLocation = touch?.locationInNode(self)
        let previousTouchLocation = touch?.previousLocationInNode(self)

        let paddle = self.childNodeWithName(PaddleCategoryName) as! SKSpriteNode

        var newYPosition = paddle.position.y + (touchLocation!.y - previousTouchLocation!.y)

        newYPosition = max(paddle.size.height / 2, newYPosition)
        newYPosition = min(self.size.height - paddle.size.height / 2, newYPosition)

        paddle.position = CGPointMake(paddle.position.x, newYPosition)

    }

    if fingerIsOnPaddle2 {
        let touch = touches.first as UITouch?
        let touchLocation = touch?.locationInNode(self)
        let previousTouchLocation = touch?.previousLocationInNode(self)

        let paddle2 = self.childNodeWithName(PaddleCategoryName2) as! SKSpriteNode

        var newYPosition = paddle2.position.y + (touchLocation!.y - previousTouchLocation!.y)

        newYPosition = max(paddle2.size.height / 2, newYPosition)
        newYPosition = min(self.size.height - paddle2.size.height / 2, newYPosition)

        paddle2.position = CGPointMake(paddle2.position.x, newYPosition)
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    fingerIsOnPaddle = false
    fingerIsOnPaddle2 = false
}

【问题讨论】:

  • 我不确定这是否真的是您想要做的。您可能会更好地获取触摸的位置,因此您可以使用该位置来查看它是在 iDevice 的一侧还是另一侧。 touchesMoved 也是如此,只需查看触摸的位置,然后移动该侧的拨片即可。
  • 我试过了,但还是不行。
  • 你遇到了什么错误?你能展示一些代码吗?
  • 基本上,我制作了两个倾斜的LeftSide和RightSide节点。如果当时按下了 LeftSide,它会发现我是否按下了左侧的 Paddle1,反之亦然。当我运行更改时,我仍然遇到同样的问题。
  • 我在问题中添加了代码。

标签: ios swift sprite-kit


【解决方案1】:

您提供的代码的主要问题是您使用第一次触摸,当您有两个人移动桨时,这并不好。我实际上不会使用touchesBegantouchesEnded。我将假设游戏场的水平方向将有x,因此512(默认场景宽度除以2)将是中间线,桨位于x = 0x = 1024

下面的代码在touchesMoved 中,处理每个移动的触摸。如果触摸位置在屏幕的一侧,它会获取该侧的拨片并移动它。您可能希望将此代码也放入touchesBegan 方法中,以便用户可以触摸他们想要桨叶去的地方。你可以回到你的桨检测方法(除了不使用每次触摸之外,我没有发现任何问题),但我建议使用paddle.containsPoint 来测试触摸是否在里面。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: UITouch in touches {
        let location = touch.locationInNode(self)
        if location.x < 512 {
            let paddle = self.childNodeWithName(PaddleCategoryName) as! SKSpriteNode
            paddle.position.y = location.y
        } else {
            let paddle2 = self.childNodeWithName(PaddleCategoryName2) as! SKSpriteNode
            paddle2.position.y = location.y
        }
    }
}

我目前正在下载 Xcode 7 以获取 Swift 2,我将使用正确的语法对其进行编辑。但就目前而言,只需进行一些更改(例如方法调用),它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多