Virtual Joystick question

This topic contains 5 replies, has 2 voices, and was last updated by  Diego Aguilar 1 year, 1 month ago.

  • October 11, 2016 at 2:00 pm #156873

    How do I make my joystick move with my SKCamera. I’ve tried making it a child of the SKCamera but it no longer functions. I can see it but it won’t function.

  • October 11, 2016 at 2:44 pm #156875

    When you touchdown on it, run a print statement and see what coordinates you’re getting, compared to when the joystick wasn’t a child of the camera. You might need to convert the coordinates from the camera space or instead of making the joystick a child of the camera, you could just tie it to the camera programmatically. For example…

    joystick.position = CGPoint(x: camera.position.x, y:joystick.position.y)

    • October 11, 2016 at 3:42 pm #156879

      Below is the code that I wrote following your tutorial. As my gameHero moves through the scene the joystick stops working I can always see it but it doesn’t work.

      import Foundation
      import SpriteKit

      enum BodyType:UInt32 {

      case player = 1
      case ground = 2
      }

      class LevelOneScene: SKScene, SKPhysicsContactDelegate {

      // let for your buttons
      // let moveLeftBtn = SKSpriteNode(imageNamed: “LeftBtn”)
      // let moveRightBtn = SKSpriteNode(imageNamed: “RightBtn”)
      let jumpBtn = SKSpriteNode(imageNamed: “JumpBtn”)
      let attacKBtn = SKSpriteNode(imageNamed: “AttackBtn”)

      let base = SKSpriteNode(imageNamed:”Base”)
      let ball = SKSpriteNode(imageNamed:”Ball”)

      // let for the player
      let gameHero = Player(imageNamed: “Idle 0”)
      var playerSpeedX:CGFloat = 0
      var playerSpeedY:CGFloat = 0

      var jumpAmount:CGFloat = 0
      var maxJump:CGFloat = 22

      var attackAmount:CGFloat = 0
      var maxAttack:CGFloat = 8

      var walkAction:SKAction?
      var idleAction:SKAction?
      var attackAction:SKAction?
      var jumpAction:SKAction?

      var maxSpeed:CGFloat = 5

      var isAttacking:Bool = false
      var isJumping:Bool = false
      var doubleJumpAlreadyUsed:Bool = false
      var walkingSlow:Bool = false

      // Camera
      var gameCamera:SKCameraNode?

      var stickActive:Bool = false

      var strictCompassMovements:Bool = false

      // try to move character

      override func didMove(to view: SKView) {

      // Initial Setup
      physicsWorld.contactDelegate = self
      self.view?.isMultipleTouchEnabled = true
      self.backgroundColor = SKColor.black

      gameHero.position = CGPoint(x: 0, y: 0)
      gameHero.setScale(fabs(0.5))
      self.addChild(gameHero)

      //self.addChild(base)

      base.zPosition = 99
      base.position = CGPoint(x: -400, y: -200)

      //self.addChild(ball)
      ball.zPosition = 100
      ball.position = base.position

      gameCamera = self.childNode(withName: “MainCamera”) as? SKCameraNode!
      gameCamera?.addChild(base)
      gameCamera?.addChild(ball)

      createGrounds()

      }

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      /* Called when a touch begins */

      for touch in (touches ) {

      let location = touch.location(in: self)

      if atPoint(location).name == “AttackBtn” {

      stickActive = false
      gameHero.attack()

      } else if atPoint(location).name == “JumpBtn” {

      stickActive = false
      gameHero.jump()

      } else if (location.x < 5000) {

      stickActive = true

      gameHero.startWalk()

      ball.alpha = 0.4
      base.alpha = 0.4

      } else {
      print( “touch down was on right side of screen, you maybe missed a button”)
      stickActive = false

      }
      }
      }

      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

      for touch in (touches ) {
      let location = touch.location(in: self)

      if (stickActive == true) {

      let v = CGVector(dx: location.x – base.position.x, dy: location.y – base.position.y)
      let angle = atan2(v.dy, v.dx)

      // let deg = angle * CGFloat( 180 / M_PI)
      //println( deg + 180 )

      let length:CGFloat = base.frame.size.height / 2

      //let length:CGFloat = 40

      let xDist:CGFloat = sin(angle – 1.57079633) * length
      let yDist:CGFloat = cos(angle – 1.57079633) * length

      if (base.frame.contains(location)) {

      ball.position = location

      } else {

      ball.position = CGPoint(x: base.position.x – xDist, y: base.position.y + yDist)

      }

      // set up the speed

      let multiplier:CGFloat = 0.1

      gameHero.playerSpeedX = v.dx * multiplier

      gameHero.adjustXSpeedAndScale()

      } // ends stickActive test

      }
      }

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

      resetJoyStick()

      if (stickActive == true) {

      // let go for joystick

      gameHero.stopWalk()
      gameHero.playerSpeedX = 0

      } else if (stickActive == false) {

      //assumes let go of a button
      }
      }

      func resetJoyStick(){

      let move:SKAction = SKAction.move(to: base.position, duration: 0.2)
      move.timingMode = .easeOut

      ball.run(move)
      }

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

      gameCamera?.position = gameHero.position

      if ( self.isPaused == false) {

      gameHero.update()

      }

      }

      func createGrounds() {

      let ground = SKSpriteNode(imageNamed: “Ground”)
      ground.name = “Ground”
      ground.anchorPoint = CGPoint(x: 0.5, y: 0.5)
      ground.position = CGPoint(x: -200, y: -300)
      ground.zPosition = 10
      ground.physicsBody = SKPhysicsBody(rectangleOf: ground.size)
      ground.physicsBody?.affectedByGravity = true
      ground.physicsBody?.isDynamic = false
      ground.physicsBody?.categoryBitMask = BodyType.ground.rawValue
      ground.physicsBody?.collisionBitMask = BodyType.player.rawValue
      ground.physicsBody?.contactTestBitMask = BodyType.player.rawValue
      self.addChild(ground)

      let ground2 = SKSpriteNode(imageNamed: “Ground”)
      ground2.name = “Ground”
      ground2.anchorPoint = CGPoint(x: 0.5, y: 0.5)
      ground2.position = CGPoint(x: 1200, y: -250)
      ground2.zPosition = 10
      ground2.physicsBody = SKPhysicsBody(rectangleOf: ground.size)
      ground2.physicsBody?.affectedByGravity = true
      ground2.physicsBody?.isDynamic = false
      ground2.physicsBody?.categoryBitMask = BodyType.ground.rawValue
      ground2.physicsBody?.collisionBitMask = BodyType.player.rawValue
      ground2.physicsBody?.contactTestBitMask = BodyType.player.rawValue
      self.addChild(ground2)
      }
      }

  • October 12, 2016 at 9:17 am #156912

    Because you changed the ball and base of the joystick to inside the camera’s coordinate system, your touchesMoved statement needs to get the touch location from inside that space. In other words, the touch location.x and .y values are not the same within the camera as they are in the main scene. You can probably just change this line…

    let location = touch.location(in: self)

    to

    let location = touch.location(in: self.gameCamera )

  • October 14, 2016 at 2:07 am #156979

    Problem solved thank you for help.

You must be logged in to reply to this topic.

Download the new CartoonSmart TV App - Organize all your FREE and Premium Lessons

Get it!