I'm making an overhead game with a scrolling map.
Here's my code, on the "player" instance. Obstacles are named "wall1", "wall2", etc. The game and player are in a movieclip, covered by an interface, which is the line you see across the middle of the screenshot below. The entire thing is in a movieclip on the _root layer.
onClipEvent (load) {
var speed = 5;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_parent._x += speed;
this._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_parent._x -= speed;
this._x += speed;
}
if (Key.isDown(Key.UP)) {
_parent._y += speed;
this._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_parent._y -= speed;
this._y += speed;
}
for (i in this._parent) {
if (_parent[i]._name.indexOf("wall") != -1) {
while (_parent[i].hitTest(this._x, this._y+this._height/2, true)) {
_parent._y+=speed;
this._y-=speed;
}
while (_parent[i].hitTest(this._x, this._y-this._height/2, true)) {
_parent._y-=speed;
this._y+=speed;
}
while (_parent[i].hitTest(this._x+this._width/2, this._y, true)) {
_parent._x+=speed;
this._x-=speed;
}
while (_parent[i].hitTest(this._x-this._width/2, this._y, true)) {
_parent._x-=speed;
this._x+=speed;
}
}
}
}
The problem is, the ball is being stopped in an odd location (see below). As I walk around, there are "ghosts" of my barriers and walls, displaced a few hundred pixels from the real movieClips.
Screenshot: