Problem about my table's object

Hi,  :slight_smile:

Here is my problem.

I have created a table call boxes and a group call elements

[lua]local boxes = {}

local elements = display.newGroup ()[/lua]

and I also have a function to add boxes

[lua]local function addboxes ()

     local height = math.random ( display.contentCenterY - 160, display.contentCenterY + 160)

     box = display.newImageRect ( “box.png”, 110, 350)
     box.x = w + 100
     box.y = height

     elements:insert (box)

     boxes[#boxes+1] = box

     

end

timer.performWithDelay ( 5000, addboxes, -1)

[/lua]

After that, I want to move all the boxes from the right side to left side of the simulator.

[lua]function moveboxes ()

     for a = elements.numChildren, 1, -1 do
          if (elements[a].x < display.contentCenterX - 120) then
     end

     if ( elements[a].x > -100 ) then

          elements[a].x = elements[a].x - 12

     else

          elements:remove (elements[a])
          elements[a] = nil

end
end
end

timer.performWithDelay ( 50, moveboxes, -1)

[/lua]

Also, when the box move to x point, I want it to print (“hi”)

However, after the function printed hi for the first column, and the second column went error…

here is my code

[lua]local function boxMoveDown ()

     local firstBox = boxes[1]     --Here might be the problem, I’m not so sure

     

     if (firstBox.x < 100) then

          print ( “hi” )
     end

end
[/lua]

Try changing:

    box = display.newImageRect ( “box.png”, 110, 350)

to

   local box = display.newImageRect ( “box.png”, 110, 350)

and see if that changes anything.

It still doesen’t work. the error saids

main.lua:18 attempt to compare nil with number

[lua]if (firstBox.x < 100) then – this is the main.lua:18
[/lua]

is the boxes [1] go nil after the moveboxes function

It’s hard to see how all of this works together as we never see you call the last function.  Can you post the code in one block, so we can see everything in order and include the block of code that calls boxMoveDown().

Rob

[lua]local physics = require (“physics”)
physics.start()

physics.setGravity(0, 0)

local w = display.contentWidth
local h = display.contentHeight
local cx = display.contentCenterX
local cy = display.contentCenterY

local boxes = {}

local function boxMoveDown ()

     local firstBox = boxes[1]

     if (firstBox.x < 100) then
     print ( “hi” )

     end

end

local elements = display.newGroup ()

elements.x = 0
elements.y = 0

function moveboxes ()

     for a = elements.numChildren, 1, -1 do
          if (elements[a].x < display.contentCenterX - 120) then

     end

     if ( elements[a].x > -100 ) then
     elements[a].x = elements[a].x - 12
     else

     elements:remove (elements[a])

     elements[a] = nil

end
end
end

local function addboxes ()

     local height = math.random ( display.contentCenterY - 160, display.contentCenterY + 160)

     

     local box = display.newImageRect ( “box.png”, 110, 350)
     box.x = w + 100
     box.y = height

     elements:insert (box)

     boxes[#boxes+1] = box

     boxMoveDown ()

end

timer.performWithDelay ( 5000, addboxes, -1)
timer.performWithDelay ( 50, moveboxes, -1 )

[/lua]

The problem is that your “moveboxes” function is firing off every 50 milliseconds and your addboxes is firing every 5 seconds.  The moveboxes is moving the box faster than 5 second and the first box is getting removed in your moveboxes function before the second time addboxes (and moveBoxDown()) gets called a second time.

Rob

What should I do to solve this problem? Since I have to call a function when the box reach x point. I don’t really want to slow down the move boxes function. Can I call the moveBoxDown function in other place in stead of calling it in the add boxes function?

It’s really hard to answer your question because I don’t know what your intent or end goal is.  It would seem to me that if you want to know when the box gets to an X of 100 that you would do that in your moveboxes function not your addboxes function since its the moveboxes that’s actually moving them.    I don’t know why you care about the 1st box.  Without knowing what you’re really trying to do, it’s going to be tough to provide any real advise.

Rob

THANKS ROB!!

the problem is solved. 

Thank you

Try changing:

    box = display.newImageRect ( “box.png”, 110, 350)

to

   local box = display.newImageRect ( “box.png”, 110, 350)

and see if that changes anything.

It still doesen’t work. the error saids

main.lua:18 attempt to compare nil with number

[lua]if (firstBox.x < 100) then – this is the main.lua:18
[/lua]

is the boxes [1] go nil after the moveboxes function

It’s hard to see how all of this works together as we never see you call the last function.  Can you post the code in one block, so we can see everything in order and include the block of code that calls boxMoveDown().

Rob

[lua]local physics = require (“physics”)
physics.start()

physics.setGravity(0, 0)

local w = display.contentWidth
local h = display.contentHeight
local cx = display.contentCenterX
local cy = display.contentCenterY

local boxes = {}

local function boxMoveDown ()

     local firstBox = boxes[1]

     if (firstBox.x < 100) then
     print ( “hi” )

     end

end

local elements = display.newGroup ()

elements.x = 0
elements.y = 0

function moveboxes ()

     for a = elements.numChildren, 1, -1 do
          if (elements[a].x < display.contentCenterX - 120) then

     end

     if ( elements[a].x > -100 ) then
     elements[a].x = elements[a].x - 12
     else

     elements:remove (elements[a])

     elements[a] = nil

end
end
end

local function addboxes ()

     local height = math.random ( display.contentCenterY - 160, display.contentCenterY + 160)

     

     local box = display.newImageRect ( “box.png”, 110, 350)
     box.x = w + 100
     box.y = height

     elements:insert (box)

     boxes[#boxes+1] = box

     boxMoveDown ()

end

timer.performWithDelay ( 5000, addboxes, -1)
timer.performWithDelay ( 50, moveboxes, -1 )

[/lua]

The problem is that your “moveboxes” function is firing off every 50 milliseconds and your addboxes is firing every 5 seconds.  The moveboxes is moving the box faster than 5 second and the first box is getting removed in your moveboxes function before the second time addboxes (and moveBoxDown()) gets called a second time.

Rob

What should I do to solve this problem? Since I have to call a function when the box reach x point. I don’t really want to slow down the move boxes function. Can I call the moveBoxDown function in other place in stead of calling it in the add boxes function?

It’s really hard to answer your question because I don’t know what your intent or end goal is.  It would seem to me that if you want to know when the box gets to an X of 100 that you would do that in your moveboxes function not your addboxes function since its the moveboxes that’s actually moving them.    I don’t know why you care about the 1st box.  Without knowing what you’re really trying to do, it’s going to be tough to provide any real advise.

Rob

THANKS ROB!!

the problem is solved. 

Thank you