how do i store this function

ok so im having trouble manipulating objects  created randomly, for example the code below creates random boxes, well how do i maipulate one box without touching affecting the others? can this be done? what if i wanted to manipulate all of them? Where is each box stored since i only see one variable, are they all inside the local crate variable?

thanks

local newCrate = function()

rand = math.random( 50 )

local crate


if (rand < 51) then

crate = display.newImage(“crate.png”);

crate.x = 50 + math.random( 160 )

crate.y = -100

physics.addBody( crate, dynamic,{ density=0.001, friction=1, bounce=.05} )

–crate.angularVelocity=math.random(800-400)

–crate.isSleepingAllowed=false

end

crate:addEventListener(“touch”,removeBody)

end


local dropCrates = timer.performWithDelay( 1000, newCrate, 10 )

you should store the crate in a table so you can reference each item later.

how would i write the code for that?

local crates = {}

local newCrate = function()   
    rand = math.random( 50 )
    local crate

    if (rand < 51) then
        crate = display.newImage(“crate.png”);
        crate.x = 50 + math.random( 160 )
        crate.y = -100
        physics.addBody( crate, dynamic,{ density=0.001, friction=1, bounce=.05} )
        --crate.angularVelocity=math.random(800-400)
        --crate.isSleepingAllowed=false
    end
    crate:addEventListener(“touch”,removeBody)

    crates[#crates+1] = crate
end

Then you can access your crates from the crates[] table (crates[1] would be the first crate)

I’m not normally one to point this out, but I’ve noticed you’ve made quite a few posts recently asking about some fairly common Corona/Lua principles. 

I would strongly recommend that you check out some of the video tutorials available either from Corona:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

or from numerous other places online (just typing “corona sdk tutorial” should turn up some youtube links).

While we’re all happy to help, a lot of the things you’ve asked for help with are the real basic elements of Corona /Lua, and without understanding how they work you will find it difficult to make anything more complex. Luckily Lua is pretty simple to pick up in comparison to other languages, so investing some time into some tutorials should really be worthwhile.

As I say, the forums are pretty active with people willing to help, and the staff are always very helpful, but you will be slowed down if you constantly have to:

  • post on forum…
  • wait for a reply…
  • implement the fix…
  • post on forum again because it’s not quite working the way you want…
  • rinse and repeat

Even a day should be enough to learn some useful stuff, and even if it doesn’t all sink in, at least you’ll know where to quickly look something up in a tutorial in future.

so far i have been doing that, and i understand all the concepts, i guess its just that when i start working on personal projects its some much to digest and implement, but ill review the videos again, and the articles that apply, thanks for your help  :slight_smile:

you should store the crate in a table so you can reference each item later.

how would i write the code for that?

local crates = {}

local newCrate = function()   
    rand = math.random( 50 )
    local crate

    if (rand < 51) then
        crate = display.newImage(“crate.png”);
        crate.x = 50 + math.random( 160 )
        crate.y = -100
        physics.addBody( crate, dynamic,{ density=0.001, friction=1, bounce=.05} )
        --crate.angularVelocity=math.random(800-400)
        --crate.isSleepingAllowed=false
    end
    crate:addEventListener(“touch”,removeBody)

    crates[#crates+1] = crate
end

Then you can access your crates from the crates[] table (crates[1] would be the first crate)

I’m not normally one to point this out, but I’ve noticed you’ve made quite a few posts recently asking about some fairly common Corona/Lua principles. 

I would strongly recommend that you check out some of the video tutorials available either from Corona:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

or from numerous other places online (just typing “corona sdk tutorial” should turn up some youtube links).

While we’re all happy to help, a lot of the things you’ve asked for help with are the real basic elements of Corona /Lua, and without understanding how they work you will find it difficult to make anything more complex. Luckily Lua is pretty simple to pick up in comparison to other languages, so investing some time into some tutorials should really be worthwhile.

As I say, the forums are pretty active with people willing to help, and the staff are always very helpful, but you will be slowed down if you constantly have to:

  • post on forum…
  • wait for a reply…
  • implement the fix…
  • post on forum again because it’s not quite working the way you want…
  • rinse and repeat

Even a day should be enough to learn some useful stuff, and even if it doesn’t all sink in, at least you’ll know where to quickly look something up in a tutorial in future.

so far i have been doing that, and i understand all the concepts, i guess its just that when i start working on personal projects its some much to digest and implement, but ill review the videos again, and the articles that apply, thanks for your help  :slight_smile: