Avi Rok
Avi Rok

Reputation: 558

Sprite move once time in random positions

I tried to move sprites move randomly on screen but the sprite move once time to random position and stop to move

Here I call to make shapes func with timer

    //Make shape Timer
func makeshapetimer () {
    maketimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(makerandomShape), userInfo: nil, repeats: true)
}

//Make random shape
func makerandomShape () {

    //Check if have more than 12 shapes
    if shapesamount <= 12 {

        let sprite = shape.copy() as! SKShapeNode
        sprite.name = "Shpae \(shapesamount)"
        sprite.position = CGPoint(x: frame.minX - sprite.frame.width, y: frame.maxY)

        shapes.addChild(sprite)

        shapesamount += 1

        moveRandom(node: sprite)
    }
}

And here I make a random position action and repeat the action forever but is run only once time per shape

//Move shape radmonly
func moveRandom (node: SKShapeNode) {

    move = SKAction.move(to: CGPoint(x: CGFloat.random(min: frame.minX, max: frame.maxX), y: CGFloat.random(min: frame.minY, max: frame.maxY)), duration: shapespeed)

    node.run(SKAction.repeatForever(move))
}

Upvotes: 0

Views: 77

Answers (1)

DonMag
DonMag

Reputation: 77433

Your moveRandom function is called only once per sprite.

Here's what you tell it to do:

  • get a random x,y position --- let's say it got 120,200
  • move to 120,200 --- and repeat moving to 120,200 forever

So, the sprite dutifully moves to that random position, and keeps moving to that position. It never goes back to its starting point, and it never gets a new position to move to.

If you want the sprites to keep moving to new random positions, you need to create a new random position each time the current move finishes.

Upvotes: 1

Related Questions