My question is how can i incorporate my sprite to have a continuous movement along with the joystick? I want the player to be moving at a certain speed the whole time, and then whenever the user moves the joystick a certain angle then the player will just follow along with it.
ex: Sprite automatically moves a certain speed, ex; speed = CGFloat(100), then it will continue at this speed, by speed the sprite just automatically is moving in a direction, I don't have any idea on how to implement it so that my player is moving at a constant rate, and how to change the direction is moves with the joysticks.
Currently with my code i'm able to rotate the sprite.
I appreciate all the help you guys give, thank you in advance.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
if (ball.frame.contains(touchLocation)) {
stickActive = true
} else {
stickActive = false
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
if isAlive == true{
joyStickMoved()
}
if isAlive == false{
player.position.x = -300
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
move.timingMode = .easeOut
ball.run(move)
}
}
func joyStickMoved() {
if (stickActive == true) {
let v = CGVector(dx: touchLocation.x - base.position.x, dy: touchLocation.y - base.position.y)
let angle = atan2(v.dy, v.dx)
// let degree = angle * CGFloat( 180 / Double.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
// TODO // xJoystick = touchLocation.x - base.position.x
// yJoystick = touchLocation.y - base.position.y
if (base.frame.contains(touchLocation)) {
ball.position = touchLocation
} else {
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
player.zRotation = angle - 1.57079633
}
}
override func update(_ currentTime: TimeInterval) {
if isAlive == false && stickActive == false {
lblMain.text = "Game Over"
player.position.x = -300
waitThenResetTheGame()
}
if stickActive == true {
moveNodeToLocation()
}
}
func spawnPlayer(){
player = SKSpriteNode(color: offWhiteColor, size: playerSize)
player.position = CGPoint(x: self.frame.midX, y: 130)
player.physicsBody = SKPhysicsBody(rectangleOf: (player.size))
player.physicsBody?.affectedByGravity = false
player.physicsBody?.categoryBitMask = physicsCategory.player
player.physicsBody?.contactTestBitMask = physicsCategory.fallingBlock
player.physicsBody?.isDynamic = false
player.physicsBody?.allowsRotation = false
player.physicsBody?.angularVelocity = 5
player.physicsBody?.angularDamping = 0
player.name = "player"
self.addChild(player)
setupFollower()
}