1

I'm making an app where you controll the ship's movement with a joystick, and fires a bullet with a shoot button. These two functions works great separately but if i tap the shooting button while i move the ship with the joystick, the joystick becomes uncontrolable. So what i need is that the joystick functions normally when i tap anywhere on the screen.

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    let player=SKSpriteNode(imageNamed: "prez")
    let joystickback=SKSpriteNode(imageNamed: "0")
    let joystickbutton=SKSpriteNode(imageNamed: "1")
    let shootbutton=SKSpriteNode(imageNamed: "1")
    var joystickinuse=false
    var velocityX : CGFloat=0.0
    var velocityY : CGFloat=0.0

    override func didMove(to view: SKView) {

        let background=SKSpriteNode(imageNamed: "back")
        background.size=self.size
        background.position=CGPoint(x: self.size.width/2, y: self.size.height/2)
        background.zPosition=0
      self.addChild(background)
        //joystick back
        self.joystickback.position=CGPoint(x: (self.view?.frame.width)!*0.1, y: (self.view?.frame.height)!/5)
        self.joystickback.zPosition=1
        //jostick butt
        self.joystickbutton.position=CGPoint(x: (self.view?.frame.width)!*0.1, y: (self.view?.frame.height)!/5)
        self.joystickbutton.zPosition=2
        //shootingbutton
        self.shootbutton.position=CGPoint(x: (self.view?.frame.width)!*0.9, y: (self.view?.frame.height)!/5)
        self.shootbutton.zPosition=2
        self.shootbutton.setScale(0.4)

       self.ship.position=CGPoint(x: (self.view?.frame.width)!/2, y: (self.view?.frame.height)!/2)
        self.ship.setScale(0.2)
        self.ship.zPosition=3
        self.joystickback.setScale(0.5)
        self.joystickbutton.setScale(0.5)
        self.addChild(shootbutton)
        self.addChild(ship)
        self.addChild(joystickback)
        self.addChild(joystickbutton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches{
            let location=t.location(in: self)

            if (joystickbutton.frame.contains(location)){
                joystickinuse=true
            }
        }
    }

    func touchMoved(touch:UITouch){
        let location=touch.location(in: self)
        if (joystickinuse){
            let vecktor=CGVector(dx: location.x-joystickback.position.x, dy: location.y-joystickback.position.y)
            let angle=atan2(vecktor.dy, vecktor.dx)
            let DistanceFromCenter=CGFloat(joystickback.frame.size.height/2)
            let DistanceX=CGFloat(sin(angle-CGFloat(M_PI)/2)*DistanceFromCenter)
           let DistanceY=CGFloat(cos(angle-CGFloat(M_PI)/2)*DistanceFromCenter)
            if(joystickback.frame.contains(location)){
                joystickbutton.position=location
            }
            else{
                joystickbutton.position=CGPoint(x: joystickback.position.x-DistanceX, y: joystickback.position.y+DistanceY)
            }
            velocityX=(joystickbutton.position.x-joystickback.position.x)/10
            velocityY=(joystickbutton.position.y-joystickback.position.y)/10
        }
    }

    func firebullet(){
        let bullet=SKSpriteNode(imageNamed: "rocket")
        bullet.position=urhajo.position
        bullet.setScale(0.2)
        bullet.zPosition=1
        self.addChild(bullet)
        let movebullet=SKAction.moveTo(x: self.size.width+bullet.size.width, duration: 1)
        let deletebullet=SKAction.removeFromParent()
        let bulletsequence=SKAction.sequence([movebullet,deletebullet])
        bullet.run(bulletsequence)  
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches{
            self.touchMoved(touch: t)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if(joystickinuse){
            movementOver()
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        if(joystickinuse){
            movementOver()
        }
    }

    func movementOver(){
        let moveBack=SKAction.move(to: CGPoint(x: joystickback.position.x,y: joystickback.position.y), duration: TimeInterval(floatLiteral:0.1))
        moveBack.timingMode = .linear
        joystickbutton.run(moveBack)
        joystickinuse=false
        velocityY=0
        velocityX=0
    }

  override func update(_ currentTime: TimeInterval) {
        if(ship.position.x>=(self.view?.frame.width)!+urhajo.frame.width/2){


        }
        else{ self.ship.position.x+=velocityX

        }

         self.ship.position.y+=velocityY
    }
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
B.Ticca
  • 23
  • 5
  • you should strongly consider cleaning up the spaces and line breaks on your code before posting your question. If not for yourself do it for people they are trying to decipher what is happening in your code. not only was the code hard to read because of this but you were also missing two closing brackets in the code. – Ron Myschuk Jan 10 '18 at 19:06

1 Answers1

2

try setting this is your Scene code

func didMove(to view: SKView) {

    self.view!.isMultipleTouchEnabled = true
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32