How to spawn objects

Hi! I am trying to spawn some objects in my scene,but I am having some problems.

So when onSpawnOne function is triggered it should spawn 1 to 4 objects,but it only spawns just 2.One object that stands still and another objects that is moving.

I have the same problem with onSpawnTwo function.

Why isn’t it working? And how to spawn objects from n to n+1?

local score = 0 function spawn (event) obj = display.newImageRect("Icon.png",70,70) obj.x = 100 obj.y = 300 return obj end local spawnTable = {} function onSpawnOne (event) for i = 1,4 do spawnTable[i] = spawn() end end function onSpawnTwo (event) for i = 4,8 do spawnTable[i] = spawn() end end if score == 0 then onSpawnOne() elseif score == 100 then onSpawnTwo() end function movement (event) if obj.x \<-20 then obj.x = 300 else obj.x = obj.x - 2 end end Runtime:addEventListener ("enterFrame",movement)

Hi @bogdanmocanu2,

Most likely, this is because you’re using “obj” as a global variable, at least from what I can see in the code you’ve provided (if there’s more, then please clarify and post it). Lua is going to get confused with what you’re trying to reference if you approach it this way.

Take care,

Brent

Hi Bren,

 Thank you for your repply. I somehow fix the problem.But I think that my objects are overlapping.How can I avoid that?

Can I spawn one object at a time,then after a few seconds another object?Is that possible?

Thank you!

Hello,

Yes, it’s easily possible. :slight_smile: You should probably re-design this however, so that you’re using a repeating timer that, for each time it executes, it spawns a new object.

Brent

Hi,

  This is just an experiment.When I get the final code,I’ll assemble it into the final product.Can you please provide me an example on how to do that?

Thank you!

Hi @bogdanmocanu2,

Are you speaking about how to build your Corona project into a running app for testing or distribution? If so, please follow these guides carefully:

http://docs.coronalabs.com/guide/distribution/buildSettings/index.html

iOS: http://docs.coronalabs.com/guide/distribution/iOSBuild/index.html

Android: http://docs.coronalabs.com/guide/distribution/androidBuild/index.html

Take care,

Brent

No I’m talking about how to spawn one object at a time.

I have expressed incorectly,I’m sorry!

bogdan,

This example is a quick fix, and I tried to keep as much of your code as it was, but several parts had to be changed to get this to function.  I only am trying to show what you requested, that is, how to spawn the objects one a time.

I set up a timer in the movement function you had, so every 3 seconds an obj will spawn.  But, also, a simple rectangle at the top of the screen if you touch it it will spawn an object also.

Cut and paste this code into a new project into your corona and try it out.  It is very crude and simple, and not the way I would do my project, but it shows what you are looking for.  Play around with changing parts of the code and that is a good way to learn how it all works.

Note: I use local in front of all the functions, you had yours as global, and that is not normally recommended.

Note: I added a flag ‘isdone’ to each object so you can deactivate the object once it should no longer be moving.

Note: I made ‘obj’ local in the ‘spawn’ function

Good luck.

&nbsp; local score = 0 local spawnCnt = 0 local spawnTable = {} local startTime = os.time() &nbsp; local function spawn () local obj = display.newImageRect("Icon.png",70,70) obj.x = 100 obj.y = 300 obj.isDone = false return obj end &nbsp; local function onSpawn () spawnCnt = spawnCnt + 1 spawnTable[spawnCnt] = spawn() end &nbsp; local function onTouch(e) if e.phase == "ended" then onSpawn() end end &nbsp; local rect = display.newRect(100,100,50,50) rect:addEventListener("touch", onTouch) &nbsp; local function movement (event) -- SPAWN OBJECT USING TIMER if os.time() - startTime \>= 3 then onSpawn() startTime = os.time() end &nbsp; -- MOVE OBJECT if spawnCnt \> 0 then for i = 1, spawnCnt do if spawnTable[i].isDone == false then if spawnTable[i].x \< -20 then spawnTable[i].x = 300 spawnTable[i].isDone = true else&nbsp; spawnTable[i].x = spawnTable[i].x - 2 end end end end end Runtime:addEventListener ("enterFrame",movement) &nbsp; &nbsp; &nbsp;

Thanks mate! That’s the thing I was looking for. 

Hi @bogdanmocanu2,

Most likely, this is because you’re using “obj” as a global variable, at least from what I can see in the code you’ve provided (if there’s more, then please clarify and post it). Lua is going to get confused with what you’re trying to reference if you approach it this way.

Take care,

Brent

Hi Bren,

 Thank you for your repply. I somehow fix the problem.But I think that my objects are overlapping.How can I avoid that?

Can I spawn one object at a time,then after a few seconds another object?Is that possible?

Thank you!

Hello,

Yes, it’s easily possible. :slight_smile: You should probably re-design this however, so that you’re using a repeating timer that, for each time it executes, it spawns a new object.

Brent

Hi,

  This is just an experiment.When I get the final code,I’ll assemble it into the final product.Can you please provide me an example on how to do that?

Thank you!

Hi @bogdanmocanu2,

Are you speaking about how to build your Corona project into a running app for testing or distribution? If so, please follow these guides carefully:

http://docs.coronalabs.com/guide/distribution/buildSettings/index.html

iOS: http://docs.coronalabs.com/guide/distribution/iOSBuild/index.html

Android: http://docs.coronalabs.com/guide/distribution/androidBuild/index.html

Take care,

Brent

No I’m talking about how to spawn one object at a time.

I have expressed incorectly,I’m sorry!

bogdan,

This example is a quick fix, and I tried to keep as much of your code as it was, but several parts had to be changed to get this to function.  I only am trying to show what you requested, that is, how to spawn the objects one a time.

I set up a timer in the movement function you had, so every 3 seconds an obj will spawn.  But, also, a simple rectangle at the top of the screen if you touch it it will spawn an object also.

Cut and paste this code into a new project into your corona and try it out.  It is very crude and simple, and not the way I would do my project, but it shows what you are looking for.  Play around with changing parts of the code and that is a good way to learn how it all works.

Note: I use local in front of all the functions, you had yours as global, and that is not normally recommended.

Note: I added a flag ‘isdone’ to each object so you can deactivate the object once it should no longer be moving.

Note: I made ‘obj’ local in the ‘spawn’ function

Good luck.

&nbsp; local score = 0 local spawnCnt = 0 local spawnTable = {} local startTime = os.time() &nbsp; local function spawn () local obj = display.newImageRect("Icon.png",70,70) obj.x = 100 obj.y = 300 obj.isDone = false return obj end &nbsp; local function onSpawn () spawnCnt = spawnCnt + 1 spawnTable[spawnCnt] = spawn() end &nbsp; local function onTouch(e) if e.phase == "ended" then onSpawn() end end &nbsp; local rect = display.newRect(100,100,50,50) rect:addEventListener("touch", onTouch) &nbsp; local function movement (event) -- SPAWN OBJECT USING TIMER if os.time() - startTime \>= 3 then onSpawn() startTime = os.time() end &nbsp; -- MOVE OBJECT if spawnCnt \> 0 then for i = 1, spawnCnt do if spawnTable[i].isDone == false then if spawnTable[i].x \< -20 then spawnTable[i].x = 300 spawnTable[i].isDone = true else&nbsp; spawnTable[i].x = spawnTable[i].x - 2 end end end end end Runtime:addEventListener ("enterFrame",movement) &nbsp; &nbsp; &nbsp;

Thanks mate! That’s the thing I was looking for.