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. 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.
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
. 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:
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.