Gage
Gage

Reputation: 61

how to run a SKAction in the direction of a joystick's degree

i am creating a game and i am having trouble making my bullets spawn in the direction of my joystick. here is the method for my bullets

func spawnBullets(){

    let bullet = SKSpriteNode(imageNamed: "bullet.png") 
    bullet.zPosition = -5
    bullet.position = CGPointMake(hero.position.x, hero.position.y)        
    bullet.xScale = 0.25
    bullet.yScale = 0.25
    bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
    bullet.physicsBody?.categoryBitMask = PhysicsCategory.bullet
    bullet.physicsBody?.contactTestBitMask = PhysicsCategory.enemy
    bullet.physicsBody?.affectedByGravity = false
    bullet.physicsBody?.dynamic = false
    let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)     
    let actionDone = SKAction.removeFromParent()
    bullet.runAction(SKAction.sequence([action, actionDone]))
    self.addChild(bullet)
}

here is how i am calling the method in my

 override func didMoveToView(view: SKView) {

_ = NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, 
selector: Selector ("spawnBullets"), userInfo: nil, repeats: true)
}

and here is how i coded my touches moved function (i believe that is the method that i need to edit)

override func touchesMoved(touches: Set<UITouch>, withEvent event: 
UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let v = CGVector(dx: location.x - joystickBase.position.x, dy: 
        location.y - joystickBase.position.y)
        let angle = atan2(v.dy, v.dx)
        let deg = angle * CGFloat(180 / M_PI)
        let length: CGFloat = joystickBase.frame.size.height / 2
        let xDist:CGFloat = sin(angle - 1.5709633) * length
        let yDist:CGFloat = cos(angle - 1.5709633) * length
        print(deg + 180)

        if (stickActive == true) {

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

            joystick.position = location

        }else{

joystick.position = CGPointMake(joystickBase.position.x - 
xDist, joystickBase.position.y + yDist)
            }

        }
    }
}

Upvotes: 2

Views: 81

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16827

You are only moving the bullets in a vertical direction:

let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)

Not sure if this is a twin stick game or not.

To get sprites to go based on the joystick, just do the following

let accel = 10 //accel is the # of pixels the sprite moves per frame, do not use moveTo, because moveTo will change the speeds of your sprite based on distance   
let action = SKAction.moveByX(joystick.x * accel,y:joystick.y * accel,duration:1)

This of course is based on the understanding that your joystick coordinates go from -1.0 to 1.0 on each axis, and that point up or right gets you a 1.0 result

I would not even do this approach though, SKActions are not a good choice for things that are not automated. You should be updating the position of the sprite at some point in your update phase

sprite.position.x += joystick.x * accel
sprite.position.y += joystick.y * accel

Upvotes: 1

Related Questions