So my problem is that im making a game using Sprite Kit and Ive implemented a joystick that I need in the game and first I wanted to make a speed cap when you drag the joystick and also when I put the joystick as a child to the camera instead of putting it on the map for testing it both wouldn't work. I just started IOS Development a little help would be very appreciated.
Here is my Code for the JoyStick:
import UIKit import SpriteKit import Foundation
class Joystick:SKNode{
let base = SKSpriteNode(imageNamed: "JBase")
let stick = SKSpriteNode(imageNamed: "JStick")
var stickActive:Bool = false
var xJoystickDelta = CGFloat()
var yJoystickDelta = CGFloat()
var v:CGVector = CGVector(dx: 0,dy: 0)
func moveStick(location:CGPoint){
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)
let length: CGFloat = base.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * length
let yDist: CGFloat = cos(angle - 1.57079633) * length
xJoystickDelta = location.x - base.position.x
yJoystickDelta = location.y - base.position.y
if (base.frame.contains(location)) {
stick.position = location
} else {
stick.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
} // End stick active test
}// End moveStick Function
func endMoveStick(){
stick.position = base.position
self.xJoystickDelta = 0
self.yJoystickDelta = 0
}// End moveStick fun
func getVector() -> CGVector { return self.v}
This is what I use to move the player:
The force in the player class:
func vectorForce(xDelta:CGFloat, yDelta:CGFloat){
let xAdd = xSpeed * xDelta
let yAdd = ySpeed * yDelta
self.position.x += xAdd
self.position.y += yAdd
}// End of vectorForce
playerBase.vectorForce(xDelta: joyStick.xJoystickDelta,yDelta: joyStick.yJoystickDelta)