3

I'm making a game where I have a node that is spawning and falling from the top of the screen. However I want to make the nodes spawn at random time intervals between a period of 3 seconds. So one spawns in 1 second, the next in 2.4 seconds, the next in 1.7 seconds, and so forth forever. I am struggling with what the code should be for this.

Code I currently have for spawning node:

    let wait = SKAction.waitForDuration(3, withRange: 2)
    let spawn = SKAction.runBlock { addTears()
    }

    let sequence = SKAction.sequence([wait, spawn])
    self.runAction(SKAction.repeatActionForever(spawn))

The code for my addTears() function is:

func addTears() {
        let Tears = SKSpriteNode (imageNamed: "Tear")
        Tears.position = CGPointMake(Drake1.position.x, Drake1.position.y - 2)
        Tears.zPosition = 3
        addChild(Tears)

    //gravity
    Tears.physicsBody = SKPhysicsBody (circleOfRadius: 150)
    Tears.physicsBody?.affectedByGravity = true

    //contact
    Tears.physicsBody = SKPhysicsBody (circleOfRadius: Tears.size.width/150)
    Tears.physicsBody!.categoryBitMask = contactType.Tear.rawValue
    Tears.physicsBody!.contactTestBitMask = contactType.Bucket.rawValue
    }
Pimgd
  • 5,983
  • 1
  • 30
  • 45
NickyNick321
  • 213
  • 1
  • 4
  • 12
  • Use `arc4random_uniform()` function to generate random numbers. And then assign them in the timeInterval. – Shamas S Jun 10 '15 at 18:01
  • Would you be able to give me an example of how to assign them in the timeInterval? – NickyNick321 Jun 10 '15 at 18:12
  • possible duplicate of [Spawn nodes at random times combing waitForDuration:withRange and runBlock: in an SKAction sequence](http://stackoverflow.com/questions/30851619/spawn-nodes-at-random-times-combing-waitfordurationwithrange-and-runblock-in-a) – lchamp Jun 15 '15 at 19:04

1 Answers1

4

It's not advisable that you use NSTimer with SpriteKit (see SpriteKit - Creating a timer). Instead, to generate random times you could use SKAction.waitForDuration:withRange:

Creates an action that idles for a randomized period of time.

When the action executes, the action waits for the specified amount of time, then ends...

Each time the action is executed, the action computes a new random value for the duration. The duration may vary in either direction by up to half of the value of the durationRange parameter...

To spawn nodes at random times you could combine waitForDuration:withRange with runBlock: together in an SKAction sequence. For example:

// I'll let you adjust the numbers correctly...
let wait = SKAction.wait(forDuration: 3, withRange: 2)
let spawn = SKAction.run {
    // Create a new node and it add to the scene...
}

let sequence = SKAction.sequence([wait, spawn])
self.run(SKAction.repeatForever(sequence))
// self in this case would probably be your SKScene subclass.
dschu
  • 4,992
  • 5
  • 31
  • 48
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • I replaced my code with this code and for some reason my game wouldn't start. My node is already created so I added it to the area that says // Create new node and it add to the scene... . Can't figure out what I'm doing wrong. – NickyNick321 Jun 10 '15 at 18:46
  • When you say "wouldn't start"? Are you getting any errors? – ABakerSmith Jun 10 '15 at 18:55
  • The scene that runs this code is my second scene, the first is a "tap to start". When i run the game and try to tap to start, it doesn't load my second scene. – NickyNick321 Jun 10 '15 at 19:00
  • Is it freezing? Does anything on he second scene get run? Where have you put the code I supplied in my answer? – ABakerSmith Jun 10 '15 at 19:04
  • I put the code in my didMoveToView. I think it is just freezing because the second scene does not run. – NickyNick321 Jun 10 '15 at 19:48
  • @NickyNick321 try putting a log in the run block. Guessing you have your delay wrong and it is stuck in an infinite loop. Also I would recommend doing runAction:withKey so you can stop this action when you need to do something like game over. – Skyler Lauren Jun 11 '15 at 00:50
  • You were right I added the log and there is an infinite loop. Would you know how I should go about fixing this? – NickyNick321 Jun 11 '15 at 03:31
  • If you update your question with your code, including my answer above, I can take a look for you. – ABakerSmith Jun 11 '15 at 13:34