Spawning Objects a Set Distance

Hi Corona, 

I recently started to learn how to spawn objects on Corona and setting the parameters. I was able to set the parameters for x and y for one spawn.

What I would like to do is set the y coordinate 5 pixels after the object that was spawned before?

For example:

object 1 spawns at x=0 and y=5

object 2 should spawn 5 pixels from the x and y of object 1 

and so on…

Here is my code. (Also I am still learning to understand tables.) I’ve researched a lot about spawns and I get a lot of random spawns, but I want specific spawns. All help is greatly appreciated and any problems you see with the code will also help a lot. 

Keep in mind “steps” is the “object” I want to spawn.

Thanks in advance

--Function to spawn an object local function spawn(params) local steps = display.newImage(params.image) --Set the objects table to a table passed in by parameters steps.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index steps.index = #steps.objTable + 1 --Give the object a custom name steps.myName = "Object : " .. steps.index --If the object should have a body create it, else dont. if params.hasBody then --Allow physics parameters to be passed by parameters: steps.anchorX = params.anchorX or 0 steps.anchorY = params.anchorY or 0 steps.x = params.x or 0 steps.y = params.y or 0 steps.objType = params.objType steps.camera= params.camera steps.xScale = params.xScale or 1 steps.yScale = params.yScale or 1 steps.shape = params.shape or 0 steps.density = params.density or 0 steps.friction = params.friction or 0 steps.bounce = params.bounce or 0 steps.isSensor = params.isSensor or false steps.bodyType = params.bodyType or "dynamic" physics.addBody(steps, steps.bodyType, {density = steps.density, friction = steps.friction, bounce = steps.bounce, shape= steps.shape, isSensor = steps.isSensor}) end --The objects group steps.group = params.group or nil --If the function call has a parameter named group then insert it into the specified group steps.group:insert(steps) --Insert the object into the table at the specified index steps.objTable[steps.index] = steps return steps end local localGroup = display.newGroup() --Create a table to hold our spawns local spawnTable = {} --Create 2 spawns for i = 1,2 do local spawns = spawn( { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x=17, y= 5, xScale = 0.48, yScale = 0.48, group = localGroup, } ) camera:add(spawnTable[i], 1) end

Given x and y of object1 you can calculate x2 and y2 of object2 in distance equal r by using formula

local alpha = 100 -- should be between 0 and 360 local rad = math.rad local sin = math.sin local cos = math.cos x2 = r \* cos(rad( alpha )) + x y2 = r \* sin(rad( alpha )) + y

For example 

if alpha = 0 or alpha = 180 you get y = y2 and |x-x2|=r,

if alpha = 90 or alpha = 270 you get x = x2 and |y-y2|=r.

Hi Idurniat,

 

I am not really sure what you meant to say. I was wondering if maybe you can explain the code a bit more? I am not looking for distance. Maybe I did not make myself clear.

 

I am trying to make a code that spawns the object say 5 pixels from the previously spawned object. I think maybe your code works, but I cannot understand it.

 

Thanks in advance. Also thanks for responding in the topic!

 

Basically the code spawns “steps” Once it does that, it puts it into a table. I want to manipulate the Y variable so that it spawns 5 pixels down from “spawn1” That would be spawn2. Then spawn3 should get created 5 pixels from spawn2. And so on. 

Maybe you can incorporate your code somehow. The parameter “y=5” on the code is where the change needs to happen.

 

Again thank you!

local spawnTable = {} --Create 2 spawns for i = 1,3 do local spawns = spawn( { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x=17, y= 5, xScale = 0.48, yScale = 0.48, group = localGroup, } ) camera:add(spawnTable[i], 1) end camera:setParallax(1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3)

I have prepare image to explain you what I wrote in my  previous post. Sorry for my english. It is not my native language.

0vXeUSu.png?1

Thank you for being so elaborate, but I am not really sure how I can use it. I am a beginner in Corona and Lua.

Here is an image to show what I need.

Again, Thank you Idurniat I really appreciate you trying to help.

Try

local height = 50 -- height of spawn image local distanceY = 5 local startX, startY = 17, 5 -- x and y for first spawn object for i = 1,3 do local spawns = spawn { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x = startX, y = startY + ( i - 1 ) \* ( height + distanceY ), xScale = 0.48, yScale = 0.48, group = localGroup, }     -- I think here you should add spawn to spawnTable  camera:add(spawnTable[i], 1) end

Idurniat,

I cannot thank you enough. It worked like a charm!!!

It worked perfectly. 

I really cannot thank you enough. Thanks a lot for your time and help.

Do you think you can explain how I can add and access the data stored in tables briefly? I understand it a little bit.

Again, cannot tell you how much I appreciate what you’ve done for me.

Thanks.

local spawnTable = {} local height = 34 -- height of spawn image local distanceY = 0 local startX, startY = 17, -27 -- x and y for first spawn object --Create 2 spawns for i = 1,3 do local spawns = spawn( { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x = startX, y = startY + ( i - 1 ) \* ( height + distanceY ), xScale = 0.48, yScale = 0.48, group = localGroup, } ) camera:add(spawnTable[i], 1) end

I do nothing special. It was not so hard:)

I  would love to do that but I think my english is not good enough. So I recommended you read Understanding Lua tables in Corona SDK

I see you made progress with your game. Good for you. Keep working and good luck:)

ldurniat 

Given x and y of object1 you can calculate x2 and y2 of object2 in distance equal r by using formula

local alpha = 100 -- should be between 0 and 360 local rad = math.rad local sin = math.sin local cos = math.cos x2 = r \* cos(rad( alpha )) + x y2 = r \* sin(rad( alpha )) + y

For example 

if alpha = 0 or alpha = 180 you get y = y2 and |x-x2|=r,

if alpha = 90 or alpha = 270 you get x = x2 and |y-y2|=r.

Hi Idurniat,

 

I am not really sure what you meant to say. I was wondering if maybe you can explain the code a bit more? I am not looking for distance. Maybe I did not make myself clear.

 

I am trying to make a code that spawns the object say 5 pixels from the previously spawned object. I think maybe your code works, but I cannot understand it.

 

Thanks in advance. Also thanks for responding in the topic!

 

Basically the code spawns “steps” Once it does that, it puts it into a table. I want to manipulate the Y variable so that it spawns 5 pixels down from “spawn1” That would be spawn2. Then spawn3 should get created 5 pixels from spawn2. And so on. 

Maybe you can incorporate your code somehow. The parameter “y=5” on the code is where the change needs to happen.

 

Again thank you!

local spawnTable = {} --Create 2 spawns for i = 1,3 do local spawns = spawn( { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x=17, y= 5, xScale = 0.48, yScale = 0.48, group = localGroup, } ) camera:add(spawnTable[i], 1) end camera:setParallax(1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3)

I have prepare image to explain you what I wrote in my  previous post. Sorry for my english. It is not my native language.

0vXeUSu.png?1

Thank you for being so elaborate, but I am not really sure how I can use it. I am a beginner in Corona and Lua.

Here is an image to show what I need.

Again, Thank you Idurniat I really appreciate you trying to help.

Try

local height = 50 -- height of spawn image local distanceY = 5 local startX, startY = 17, 5 -- x and y for first spawn object for i = 1,3 do local spawns = spawn { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x = startX, y = startY + ( i - 1 ) \* ( height + distanceY ), xScale = 0.48, yScale = 0.48, group = localGroup, }     -- I think here you should add spawn to spawnTable  camera:add(spawnTable[i], 1) end

Idurniat,

I cannot thank you enough. It worked like a charm!!!

It worked perfectly. 

I really cannot thank you enough. Thanks a lot for your time and help.

Do you think you can explain how I can add and access the data stored in tables briefly? I understand it a little bit.

Again, cannot tell you how much I appreciate what you’ve done for me.

Thanks.

local spawnTable = {} local height = 34 -- height of spawn image local distanceY = 0 local startX, startY = 17, -27 -- x and y for first spawn object --Create 2 spawns for i = 1,3 do local spawns = spawn( { image = "Steps.png", objTable = spawnTable, hasBody = true, friction = 0.2, shape={-16.5,-16.5, 16.5,-16.5, 16.5,16.5, -16.5,16.5}, bodyType = "static", objType = "ground", anchorX = 0.5, anchorY = 0.5, x = startX, y = startY + ( i - 1 ) \* ( height + distanceY ), xScale = 0.48, yScale = 0.48, group = localGroup, } ) camera:add(spawnTable[i], 1) end

I do nothing special. It was not so hard:)

I  would love to do that but I think my english is not good enough. So I recommended you read Understanding Lua tables in Corona SDK

I see you made progress with your game. Good for you. Keep working and good luck:)

ldurniat