Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting dynamically instantiated movieclips

Status
Not open for further replies.

Legion6789

Programmer
Jun 11, 2007
2
I'm making a video game where you have a turret that fires multiple bullets. Each bullet is an composition object that stores a movieclip and some properties such as a desination x and y, an angle for direction and a speed. Once the bullet reaches it's destination I want to delete it so it doesn't stay in memory. I've tried using delete this, delete this.onEnterFrame, this.removeMovieClip but when I use the debugger it still show all the instances in memory. Given that in a single game session the user could easily exceed 1000 bullets this thing will become a huge memory hog. Here's the class definition code.

------------
Actionscript
------------

class Bullet
{
private var pTargetClip:MovieClip;

public function Bullet(targetClip:MovieClip)
{
//trace("targetClip: "+targetClip);
pTargetClip = targetClip;
//trace("pTargetClip: " + pTargetClip);
}

public function shoot(destx:Number, desty:Number, angle:Number, speed:Number):Void
{
var spd:Number;

spd = speed;

pTargetClip.onEnterFrame = function()
{
if (this._y <= desty)
{
this._visible = false;
delete this.onEnterFrame;
this.swapDepths(10000);
this.removeMovieClip();
}
else
{
if (destx > this._x)
{
this._x += spd*Math.cos(angle);
}
else
{
this._x -= spd*Math.cos(angle);
}
this._y -= spd*Math.abs(Math.sin(angle));
}
}
}
}


Here's the code from the event that creates the object in the main timeline.

------------
Actionscript
------------

this.onMouseDown = function() {
var destx:Number = _xmouse;
var desty:Number = _ymouse;
var angle:Number = Math.atan(((_ymouse-420)/(_xmouse-268)));

(shotnum<1000) ? shotnum++ : shotnum=0;
this.attachMovie("myBullet","b"+shotnum,this.getNextHighestDepth(),{_x:268, _y:420});
trace(_level0.b1.getDepth());
var myBull = new Bullet(this["b"+shotnum]);
myBull.shoot(destx,desty,angle,10);
}

Here are some of the weird things I've noticed beyond the fact that the movieclips are never removed. Using the getDepth method in either the maintimeline or the Bullet.as class definition file always returns undefined, whether I use getNextHighestDepth, an incremental variable or just a straight up number like 1. In the class definition when I trace pTargetClip and targetClip to see what they point to I get the following, in this exact order:
targetClip: undefined
pTargetClip: undefined
targetClip: _level0.b1
pTargetClip: _level0.b1

This happens everytime the event fires which is odd because it seems to fire the constructor twice the first time with the variable undefined and the second time with them defined. Either way removeMovieClip() doesn't work.

Any suggestions?
 
try using unloadMovieClip() instead of removeMovieClip()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top