【问题标题】:swift 4 two dpads with spritekitswift 4 两个带 spritekit 的 dpad
【发布时间】:2018-09-10 18:51:00
【问题描述】:

A screenshot of my game 我想制作一个游戏(使用 Spritekit),您可以在其中使用左侧 dpad 在已经可以使用的瓷砖地图中移动玩家。使用正确的,您可以瞄准同样有效的对手。虽然我启用了多点触控,但只有一个控制器同时工作。

Joystick 与 dpad 的含义相同。

    import SpriteKit
    import GameplayKit

    class GameScene: SKScene {

//These are just the touch functions

    //touch functions

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for _ in touches {
            if touches.first!.location(in: cam).x < 0 {
                moveStick.position = touches.first!.location(in: cam)
            }
            if touches.first!.location(in: cam).x > 0 {
                shootStick.position = touches.first!.location(in: cam)
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {     
        for _ in touches {
            if touches.first!.location(in: cam).x < 0 {
                moveStick.moveJoystick(touch: touches.first!)
            }
            if touches.first!.location(in: cam).x > 0 {
                shootStick.waponRotate(touch: touches.first!)
            }
        }
    }

    open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        for _ in touches {
            resetMoveStick()
            resetShootStick()
        }
    }

    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        for _ in touches {
            resetMoveStick()
            resetShootStick()
        }
    }



    //  update function
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered

        let jSForce = moveStick.velocityVector
        self.player.position = CGPoint(x: self.player.position.x + jSForce.dx,
                                       y: self.player.position.y + jSForce.dy)
        cam.position = player.position

    }
}

【问题讨论】:

  • 你在使用.first....想想这意味着什么
  • 什么是凸轮? moveStick 和shootStick 是什么?你能把这些东西的代码贴出来让我明白它们是什么吗?

标签: swift sprite-kit multi-touch d-pad


【解决方案1】:

正如 KnightOfDragon 所指出的,您使用的是.first。这意味着您的代码正在寻找场景中的第一次触摸,然后从那里开始。您的游戏不会让您同时使用两个操纵杆,因为您不会让它们同时使用。

您在各种触摸功能中的这些 if 语句:

for _ in touches {
    if touches.first!.location(in: cam).x < 0 {
    }
    if touches.first!.location(in: cam).x > 0 {
    }
}

应该是这样的:

for touch in touches {
    let location = touch.location(in: self)
    if location.x < 0 {
        moveStick.moveJoystick(touch: location)
    }
    if if location.x > 0 {
        shootStick.waponRotate(touch: location)
    }
}

这应该可以解决您遇到的所有错误。

【讨论】:

  • 无意冒犯,但一些简单的问题不应该回答。有时新开发人员需要磨合自己的齿轮来解决这些简单的问题。如果我们用勺子喂这些简单的问题,他们将不会培养编程所需的批判性思维技能,而是会求助于 SO 来解决每个问题。
  • ?‍♂️ 好吧,现在我知道了。知道是成功的一半。我想知道你为什么还没有回答这个问题。看起来很简单,你是精灵套件大师。
  • 是的,对于简单的问题,我让他们花一些时间来回答他们自己的问题。它允许他们学习,并在他们学习时获得代表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多