Simple Group:insert() question

I’m sorry I have to ask this here but the API page had such little explanation anyways, heres the code I’m using which works when I don’t put the object in a group, but when I do, the object just doesn’t appear and I get no debug error messages.

local physics = require("physics")   
physics.start()  
  
local tanks = display.newGroup()  
function spawntank()  
 tank = display.newImage("tank.png")  
 tank.x = 437; tank.y = 202  
 physics.addBody( tank, { density=0.9, friction=0.3, bounce=0.3} )  
 tank:setLinearVelocity( -333, 0 )  
 tanks:insert(tank, true)  
end  
  
spawntank()  

Any idea how I’m using :insert wrong? [import]uid: 44393 topic_id: 8292 reply_id: 308292[/import]

Physics will only work on objects within the same group.

So when you add your tank into tanks it is in a different group to your other physics objects and thus not available for any dynamics. As a sad consequence of the tank is not rendered :frowning: [import]uid: 22878 topic_id: 8292 reply_id: 29566[/import]

Ah I see, I tried this instead but it still didn’t render.

local physics = require("physics")   
physics.start()  
   
local tanks = display.newGroup()  
function spawntank()  
 tank = display.newImage("tank.png")  
 tanks:insert(tank)  
 tanks[1].x = 437; tanks[1].y = 202  
 physics.addBody( tanks[1], { density=0.9, friction=0.3, bounce=0.3} )  
 tanks[1]:setLinearVelocity( -333, 0 )  
end  
   
spawntank()  

Wait is the problem that display.newGroup can’t contain objects with physics?.. [import]uid: 44393 topic_id: 8292 reply_id: 29610[/import]

I just copied your code an it works for me. I used a square.png for the object and it starts mid right and goes to lower left then off the screen. [import]uid: 11809 topic_id: 8292 reply_id: 29634[/import]

I figured out the problem was that my background image was actually covering up the sprite.

Which is weird because I didn’t make the background image an object, just an image, and in Coronas “HelloPhysics” it does that too yet the crate still displays over the background…

Any help with this? I tried searching the forum but I don’t know how to properly phrase this question. [import]uid: 44393 topic_id: 8292 reply_id: 29641[/import]

When do call the background image? [import]uid: 11809 topic_id: 8292 reply_id: 29659[/import]

Call and create the background image first and then build everything else on top of it. If you do create the background after the other objects and they are all in the same group you can use object:toBack() to push the background to the back. [import]uid: 22878 topic_id: 8292 reply_id: 29673[/import]