How can an object follow another? Example: the asteroid following the ship in starExplorer.

I tried to make a big asteroid and make it follow the ship everytime I change it location until it reaches it. I tried to modify the dragShip function but when I drag the ship, the asteroid just follow the initial position of the ship. Can you help me?  I am on the starExplorer tutorial in Corona.

boss = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

_ boss.myName = “boss” _

_ boss.x = display.contentCenterX _

boss.y = (display.contentCenterY)*0.5

_ boss.xScale = 2.5 _

_ boss.yScale = 2.5 _

function dragShip( event )

_ ship = event.target _

_ phase = event.phase _

if ( “began” == phase ) then

_ – Set touch focus on the ship _

display.currentStage:setFocus( ship )

_ – Store initial offset position _

_ ship.touchOffsetX = event.x - ship.x _

    boss = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y} )

elseif ( “moved” == phase ) then

_ – Move the ship to the new touch position _

_ ship.x = event.x - ship.touchOffsetX _

    boss = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y} )

elseif ( “ended” == phase or “cancelled” == phase ) then

_ – Release touch focus on the ship _

display.currentStage:setFocus( nil )

    boss = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y} )

_ end _

_ return true  – Prevents touch propagation to underlying objects _

_ end _

If this code is exactly as it appears in your app I’m surprised you are not getting a runtime error:

boss = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y} )

This bit of code is overriding the “boss” variable by assigning a transition object to it. The next time you call it I would expect to see an error because you are trying to set a transition on a transition.

Try something like this instead:

local function clearTransition() if boss.transition then transition.cancel(boss.transition) boss.transition = nil end end function dragShip( event ) ship = event.target phase = event.phase --clear the previous transition, because a new one will be set on every phase clearTransition() if ( "began" == phase ) then -- Set touch focus on the ship display.currentStage:setFocus( ship ) -- Store initial offset position ship.touchOffsetX = event.x - ship.x boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) elseif ( "moved" == phase ) then -- Move the ship to the new touch position ship.x = event.x - ship.touchOffsetX boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ship display.currentStage:setFocus( nil ) boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) end return true -- Prevents touch propagation to underlying objects end

Notice 2 things. Firstly I am setting the transition as a property of the boss object, rather than overriding the variable itself:

boss.transition = transition.to(etc) --rather than boss = transition.to(etc)

Secondly, I have added a function to clear that property once a transition ends. This occurs either when the transitions finished naturally, or when the touch function causes a new transition to be set.

I should also point out that everything appears to be a global variable in your code, nothing is defined as “local”. Perhaps this is needed in your game, but generally it’s best to avoid global variables wherever possible.

I just remembered something else. If you use a transition with a fixed time, the speed of the boss is going to change depending on how far away it is. For example if the boss is only 4 pixels away, and it takes 4000ms to reach you, it will only move at 1 pixel per second. If it is 400 pixels away, it will move at 100 pixels per second. This means that as you move, the speed of the boss will appear jerky as it constantly changes speed. 

A better approach may be to use some trigonometry to calculate the distance between the 2 objects, and set the time based on that.

If this code is exactly as it appears in your app I’m surprised you are not getting a runtime error:

boss = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y} )

This bit of code is overriding the “boss” variable by assigning a transition object to it. The next time you call it I would expect to see an error because you are trying to set a transition on a transition.

Try something like this instead:

local function clearTransition() if boss.transition then transition.cancel(boss.transition) boss.transition = nil end end function dragShip( event ) ship = event.target phase = event.phase --clear the previous transition, because a new one will be set on every phase clearTransition() if ( "began" == phase ) then -- Set touch focus on the ship display.currentStage:setFocus( ship ) -- Store initial offset position ship.touchOffsetX = event.x - ship.x boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) elseif ( "moved" == phase ) then -- Move the ship to the new touch position ship.x = event.x - ship.touchOffsetX boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ship display.currentStage:setFocus( nil ) boss.transition = transition.to( boss ,{ time=4000, x=ship.x, y=ship.y, onComplete = clearTransition} ) end return true -- Prevents touch propagation to underlying objects end

Notice 2 things. Firstly I am setting the transition as a property of the boss object, rather than overriding the variable itself:

boss.transition = transition.to(etc) --rather than boss = transition.to(etc)

Secondly, I have added a function to clear that property once a transition ends. This occurs either when the transitions finished naturally, or when the touch function causes a new transition to be set.

I should also point out that everything appears to be a global variable in your code, nothing is defined as “local”. Perhaps this is needed in your game, but generally it’s best to avoid global variables wherever possible.

I just remembered something else. If you use a transition with a fixed time, the speed of the boss is going to change depending on how far away it is. For example if the boss is only 4 pixels away, and it takes 4000ms to reach you, it will only move at 1 pixel per second. If it is 400 pixels away, it will move at 100 pixels per second. This means that as you move, the speed of the boss will appear jerky as it constantly changes speed. 

A better approach may be to use some trigonometry to calculate the distance between the 2 objects, and set the time based on that.