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! [import]uid: 87138 topic_id: 18095 reply_id: 318095[/import]

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, 100for i=1,#selectionTable do selectionTable[i].x = 100 selectionTable[i].y = 100end[/code] [import]uid: 52430 topic_id: 18095 reply_id: 69275[/import]

Thanks for the detailed reply :slight_smile:

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! [import]uid: 87138 topic_id: 18095 reply_id: 69321[/import]