fredericdnd
fredericdnd

Reputation: 1008

Swift - Good random node generation SpriteKit

i try to creating a game where you need to avoid objects (nodes), and I wan't to generate these node at a random position (out of the screen, on the right) and with an action, the nodes cross the screen to the left. But how can I create a good random generation, so that there is no two nodes on the same position or too close.

I have a timer in the didMoveToView method :

nodeGenerationTimer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("nodeGeneration"), userInfo: nil, repeats: true)

And the function to generate the node every 2 seconds :

func nodeGeneration() {


    var randomX = CGFloat(Int(arc4random()) % sizeX)
    println(randomX)


    let node = SKSpriteNode(imageNamed: "node")
    node.anchorPoint = CGPointMake(0.5, 0.5)
    node.size.width = self.size.height / 8
    node.size.height = bin.size.width * (45/34)
    node.position = CGPointMake(self.size.width + randomX, ground1.size.height / 2 + self.size.height / 14)
    node.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: node.size.width, height: node.size.height))
    node.physicsBody?.dynamic = false
    self.addChild(node)

    let moveAction = SKAction.moveByX(-1, y: 0, duration: 0.005)
    let repeatMoveAction = SKAction.repeatActionForever(moveAction)
    node.runAction(repeatMoveAction)


}

And I have a second problem, imagine a game where you need to avoid object, but you can jump on it, is it possible to detect if the user collide with the side of the node, or if he jump on its top. I think there is another way than create a physic body on the side, and one on the top !

Thanks for your help !

Upvotes: 2

Views: 1475

Answers (1)

Log_n
Log_n

Reputation: 404

For problem 1: Keep a list of possible places that you can spawn an enemy from. When you create an enemy, remove from the list. When the enemy finishes its action and is destroyed/goes offscreen then replace its starting position on the list. Whenever you roll the dice to generate a random enemy you are really just choosing between possible start locations on from your list.

//Create your array and populate it with potential starting points
var posArray = Array<CGPoint>()
posArray.append((CGPoint(x: 1.0, y: 1.0))
posArray.append((CGPoint(x: 1.0, y: 2.0))
posArray.append((CGPoint(x: 1.0, y: 3.0))

//Generate an enemy by rolling the dice and 
//remove its start position from our queue
let randPos = Int(arc4random()) % posArray.count
posArray[randPos]
posArray.removeAtIndex(randPos)

...

//Play game and wait for enemy to die
//Then repopulate the array with that enemy's start position
posArray.append(enemyDude.startPosition)

For problem 2: You have access to the x and y coordinates of node so you can handle the collision appropriately with a single physics body. Just compare the bottom X of your object to top Y of your obstacle and you'll know if you were on top or to the side.

Upvotes: 1

Related Questions