Hunter Hunter - 5 months ago 32
Swift Question

How to move a node from a joystick I created in Xcode SWIFT

enter image description hereHey in the code i figured out how to make the ship have the same angle as the direction/angle that the joystick is pointing but i don't know how to actually move the ship left,right,up,down based on the position of the joy stick

Code:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let base = SKSpriteNode(imageNamed:"JoyStickBase")
let ball = SKSpriteNode(imageNamed:"JoyStickHandle")
let ship = SKSpriteNode(imageNamed:"Ship")

var stickActive:Bool = false


override func didMoveToView(view: SKView) {
/* Setup your scene here */
//---Setting-up-World----------------------
self.backgroundColor = SKColor.blackColor()
self.anchorPoint = CGPointMake(0.5, 0.5)
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
//--Positing-the-Base-under-the-Joystick---
base.size = CGSize(width: 100, height: 100)
base.position = CGPointMake(0, -200)
self.addChild(base)
//----Positing-the-Ball/Joystick-----------
ball.size = CGSize(width: 50, height: 50)
ball.position = base.position
self.addChild(ball)
//---Setting-Up-Ships-Position/PhysicsBody---------------------
ship.position = CGPointMake(0, 200)
ship.physicsBody?.dynamic = true
ship.physicsBody?.allowsRotation = true
ship.physicsBody?.affectedByGravity = true
ship.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship"), size: ship.size)
self.addChild(ship)

//ball.alpha = 0.4
//base.alpha = 0.4

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */


for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)



if (CGRectContainsPoint(base.frame, location)) {

stickActive = true






} else {



stickActive = false

}
}
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(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 xDist:CGFloat = sin(angle - 1.57879633) * length
let yDist:CGFloat = cos(angle - 1.57879633) * length


if (CGRectContainsPoint(base.frame, location)) {

ball.position = location





} else {

ball.position = CGPointMake( base.position.x - xDist, base.position.y + yDist)



}

ship.zRotation = angle - 1.57879633


var shipRotation : CGFloat = ship.zRotation
var calcRotation : Float = Float(angle - 1.57879633) + Float(M_PI_2);
let intensity : CGFloat = 10.0 // put your value
let xVelocity = intensity * CGFloat(cosf(calcRotation))
let yVelocity = intensity * CGFloat(sinf(calcRotation))
let vector : CGVector = CGVectorMake(xVelocity, yVelocity)
//Apply force to spaceship
ship.physicsBody?.applyForce(vector)


}// ends stackActive


}


}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (stickActive == true) {

let move:SKAction = SKAction.moveTo(base.position, duration: 0.1)
move.timingMode = .EaseOut

ball.runAction(move)

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

}
}

Answer

I assume that your ship is set up as a SKPhysicsBody, so you would do something like this:

var shipRotation : CGFloat = ship.zRotation
var calcRotation : Float = Float(spaceShipRotation) + Float(M_PI_2);

let intensity : CGFloat = 10.0 // put your value
let xVelocity = intensity * CGFloat(cosf(calcRotation))
let yVelocity = intensity * CGFloat(sinf(calcRotation))
let vector : CGVector = CGVectorMake(xVelocity, yVelocity)

//Apply force to spaceship
spaceShipNode.physicsBody?.applyForce(vector)

I believe this should work, although I haven't tested it.

Comments