Coding logic fail on my end

I wasn’t sure where to post this, but this is definitely a newb question! Ok, I have a marbleFactory.lua class that i want to use to spawn marbles.lua marbles.  Only problem is, they all spawn at the same time.  I couldn’t think of the logic to make them spawn at separate and/or random times. I definitely would love to implement spawning patterns, but first I need to overcome this hurdle.

The default marble that is created, always has a Y position of -200, and the x position is random within the contents of display.contentWidth.  So I spawned 20 marbles with that terrible for loop you see below, and they all spawn at the same time, same Y position. I wouldn’t mind if they were the same Y position, but I need help devising logic to spawn them at different times. 

The only thing I could devise was to handle the spawning in my mainGame because of the game loop and get rid of this marbleFactory.lua altogether. 
 

local marbleHandler = require "modules.marbles" local M = {} function M.new(options) local options = options or {} local difficulty = options.difficulty or 1 local spawnRate = options.spawnRate or 1000 local spawnAmount = options.spawnAmount or 20 + (difficulty\*2) local marbles = {} local group = display.newGroup() for i=1, spawnAmount do timer.performWithDelay( spawnRate, function() marbles[i] = marbleHandler.new(); group:insert(marbles[i]); end, 1) end return group end return M

I’m a bit confused by the code because you are only creating a display group but here it is:

  • You don’t need that for loop. Instead, you can call the timer spawnAmount times.

  • You should also take that call outside of the constructor because it seems to be causing a recursive behaviour in its current condition. You should call it from within your game logic.

Can you try this out?

What you said, worked so perfectly!!!  Thank you tons!   It was like magic!

Glad it worked out :slight_smile: Good luck.

I’m a bit confused by the code because you are only creating a display group but here it is:

  • You don’t need that for loop. Instead, you can call the timer spawnAmount times.

  • You should also take that call outside of the constructor because it seems to be causing a recursive behaviour in its current condition. You should call it from within your game logic.

Can you try this out?

What you said, worked so perfectly!!!  Thank you tons!   It was like magic!

Glad it worked out :slight_smile: Good luck.