R. Khaira
R. Khaira

Reputation: 131

Changing speed of node / Swift 3.0

I am trying to change the speed of my node. Right now my node(s) just move around the screen wherever you touch - very basic for testing. I just wanted my first node (bubble1 - refer to code) to move a little quicker so I can change the speed again and so on.. Thanks for your help.

*For now I just want to change the speed of "bubble1", nothing else. I looked at other posts on how to do this, but none very really up to date. So I posted my own. =)

import SpriteKit
import GameplayKit

class GameScene: SKScene {

//var simpleS = SKSpriteNode()
var ball = SKSpriteNode(imageNamed: "Ball")
var bubble = SKSpriteNode(imageNamed: "square")
var bubble1 = SKSpriteNode(imageNamed: "square")
var bubble2 = SKSpriteNode(imageNamed: "square")
var bubble3 = SKSpriteNode(imageNamed: "square")
var bubble4 = SKSpriteNode(imageNamed: "square")


override func didMove(to view: SKView) {

    //simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode

    //simpleS.physicsBody?.affectedByGravity = true
    //simpleS.setScale(3.0)

    ball.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    ball.setScale(0.1)
    //self.addChild(ball)

    bubble.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble.setScale(0.3)
    self.addChild(bubble)

    bubble1.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble1.setScale(0.3)
    self.addChild(bubble1)

    bubble2.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble2.setScale(0.3)
    self.addChild(bubble2)

    bubble3.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble3.setScale(0.3)
    self.addChild(bubble3)

    bubble4.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble4.setScale(0.3)
    self.addChild(bubble4)     
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        
    for touch in touches {
        let location = touch.location(in: self)

        //simpleS.run(SKAction.moveTo(x: location.x, duration: 0.5))
        //simpleS.run(SKAction.moveTo(y: location.y, duration: 0.5))

        ball.run(SKAction.moveTo(x: location.x, duration: 0.0))
        ball.run(SKAction.moveTo(y: location.y, duration: 0.0))

        bubble.run(SKAction.moveTo(x: location.x, duration: 0.3))
        bubble.run(SKAction.moveTo(y: location.y, duration: 0.3))

        bubble1.run(SKAction.moveTo(x: location.x, duration: 0.4))
        bubble1.run(SKAction.moveTo(y: location.y, duration: 0.4))

        bubble2.run(SKAction.moveTo(x: location.x, duration: 0.5))
        bubble2.run(SKAction.moveTo(y: location.y, duration: 0.5))

        bubble3.run(SKAction.moveTo(x: location.x, duration: 0.6))
        bubble3.run(SKAction.moveTo(y: location.y, duration: 0.6))

        bubble4.run(SKAction.moveTo(x: location.x, duration: 0.8))
        bubble4.run(SKAction.moveTo(y: location.y, duration: 0.8))
    }
}

Upvotes: 2

Views: 529

Answers (1)

Confused
Confused

Reputation: 6288

There are a couple of ways you can do this.

  1. Change the duration, as this will speed it up, but I don't think this is what you're asking. I think you want to know how to speed up an SKAction....

  2. Change the rate of execution of a SKAction via its speed property

The speed property is documented here.

You'll need to do a couple of things to make this value accessible and uniquely addressable for bubble1

  1. Make the SKAction on bubble1 unique, so you can control it. There's two ways to to this:

    • give it a unique name in its '.name' property, look up and find that
    • have a reference to bubble1 and address its SKAction (a
  2. Tell the SKAction affecting bubble1 to run faster by changing its .speed property

Or... you can be lazy and just adjust the speed value of all actions on any given node, with this: https://developer.apple.com/reference/spritekit/sknode/1483036-speed

Upvotes: 1

Related Questions