Move Object To Another Object With A Specific Variable

Hello, I need to move an object to another object with a specific variable. Example:

player = display.newRect(0,0,100,100) obj = {} for i = 1,2 do obj[i] = display.newRect(0,0,100,100) obj[i].ID = "" function objControl() if moveObject == true then player.x = obj[i].x player.x = obj[i].x end end end obj[1].ID = "target"

Okay! so my issue is how do I specify that I only want the player to move to the object that has the ‘.ID = “target”’?

Well, you could go through your objects in a for loop and check for it. i.e:

for i = 1, #obj do if obj[i].ID == "target" then player.x = obj[i].x end end

I see a number of other issues with your code and suspect you need a better grasp of scope. Thankfully there was a very handy recent tutorial on just that, which you should look at here:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Thank you very much!! :slight_smile:

i’ve made you a simple code so you can learn from:

local rand=math.random local numberObjects=10 local randomTarget=rand(1,numberObjects) local trans local function createObject(posXIn, posYIn, sizeXIn, sizeYIn) local posX=posXIn local posY=posYIn local sizeX=sizeXIn local sizeY=sizeYIn local obj=display.newRect(posX,posY,sizeX,sizeY) obj.id="" return obj end local multiObjects={} for i=1, numberObjects do multiObjects[i]=createObject(rand(1,display.contentWidth),rand(1,display.contentHeight),rand(50,100),rand(50,100)) multiObjects[i]:setFillColor(rand(),rand(),rand()) if i==numberObjects then multiObjects[i].id="target" multiObjects[i]:setFillColor(1,0,0) end end local player=createObject(0,0,50,50) player:setFillColor(1,0,0) local function removeTrans() if trans then transition.cancel(trans) trans=nil end end local function moveToTarget() for i=1, numberObjects do if multiObjects[i].id=="target" then local trans=transition.to(player,{time=1000,x=multiObjects[i].x, y=multiObjects[i].y,onComplete=removeTrans}) end end end timer.performWithDelay(1000,moveToTarget,1)

after 1s the red square goes after the other red rectangle in the screen (the target).

Well, you could go through your objects in a for loop and check for it. i.e:

for i = 1, #obj do if obj[i].ID == "target" then player.x = obj[i].x end end

I see a number of other issues with your code and suspect you need a better grasp of scope. Thankfully there was a very handy recent tutorial on just that, which you should look at here:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Thank you very much!! :slight_smile:

i’ve made you a simple code so you can learn from:

local rand=math.random local numberObjects=10 local randomTarget=rand(1,numberObjects) local trans local function createObject(posXIn, posYIn, sizeXIn, sizeYIn) local posX=posXIn local posY=posYIn local sizeX=sizeXIn local sizeY=sizeYIn local obj=display.newRect(posX,posY,sizeX,sizeY) obj.id="" return obj end local multiObjects={} for i=1, numberObjects do multiObjects[i]=createObject(rand(1,display.contentWidth),rand(1,display.contentHeight),rand(50,100),rand(50,100)) multiObjects[i]:setFillColor(rand(),rand(),rand()) if i==numberObjects then multiObjects[i].id="target" multiObjects[i]:setFillColor(1,0,0) end end local player=createObject(0,0,50,50) player:setFillColor(1,0,0) local function removeTrans() if trans then transition.cancel(trans) trans=nil end end local function moveToTarget() for i=1, numberObjects do if multiObjects[i].id=="target" then local trans=transition.to(player,{time=1000,x=multiObjects[i].x, y=multiObjects[i].y,onComplete=removeTrans}) end end end timer.performWithDelay(1000,moveToTarget,1)

after 1s the red square goes after the other red rectangle in the screen (the target).