UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SKSpriteNode speed values are not being updated or applied properly.

Forums > Swift

@Jefe  

What I'm trying to do is update my SKSpriteNodes so I can change their scrolling speeds dynamically, however they aren't really working consistently. I didn't include the code, but I have another method with a switch case that sets the value of platformSpeed whenever the state is changed (in this case, the switch case is changed with UIButtons). In my code I have an SKSpriteNode array and a platformSpeed property that includes didSet so my value is updated properly.

In my method to create the platforms, I grouped my SpriteNodes into platformGroup then looped through them with addChild(). Not sure why it's acting this way but here's a quick video of what it looks like in action:

demonstration clip

So with the buttons I'm changing the switch case, and as you can see, not all of the nodes speeds are updating properly and some get faster than others and eventually pass them. I need them to stay equal distance between each other.

Now here's my code:

class GameScene: SKScene, SKPhysicsContactDelegate {

var platformGroup = [SKSpriteNode]()
var platformSpeed: CGFloat = 1.0 {
    didSet {
        for platforms in platformGroup {
            platforms.speed = platformSpeed 
        }
    }
}

let platformTexture = SKTexture(imageNamed: "platform")
var platformPhysics: SKPhysicsBody!

func createPlatforms() {

    let platformLeft = SKSpriteNode(texture: platformTexture)
    platformLeft.physicsBody = platformPhysics.copy() as? SKPhysicsBody
    platformLeft.physicsBody?.isDynamic = false
    platformLeft.scale(to: CGSize(width: platformLeft.size.width * 4, height: platformLeft.size.height * 4))
    platformLeft.zPosition = 20

    let platformRight = SKSpriteNode(texture: platformTexture)
    platformRight.physicsBody = platformPhysics.copy() as? SKPhysicsBody
    platformRight.physicsBody?.isDynamic = false
    platformRight.scale(to: CGSize(width: platformRight.size.width * 4, height: platformRight.size.height * 4))
    platformRight.zPosition = 20

    let scoreNode = SKSpriteNode(color: UIColor.clear, size: CGSize(width: frame.width, height: 32))
    scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
    scoreNode.physicsBody?.isDynamic = false
    scoreNode.name = "scoreDetect"
    scoreNode.zPosition = 40

    platformGroup = [platformLeft, platformRight, scoreNode]

    let yPosition = frame.width - platformRight.frame.width

    let max = CGFloat(frame.width / 4)
    let xPosition = CGFloat.random(in: -80...max)

    let gapSize: CGFloat = -50

    platformLeft.position = CGPoint(x: xPosition + platformLeft.size.width - gapSize, y: -yPosition)
    platformRight.position = CGPoint(x: xPosition + gapSize, y: -yPosition)
    scoreNode.position = CGPoint(x: frame.midX, y: yPosition - (scoreNode.size.width / 1.5))

    let endPosition = frame.maxY + (platformLeft.frame.height * 3)

    let moveAction = SKAction.moveBy(x: 0, y: endPosition, duration: 7)
    let moveSequence = SKAction.sequence([moveAction, SKAction.removeFromParent()])

    for platforms in platformGroup {
        addChild(platforms)
        platforms.run(moveSequence)
    }

    platformCount += 1
}

func loopPlatforms() {
    let create = SKAction.run { [unowned self] in
        self.createPlatforms()
        platformCount += 1
    }

    let wait = SKAction.wait(forDuration: 1.1)
    let sequence = SKAction.sequence([create, wait])
    let repeatForever = SKAction.repeatForever(sequence)

    run(repeatForever)
}

Sorry for the bulk of code, but I wanted to show as much as possible. Also the concepts are borrowed from Project36 and essentially is doing what createRocks() and startRocks() does, but verically.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.