fikricanc
fikricanc

Reputation: 119

SpriteKit increasing speed of move action

I have a simple game that two lines moving from top to bottom of the screen. There is a fixed space between those two lines and a ball has to pass through in that space otherwise game is over.

I have no problem with the basics of the game, I can add two lines, a space which has different position every time but I also want to increase the moving speed of lines. I tried to use a timer and increase TimeInterval of SKAction.move but since lines which added later is faster, horizontal space between lines gets smaller which I don't want to happen.

This is the function that adds two lines and move them to bottom of the screen.

func addLines() {

    let lineNodeFirst = SKSpriteNode(color: .red, size: CGSize(width: random(min: 100, max: 400), height: 10.0))
    lineNodeFirst.position = CGPoint(x: -size.width / 2, y: 0 + size.height / 2 + 100)
    lineNodeFirst.anchorPoint = CGPoint.zero

    let actionMove = SKAction.move(to: CGPoint(x: lineNodeFirst.position.x, y: 0 - size.height / 2), duration: TimeInterval(2.3))
    let actionMoveDone = SKAction.removeFromParent()

    lineNodeFirst.run(SKAction.sequence([actionMove, actionMoveDone]))

    let centerPoint = CGPoint(x: lineNodeFirst.size.width / 2 - (lineNodeFirst.size.width * lineNodeFirst.anchorPoint.x), y: lineNodeFirst.size.height / 2 - (lineNodeFirst.size.height * lineNodeFirst.anchorPoint.y))
    lineNodeFirst.physicsBody = SKPhysicsBody(rectangleOf: lineNodeFirst.size, center: centerPoint)

    lineNodeFirst.physicsBody?.isDynamic = true
    lineNodeFirst.physicsBody?.categoryBitMask = 2
    lineNodeFirst.physicsBody?.contactTestBitMask = 1
    lineNodeFirst.physicsBody?.collisionBitMask = 0

    self.addChild(lineNodeFirst)

    let spaceBetweenNodes: CGFloat = 150
    let lineNodeSecondWidth: CGFloat = size.width - lineNodeFirst.size.width - spaceBetweenNodes

    let lineNodeSecond = SKSpriteNode(color: .red, size: CGSize(width: lineNodeSecondWidth, height: 10.0))
    lineNodeSecond.anchorPoint = CGPoint.zero
    let lineNodeSecondX = lineNodeFirst.position.x + lineNodeFirst.size.width + spaceBetweenNodes
    lineNodeSecond.position = CGPoint(x: lineNodeSecondX, y: lineNodeFirst.position.y)

    let actionMoveSecond = SKAction.move(to: CGPoint(x: lineNodeSecond.position.x, y: 0 - size.height / 2), duration: TimeInterval(2.3))

    lineNodeSecond.run(SKAction.sequence([actionMoveSecond, actionMoveDone]))

    let centerPointSecond = CGPoint(x: lineNodeSecond.size.width / 2 - (lineNodeSecond.size.width * lineNodeSecond.anchorPoint.x), y: lineNodeSecond.size.height / 2 - (lineNodeSecond.size.height * lineNodeSecond.anchorPoint.y))
    lineNodeSecond.physicsBody = SKPhysicsBody(rectangleOf: lineNodeSecond.size, center: centerPointSecond)

    lineNodeSecond.physicsBody?.isDynamic = true
    lineNodeSecond.physicsBody?.categoryBitMask = 2
    lineNodeSecond.physicsBody?.contactTestBitMask = 1
    lineNodeSecond.physicsBody?.collisionBitMask = 0

    self.addChild(lineNodeSecond)

}

I share a demo of my app which has the basics. Any suggestion for increasing move speed of lines?

Demo Video

Upvotes: 4

Views: 462

Answers (1)

Ron Myschuk
Ron Myschuk

Reputation: 6071

I cannot think of a way of speeding up the lines without increasing the speed on all the lines. without increasing the speed to all the lines you are going to have the problem of the shrinking gap between the lines or else you will have to transverse through all the lines on the screen and re-write their actions at the same time.

What you could do which is probably considerably easier is to create 2 larger panels that scroll through your scene top to bottom and reset when scrolls off the page at the bottom, creating a scrolling background of sorts. Then apply your bars to those panels. you now will have the effect of the bars moving down the screen. when you want to increase the speed at which the game is getting played all you have to do is increase the speed on the 2 panels.

In my demo image I've added multiple colors just to show you the breaks between the panels. but if those background colors were removed you wouldn't even realize that it was the panels moving and not the bars ;)

As an added bonus you could create the panels as a class and the bars as a class and assign the bars to the panels. when it comes time to reset the panel to the top you could just adjust the 2 inner opening points of the bars to a new gap. This way you wouldn't be creating these bars dynamically on the fly.

enter image description here

Upvotes: 2

Related Questions