Group elements

Hi guys,

I would very much appreciate your help.

Why below created rectangles do not “jump”?

They are created onscreen but they do not jump (print(“jumped”) is never executed in terminal window).

I get no errors and physics is started of course.

elements = display.newGroup()

elements.anchorChildren = true

elements.anchorX = 0

elements.anchorY = 1

elements.x = 0

elements.y = 0

–stage:insert(elements)

function createRectangles()

rectangle = display.newRect(  0, 0, 50, 50 )

rectangle:setFillColor(67, 255, 8)

rectangle.x = math.random(100, 200)

rectangle.y = math.random(100, 200)

rectangle.canJump = 1

physics.addBody(rectangle, “dynamic”, {density=1, bounce=0.1, friction=.2})

elements:insert(rectangle)

end

function touchAction()

for a = 1, elements.numChildren,-1  do

–if ( rectangle[a].canJump > 0) then

rectangle[a]:setLinearVelocity(0, -300)

print(“jumped”)

end

–end

end

creating = timer.performWithDelay ( 2000, createRectangles , -1 )

jumping = timer.performWithDelay ( 6000, touchAction , -1 )

Many thanks for your help.

Ivan

Not at home, but I think it should work (i guess you have more code, like start physics and such)

elements = display.newGroup() elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 --stage:insert(elements) local index = 1 local rectangle = {} function createRectangles() rectangle[index] = display.newRect( 0, 0, 50, 50 ) rectangle[index]:setFillColor(67, 255, 8) rectangle[index].x = math.random(100, 200) rectangle[index].y = math.random(100, 200) rectangle[index].canJump = 1 physics.addBody(rectangle[index], "dynamic", {density=1, bounce=0.1, friction=.2}) elements:insert(rectangle[index]) index = index + 1 end function touchAction() for a = 1, elements.numChildren,-1 do --if ( rectangle[a].canJump \> 0) then rectangle[a]:setLinearVelocity(0, -300) print("jumped") end --end end creating = timer.performWithDelay ( 2000, createRectangles , -1 ) jumping = timer.performWithDelay ( 6000, touchAction , -1 )

Hey guys,  this part of the loop is popping out at me as wrong:

for a = 1, elements.numChildren,-1 do -- should be: for a = 1, elements.numChildren do

The first line is starting at 1 and counting backwards…

I would also suggest this change:

local rectangle = {} local elements = display.newGroup() elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 function createRectangles( group, aTable ) local tmp = display.newRect( group, 0, 0, 50, 50 ) aTable[#aTable+1] = tmp tmp:setFillColor(67, 255, 8) tmp.x = math.random(100, 200) tmp.y = math.random(100, 200) tmp.canJump = 1 physics.addBody( tmp, "dynamic", {density=1, bounce=0.1, friction=.2} ) return tmp end

Then later call as:

createRectangles( elements, rectangle )

@roaminggammer , nice idea  :slight_smile:

Hi undecode,

Hi roaminggammer,

Thank you guys for your help.

I have implemented roamings suggestion and strange things happend  :smiley:

  1. One rectangle is being created as soon as I start simulator: note that I have called it with:

    creating = timer.performWithDelay(1000, createRectangles( elements, rectangle ), -1)

  • even when you replace 1000 with 5000 rectangle is immediately created onscreen (no delay).

Why is that?

  1. Rectangle is floating above ground. Is that due to anchorX and anchorY (please see screenshot on dropbox below)?

  2. How to create more rectangles (let say 3 at a time)?

  3. Why

    timer.performWithDelay(1000, createRectangles( elements, rectangle ), -1)

executes only once (only one rectangle is created - no repeating)?

Complete code and screenshot are here:

https://www.dropbox.com/s/6xhb1o71pp51wt1/rect.zip?dl=0

Many thanks.

Best regards from Croatia.

Ivan

The problem is timer.performWithDelay does not allow you to specify parameters in a listener function like you have it defined. One way to solve the problem is to embed a listener function within timer.performWithDelay.

[lua]creating = timer.performWithDelay(1000, function() createRectangles( elements, rectangle ) end, -1)[/lua]

Thank you Tom.

 

My last question is why first rectangle that is created is colliding with “imaginary” boundary above ground and then falls on the ground, while second and the rest of rectangles created are colliding directly with the ground?

 

You can find the main.lua here (Landscape orientation):

https://www.dropbox.com/s/78dbpr8gs1gtr4d/main.txt?dl=0

 

 

Many thanks.

 

Ivan

Sorry guys, I messed up, i.e. I found the error  :o

Many, many thanks!

Ivan

Not at home, but I think it should work (i guess you have more code, like start physics and such)

elements = display.newGroup() elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 --stage:insert(elements) local index = 1 local rectangle = {} function createRectangles() rectangle[index] = display.newRect( 0, 0, 50, 50 ) rectangle[index]:setFillColor(67, 255, 8) rectangle[index].x = math.random(100, 200) rectangle[index].y = math.random(100, 200) rectangle[index].canJump = 1 physics.addBody(rectangle[index], "dynamic", {density=1, bounce=0.1, friction=.2}) elements:insert(rectangle[index]) index = index + 1 end function touchAction() for a = 1, elements.numChildren,-1 do --if ( rectangle[a].canJump \> 0) then rectangle[a]:setLinearVelocity(0, -300) print("jumped") end --end end creating = timer.performWithDelay ( 2000, createRectangles , -1 ) jumping = timer.performWithDelay ( 6000, touchAction , -1 )

Hey guys,  this part of the loop is popping out at me as wrong:

for a = 1, elements.numChildren,-1 do -- should be: for a = 1, elements.numChildren do

The first line is starting at 1 and counting backwards…

I would also suggest this change:

local rectangle = {} local elements = display.newGroup() elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 function createRectangles( group, aTable ) local tmp = display.newRect( group, 0, 0, 50, 50 ) aTable[#aTable+1] = tmp tmp:setFillColor(67, 255, 8) tmp.x = math.random(100, 200) tmp.y = math.random(100, 200) tmp.canJump = 1 physics.addBody( tmp, "dynamic", {density=1, bounce=0.1, friction=.2} ) return tmp end

Then later call as:

createRectangles( elements, rectangle )

@roaminggammer , nice idea  :slight_smile:

Hi undecode,

Hi roaminggammer,

Thank you guys for your help.

I have implemented roamings suggestion and strange things happend  :smiley:

  1. One rectangle is being created as soon as I start simulator: note that I have called it with:

    creating = timer.performWithDelay(1000, createRectangles( elements, rectangle ), -1)

  • even when you replace 1000 with 5000 rectangle is immediately created onscreen (no delay).

Why is that?

  1. Rectangle is floating above ground. Is that due to anchorX and anchorY (please see screenshot on dropbox below)?

  2. How to create more rectangles (let say 3 at a time)?

  3. Why

    timer.performWithDelay(1000, createRectangles( elements, rectangle ), -1)

executes only once (only one rectangle is created - no repeating)?

Complete code and screenshot are here:

https://www.dropbox.com/s/6xhb1o71pp51wt1/rect.zip?dl=0

Many thanks.

Best regards from Croatia.

Ivan

The problem is timer.performWithDelay does not allow you to specify parameters in a listener function like you have it defined. One way to solve the problem is to embed a listener function within timer.performWithDelay.

[lua]creating = timer.performWithDelay(1000, function() createRectangles( elements, rectangle ) end, -1)[/lua]

Thank you Tom.

 

My last question is why first rectangle that is created is colliding with “imaginary” boundary above ground and then falls on the ground, while second and the rest of rectangles created are colliding directly with the ground?

 

You can find the main.lua here (Landscape orientation):

https://www.dropbox.com/s/78dbpr8gs1gtr4d/main.txt?dl=0

 

 

Many thanks.

 

Ivan

Sorry guys, I messed up, i.e. I found the error  :o

Many, many thanks!

Ivan