Hi, i have two objects on stage ones called runner and ones called chaser. what i am trying to do is when the chaser is dragged towards the runner, the runner moves away until it is out of the agroRange(50 in this case)
here’s some code to show you what i mean
[lua]function checkRunner(event)
–calculate distance between runner and target
distanceX = chaser.x - runner.x;
distanceY = chaser.y - runner.y;
–get total distance as one number
distanceTotal = math.sqrt(distanceX * distanceX + distanceY * distanceY);
–check if target is within agro range
if (distanceTotal > agroRange) and (distanceTotal < 300) then
–calculate how much to move
moveDistanceX = turnRate*distanceX/distanceTotal;
moveDistanceY = turnRate*distanceY/distanceTotal;
–increase current speed
moveX = moveX + moveDistanceX ;
moveY = moveY + moveDistanceY;
–get total move distance
totalmove = math.sqrt(moveX*moveX+moveY*moveY);
–apply easing
moveX = speed*moveX/totalmove;
moveY = speed*moveY/totalmove;
–move runner
runner.x = runner.x - moveX;
runner.y = runner.y - moveY;
–rotate runner away from target
runner.rotation = 180*math.atan2(moveY, moveX)/math.pi;
end
end
Runtime:addEventListener(“enterFrame”, checkRunner)[/lua]
that seems to work fine when there isn’t no gravity, however when i introduce gravity (physics.setGravity (0, 1)). it seems to be that gravity is overpowering that function.
i have tried using applyForce but i have had no luck
[import]uid: 34863 topic_id: 11863 reply_id: 311863[/import]