Constraining one object's location to another's

Hey guys, just wondering if there is a good way to constrain variables so display objects move in unison.

The only way I'm familiar with is by grouping objects and moving the group.. However I only want one of the objects to be a physics body and it moves via obj:setLinearVelocity(x, y) (and should be the relative point for the other objects).

Thanks!

You could always add a reference to all the objects individually to a table, and then use a for-loop to iterate through all the objects and modify the x/y value of each one. Here's a quick example:

local selectionTable = {}
 
local obj1 = display.newImage( "obj1.png" )
table.insert( selectionTable, obj1 )
 
local obj2 = display.newImage( "obj2.png" )
table.insert( selectionTable, obj2 )
 
local obj3 = display.newImage( "obj3.png" )
table.insert( selectionTable, obj3 )
 
 
-- Move all objects that are referenced to location: 100, 100
for i=1,#selectionTable do
    selectionTable[i].x = 100
    selectionTable[i].y = 100
end

Thanks for the detailed reply :)

I guess I was concerned about looping through that constantly, but I suppose that's what would need to be done regardless, and how expensive can a few coord changes be, hah.

Anyways, my health bars are now tagging along nicely, ty!

views:1546 update:2011/11/26 9:01:35
corona forums © 2003-2011