简体   繁体   English

用spritekit迅速4个两个dpad

[英]swift 4 two dpads with spritekit

A screenshot of my game I want to make a game (with Spritekit) where you can use the left dpad to move the player around in a tile map which already works. 我的游戏的屏幕截图,我想制作一个游戏(使用Spritekit),您可以在其中使用左dpad在已经有效的图块地图中移动玩家。 With the right one you can aim at opponents which works too. 有了正确的对手,您就可以针对同样有效的对手。 Althought I enabled multiple touch only one controller works at the same time. 虽然我启用了多点触控,但只有一个控制器可以同时工作。

Joystick means the same as dpad. 游戏杆的含义与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

    }
}

As KnightOfDragon pointed out, you are using .first . 作为KnightOfDragon指出的那样,你正在使用.first That means your code is looking for the first touch in your scene and then going from there. 这意味着您的代码正在寻找场景中的第一个接触点,然后从那里开始。 Your game won't let you use both joysticks at the same time because you won't let them be used at the same time. 您的游戏将不允许您同时使用两个操纵杆,因为您不会同时使用它们。

These if-statements that you have in your various touch functions: 您在各种触控功能中具有的这些if语句:

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

Should look like this: 应该看起来像这样:

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)
    }
}

This should fix any errors you have. 这应该可以解决您遇到的所有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM