ForumsProgramming ForumProblem with object depths and duplications

5 2817
Jcourtney
offline
Jcourtney
8 posts
10

I'm currently having trouble with the depth of objects that I duplicate.

In this case I have a plane(s) that fly over and drop a bomb when I press SPACE. The more I press space, the more planes I get.

I didn't have any trouble with the depths of the planes, as I could easily get many on the screen. But when they are called to drop the bomb, the bombs appear, and when the next plane drops a bomb... the previous bomb is overwritten, or erased, and can be seen falling at the origin of the page(top left).

This is the script for the planes:

var i = 0;
var timer = 5;
this.onEnterFrame = function() {
timer++;
if (Key.isDown(Key.RIGHT)) {
object._x += 5;
}
if (Key.isDown(Key.LEFT)) {
object._x -= 5;
}
if (Key.isDown(Key.SPACE)) {
if (timer>=5) {
i++;
_root.attachMovie("full_plane","full_plane"+i,_root.getNextHighestDepth());
_root["full_plane"+i]._x = -15;
_root["full_plane"+i]._y = Math.random()*250;
timer = 0;
}
}
};



This is script for the bombs:

var k = 0;
dropped = false;
this.onEnterFrame = function() {
this._x += 10;
if (this._x>=_root.object._x) {
if (!dropped) {
k++;
_root.attachMovie("bomb","bomb"+k,_root.getNextHighestDepth());
_root["bomb"+k]._x = this._x;
_root["bomb"+k]._y = this._y+3;
dropped = true;
}
}
};

Can anyone explain how to fix this to me please?

Thanks.

  • 5 Replies
Jcourtney
offline
Jcourtney
8 posts
10

Note:

The first script is located in the main timeline, the second is located inside the timeline of the plane.

dank
offline
dank
1,002 posts
960

Instead of using getNextHighestDepth, try using a number (1000) then add i on to it. Give all the dupelicated MCs their own number, such as planes would be at 1000, bombs would be at 2000, etc.

MikeVarilek
offline
MikeVarilek
36 posts
40

I don't see a problem with your code.
instead of this

k++;
_root.attachMovie("bomb","bomb"+k,_root.getNextHighestDepth());
_root["bomb"+k]._x = this._x;
_root["bomb"+k]._y = this._y+3;

try this
depth = _root.getNextHighestDepth();
_root.attachMovie("bomb","bomb"+depth,depth);
currentBomb = eval("_root.bomb"+depth);
currentBomb._x = this._x;
currentBomb._y = this._y+3;
Jcourtney
offline
Jcourtney
8 posts
10

depth = _root.getNextHighestDepth();
_root.attachMovie("bomb","bomb"+depth,depth);
currentBomb = eval("_root.bomb"+depth);
currentBomb._x = this._x;
currentBomb._y = this._y+3;


Thanks, MikeVarilek! It worked perfectly. I still don't know as to why the script I had didn't work.
MikeVarilek
offline
MikeVarilek
36 posts
40

No Problem

Showing 1-5 of 5