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 !
Aucun commentaire:
Enregistrer un commentaire