swift 4两个带spritekit的dpads

问题描述 投票:0回答:1

A screenshot of my game我想制作一个游戏(使用Spritekit),你可以使用左边的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

    }
}
swift sprite-kit multi-touch d-pad
1个回答
0
投票

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

这应该可以解决您遇到的任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.