Make spawning objects not spawn at the same time

Hi!
I’ve been sitting on this one for 2 months now and I’m slowly losing interest in finishing my game, which is stupid since it’s 90% finished. So, I gathered the remaining motivation I had left and wrote this post here.
I’m having problems making my spawning objects not spawning at the same time. Right now there’s 4 objects spawning and then moving towards the character but when starting the game and later on when they spawn again they usually spawn in clusters and on top of eachother which just looks wrong. I’ve tried with alot of if’s but it didn’t work so I’m turning to you guys, any ideas and help would be greaaaatly appreciated!
Here’s the code for the objects(some comments are wrong, ignore those):

[code]

– CREATE OBSTACLES –

----------------------TRASH
local trash = display.newImage(“trash.png”)
trash.x = display.contentWidth + 30
trash.y = 245
trash:setReferencePoint(display.CenterReferencePoint);
frontfrontGroup:insert(trash)
----------------------MAILBOX
local mailbox = display.newImage(“mailbox.png”)
mailbox.x = display.contentWidth + 30
mailbox.y = 245
mailbox:setReferencePoint(display.CenterReferencePoint);
frontfrontGroup:insert(mailbox)
----------------------HOBO
local hobosheet = sprite.newSpriteSheet( “hobo.png”, 64, 55 )
local hoboset = sprite.newSpriteSet(hobosheet, 1, 5)
sprite.add( hoboset, “hobosleep”, 1, 4, 800, 0 ) – play 3 frames every 80 ms
sprite.add( hoboset, “hobowake”, 5, 1, 800, 1 ) – play 3 frames every 80 ms

local hobo = sprite.newSprite( hoboset )
hobo.x = display.contentWidth + 50
hobo.y = 236
hobo:setReferencePoint(display.CenterReferencePoint);

frontfrontGroup:insert(hobo)

hobo:prepare(“hobosleep”)
hobo:play(“hobosleep”)
----------------------DUMPSTER
local dumpster = display.newImage(“dumpster.png”)
dumpster.x = display.contentWidth + 30
dumpster.y = 255
dumpster:setReferencePoint(display.CenterReferencePoint);
frontfrontGroup:insert(dumpster)
----------------------STREETCAR
local streetcar = display.newImage(“streetcar.png”)
streetcar.x = display.contentWidth + 70
streetcar.y = 265
frontfrontGroup:insert(streetcar)

local obsCount = 1
local lampCount = 1
local isTrashSpawned = false
local isStreetCarSpawned = false
local isDumpsterSpawned = false
local isMailboxSpawned = false
local isHoboSpawned = false

local function spawnTrashDelay()
print(“spawning trash after delay”)
tm:add(trash, { time=12000, 0, x=trash.contentWidth - 30, onComplete = function() trash.x = display.contentWidth + 20 print(“trash DISPOSED!”) isStreetCarSpawned = false end} )
end

local function spawnStreetcar()
if isStreetCarSpawned == false then

local obsCount = math.random(1,500)
if obsCount == 1 then

print(“SPAWNING streetcar”)
isStreetCarSpawned = true
–print("isStreetCarSpawned(BEFORE CAR): ", isStreetCarSpawned)
tm:add(streetcar, { time=15200, 0, x=streetcar.contentWidth - 200, onComplete = function() streetcar.x = display.contentWidth + 70 print(“streetcar DISPOSED!”) print("isStreetCarSpawned(AFTER CAR): ", isStreetCarSpawned) end} )
end
end
end

local function spawnTrash()
if isTrashSpawned == false then

local obsCount = math.random(1,300)

if obsCount == 2 then
print(“SPAWNING trash”)
isTrashSpawned = true
–print("isTrashSpawned(BEFORE TRASH): ", isTrashSpawned)
tm:add(trash, { time=12000, 0, x=trash.contentWidth - 30, onComplete = function() trash.x = display.contentWidth + 30 print(“trash DISPOSED!”) isTrashSpawned = false print("isTrashSpawned(AFTER TRASH): ", isTrashSpawned) end} )
–else
–isTrashSpawned = true
–timer.performWithDelay(2000, spawnTrashDelay(), 1)

end
end
end

local function spawnDumpsterDelay()
print(“spawning dumpster after delay”)
tm:add(dumpster, { time=13000, 0, x=dumpster.contentWidth - 100, onComplete = function() dumpster.x = display.contentWidth + 30 print(“dumpster DISPOSED!”) isDumpsterSpawned = false end} )
end
local function spawnDumpster()

if isDumpsterSpawned == false then

local obsCount = math.random(1,300)
if obsCount == 3 then
isDumpsterSpawned = true
print(“SPAWNING dumpster”)
tm:add(dumpster, { time=13000, 0, x=dumpster.contentWidth - 100, onComplete = function() dumpster.x = display.contentWidth + 30 print(“dumpster DISPOSED!”) isDumpsterSpawned = false end} )

end
end
end

local function spawnMailboxDelay()
print(“spawning mailbox after delay”)
tm:add(mailbox, { time=14000, 0, x=mailbox.contentWidth - 125, onComplete = function() mailbox.x = display.contentWidth + 20 print(“mailbox gone!”) isMailboxSpawned = false end} )
end

local function spawnMailbox()

if isMailboxSpawned == false then

local obsCount = math.random(1,300)
if obsCount == 4 then
print(“SPAWNING mailbox”)
isMailboxSpawned = true
tm:add(mailbox, { time=14000, 0, x=mailbox.contentWidth - 125, onComplete = function() mailbox.x = display.contentWidth + 20 print(“mailbox gone!”) isMailboxSpawned = false end} )

end
end

end

local function spawnHobo()
if isHoboSpawned == false then

local obsCount = math.random(1,5)
if obsCount == 5 then

print(“SPAWNING hobo”)
isHoboSpawned = true
–print("isHoboSpawned(BEFORE CAR): ", isHoboSpawned)
tm:add(hobo, { time=16000, 0, x=hobo.contentWidth - 200, onComplete = function() hobo.x = display.contentWidth + 70 print(“hobo DISPOSED!”) isHoboSpawned = false end} )
end
end
end
[/code] [import]uid: 21652 topic_id: 19702 reply_id: 319702[/import]

well, first I don’t understand the lines beginning with tm:add(…
what are you using there?

and the most interesting part would be where you invoke some of these functions – the game logic/enterFrame method or something like that…

[import]uid: 70635 topic_id: 19702 reply_id: 76269[/import]

assuming there’s more code to this

code above only shows creation of objects and their spawn functions but not the functions that envoke the spawn or as canupa said whats the tm:add(… function [import]uid: 7911 topic_id: 19702 reply_id: 76285[/import]

Ah sorry, forgot about those.
Those are simply calls to a pause transition class. View them as simple transition.to(…etc and you’ll understand.

I based my code around the base of “Jungle Scene”, the sample code in Corona. That template creates a function like this:

local function move(event)  
{  
blabla  
}  

and then sets everything in motion with:

-- Start everything moving  
Runtime:addEventListener( "enterFrame", move );  

So I simply call those earlier functions in that move function like this:

local function move(event)  
{  
 spawnTrash()  
 spawnDumpster()  
 spawnStreetcar()  
 spawnHobo()  
}  

Is this clear? The rest in move is just taken from the Jungle Scene example. Thanks for trying to help! [import]uid: 21652 topic_id: 19702 reply_id: 76286[/import]

hm okay let’s see if I get this right…

you create objects outside the screen and let them move to the left.
if an object reaches a particular coordinate (e.g. x=hobo.contentWidth - 200 ) you put it back outside the screen.

You have some bool flags that prevent objects to spawn.
The spawn methods are called on every frame, so once one of the bool flags (e.g. isHoboSpawned) is false, a new object will spawn.

well… actually in this case I wouldn’t use transitions at all but move the objects in a more simple and classical way.

  
 local trash = display.newImage("trash.png")  
 trash.x = display.contentWidth + 30  
 trash.y = 245  
  
 local speed = 10  
  
local function moveObjects ()  
  
 trash.x = trash.x - speed  
  
 if trash.x \< -10 then  
 trash.x = display.contentWidth + 30  
 end   
  
end  
  
Runtime:addEventListener( "enterFrame", moveObjects );  
  

That’s simple and clean, without any transition-timer-hiccup.
There are two problems now: there’s just one object of each type and you can’t control the spawn delay. so how about adding more than one trash bin at a time - at different times?

I usually create a table that keeps track of all my display objects,
then, in an enterFrame function I iterate through that table and do what ever I want with each object.

 local allMyObjects = {}  
 local speed = 10  
  
local function spawnTrash ()  
 local trash = display.newImage("trash.png") -- create a LOCAL object  
 trash.x = display.contentWidth + 30 -- ... at position  
 trash.y = 245  
 trash.objectType = "trash" -- manually add object type name  
 allMyObjects[#allMyObjects + 1] = trash -- add object to the table  
end  
  
local function moveObjects ()  
 for i,val in pairs(allMyObjects) do -- iterate through the table  
 if val.objectType == "trash" then -- check type  
 val.x = val.x - speed -- do stuff with object  
 if val.x \< -10 then  
 -- if the object is out of bounds, you should destroy it   
 val:removeSelf();  
 -- and also delete the pointer from the table  
 allMyObjects[i] = nil;  
 end   
 elseif val.objectType == "hobo" then -- check another type  
 -- move hobo  
 -- delete hobo ... and so on  
 end  
 end  
end  
  
-- we also need some game logic: when does what object spawn? and so on  
-- you can put it into your moveObjects method, too  
-- but it can become quite complex, so I prefer to put that in it's own method  
  
local spawnTrashCount = 0  
local spawnTrashSpeed = 30 -- spawn every 30 frames  
  
local function gameLogic ()  
  
 spawnTrashCount = spawnTrashCount + 1  
 if spawnTrashCount == spawnTrashSpeed then  
 spawnTrashCount = 0  
 spawnTrash () -- adds a new trash object and puts it into the table   
 end  
  
end  
  
-- now we need to run the enterFrame listeners and everything should run fine  
  
Runtime:addEventListener( "enterFrame", moveObjects );  
Runtime:addEventListener( "enterFrame", gameLogic );  
  

I hope that makes sense to you.
Transitions are great when making a game menu or popup(slideIn)-panels. But I really hate transitions when dealing with important game objects, because you easily lose track of them.

cheers
-finefin
[import]uid: 70635 topic_id: 19702 reply_id: 76367[/import]

Great post!
I think I understand it, the problem is that the logic doesn’t fit the gameplay. There’s a necessity to pause the objects at time of collision.

Uh, it’s hard to explain without posting my entire code here and I feel kinda uneasy doing that.

Is it possible to chat in private somehow? [import]uid: 21652 topic_id: 19702 reply_id: 77189[/import]