Mc's script stops working when inside another mc

I have things to where the player is on one layer, and the level mc (the ground) is below it on its own layer; I call it the bg mc, and it scrolls with the arrow keys. All obstacles and items are in the bg mc. I am trying to get the player to ‘chase’ another npc around when they hittest it.

At first I used a simple walls code that I put on 4 line mcs to form a box around the object, and then put them in the object so the player basically pushes the mc around, appearing to have ‘chased’ it-

Example:

onClipEvent (enterFrame) {
if(this.hitTest(_root.player)){
_parent._x+=12;
}}
(this is for the left line, so the player pushes it to the right)

This works essentially, but makes the object mc huge, and sloppy looking when pushed. I also have to put multiple mcs of this throughout the level, so I can’t name the object (and don’t want to name it).

I tried this code for the object in a test scene with the just player, and the object on its own layer:

onClipEvent (enterFrame) {
distance = 60;
x = _root.player._x;
y = _root.player._y;
ex = this._x;
ey = this._y;
if (Math.sqrt((x-ex)(x-ex)+(y-ey)(y-ey))<
distance) {
if (rx<erx-30) {
this._x += 12;
}
if (x>ex+30) {
this._x -= 12;
}
if (x<ex-30) {
this._x += 12;
}
if (y>ey+30) {
this._y -= 12;
}
if (y<ey-30) {
this._y += 12;
}
}
}

It works splendidly, but when I put the object mc BACK into the level mc, it stops working entirely…

I think its because the coordinates between the object and player change because it has to go through the new, and large bg mc. I have no clue on how change the new code to accomodate the desired action through the bg. Could anyone please explain how to fix this?
Thanks :wink: