Rae Tucker
Rae Tucker

Reputation: 523

Is there a way to spawn nodes uniformly?

I have a game where circles are moving up and down lines like so:

https://i.sstatic.net/5zKS4.png

, and Im using a .plist file to generate them, but its a little monotonous.Is there another way to generate several columns of circles uniformly without using a .plist file? Will post code if necessary.

Upvotes: 1

Views: 61

Answers (1)

Trent Sartain
Trent Sartain

Reputation: 446

You could write a function like this:

func createCirclesOnLine(line : CGFloat){
    //Set this to however low you want the circles to start.
    var currentY : CGFloat = -20.0

    //Set this to however high you want the circles to go
    let maxY = self.size.height + 100

    //Set this to the spacing you want between the circles
    let spacing : CGFloat = 50

    while currentY < maxY {
        let circle = SKShapeNode(circleOfRadius: 20)
        circle.position = CGPoint(x: line, y: currentY)
        self.addChild(circle)
        currentY += spacing
    }
}

And then pass it the line (think longitude) of wherever you want the circles to be drawn:

let line = self.size.width / 2
createCirclesOnLine(line)

Is that what you're looking for?

EDIT: To answer your question about how to use this multiple times:

If you want multiple lines of circles on the screen, you can simply decide what lines (again, think longitude) you want to place the circles on. For instance, if I wanted three equally spaced lines of circles on the screen, I would do the following:

let line = self.size.width / 4
createCirclesOnLine(line)
createCirclesOnLine(line * 2)
createCirclesOnLine(line * 3)

If you try this and don't see all of the lines, you might need to add this

scene.size = skView.bounds.size

to the viewDidLoad method in your ViewController.swift right under

skView.ignoresSiblingOrder = true

since you are running the app in portrait mode.

Upvotes: 1

Related Questions