PDA

View Full Version : removing a movieclip from stage.


arianhojat
02-24-2006, 07:35 PM
blah i havent done flash actionscript in a while and was wondering about some simple coding. Bascially what happens is a user clicks on a combo box and a event fires which adds a bullseye movieclip on a layout of the Office where the user is located.

I have a clip in my library set for actionscript export called bullseye, i attach a clip of this to the stage when a user clicks on the drop down, pretend the drop down has data 24, then the clip is called "bullseye24". then i remove it when user clicks another option... the thing is that without the remove code, it will still remove it, which stumps me.

any ideas? event code below...




function change(evt)
{
newEmployee = evt.target.selectedItem.data;//ID of employee given from ComboBox

//if lastClickedEmployee was set before from clicking a previous employee, remove his bullseye before adding new one.
if( !isNaN(lastClickedEmployee) )
{
var hideOldBullsEye = "bullseye"+lastClickedEmployee;
//trace("Removing "+hideOldBullsEye + "from position at x="+ _root[hideOldBullsEye]._x );
//removeMovieClip( this[hideOldBullsEye] ); //doesnt matter if this code is commented out, new clip still gets removed?
}

var locationBox = emp_locationsArray[newEmployee]; // has location info for the employee ID


var textBoxX = _root["textbox"+locationBox]._x; //finds position of a textbox that has users name in their Cube, and this is where the bullseye MC is placed
var textBoxY = _root["textbox"+locationBox]._y;
var newMC:MovieClip = this.attachMovie("bullseye", "bullseye"+newEmployee, 100, {_x: textBoxX, _y: textBoxY} );//seems like this removes the old one even thou 2 different instance names
trace("Adding "+ "bullseye"+newEmployee + "; @x="+textBoxX+", y="+textBoxY);
lastClickedEmployee = newEmployee; //set new last clicked employee


}
empComboBox.addEventListener("change", this);



btw where the heck is the 'Red Flash Code' icon for posting code from before? i am using the PHP code instead and seems to be fine

flashead
02-24-2006, 07:59 PM
your code:
var newMC:MovieClip = this.attachMovie("bullseye", "bullseye"+newEmployee, 100, {_x: textBoxX, _y: textBoxY} );

you're attaching all the movies to the same depth, and since only one movie can be at a certain depth at any given time, the new one will replace the old one.

either use getNextHighestDepth(); in place of your depth, or even better, use a depth variable and increment it each time. the depth variable is a better way to go, simply because older players won't support the getNextHighest... method. so it all really depends on your target version.
var newMC:MovieClip = this.attachMovie("bullseye", "bullseye"+newEmployee, depth++, {_x: textBoxX, _y: textBoxY} );

k.

edit: oh, and you can use [as] tags around your AS to format it.

arianhojat
02-27-2006, 02:19 PM
hmmm i tried that also to no avail. i trace all the movieclips on stage, it definately isnt doing anything to remove the movieclips even though they are at a higher depth on stage with this.getNextHighestDepth();

it has to be something simple, as removing a movieclip is a simple thing to do, well, should be, i must be doing something retarded.

can anyone take a quick look at the code or maybe even confirm its not removing the clip (i only put location data fro 1st 6 drop down employees, so test with those if u can)

[the ListMovieClips function, which I left out, loops through clips on _root and proves the clip isnt removed and visually obviously it doesnt so thats confirmation #2 :)]



function change(evt)
{//trace(evt.target.selectedItem.label +", "+evt.target.selectedItem.data +" clicked");

newEmployee = evt.target.selectedItem.data;

var locationBox = emp_locationsArray[ newEmployee ]; //this array has info on where to put the bullseye clip,
/*for example, i set emp_locationsArray[84] = 2; so when user clicks employee84, a clip will be made called bullseye84, set to coordinates in a Textfield labeling the room. so shows up near the textfield "textfield2"
*/


//hide old one, lastClickedEmployee
if( (!isNaN(lastClickedEmployee)) )
{
var hideOldBullsEye = "bullseye"+lastClickedEmployee;
trace("Removing "+hideOldBullsEye + "from position at x="+ _root[hideOldBullsEye]._x );
var hideClip:MovieClip = _root[ hideOldBullsEye ];
_root.hideClip.removeMovieClip();//removeMovieClip( hideClip ), _root.hideClip.removeMovieClip(), nothing works here.
lastClickedEmployee = "NaN";
}

if( !isNaN(locationBox) )//just make sure valid data from combobox chosen
{
var textBoxX = _root["textbox"+locationBox]._x;
var textBoxY = _root["textbox"+locationBox]._y;
var newMC:MovieClip = _root.attachMovie("bullseye", "bullseye"+newEmployee, this.getNextHighestDepth(), {_x: textBoxX, _y: textBoxY} );
trace("Adding "+ "bullseye"+newEmployee + "; @x="+textBoxX+", y="+textBoxY);
lastClickedEmployee = newEmployee; //SET NEW last clicked employee
}

listMovieClips();
}
empComboBox.addEventListener("change", this);

arianhojat
02-27-2006, 03:08 PM
I found this in an old thread...

removeMovieClip does not work when:

more than 1 instance has the same name (they dont in my case)
multiple movies on the same depth. (they dont since i use getNextHighestDepth())


The clip name is fine too as hideClip._visible = false; will work but in a more complicated bullseye clip will slow down the movie as more instances added

any more ideas guys?

arianhojat
02-27-2006, 03:17 PM
heh
hideClip.unloadMovie(); will work
but any combo of removeMovieClip will not work like
hideClip.removeMovieClip();
removeMovieClip( hideClip );
_root.hideClip.removeMovieClip();
_root[hideOldBullsEye].removeMovieClip();

now i am fine with using unloadMovie as a ghetto hack (it works but listMovieClips() still shows it on the timeline?), but i usually just use it for unloading .swf files and not dynamic files from library. Id just like to know why i had an issue with removeMovieClip still?

thanks again, hope this helps someone in future.

arianhojat
02-27-2006, 07:53 PM
heh i like replying to myself....

i beleive this is the answer...

"movie clips can only be removed if they have a depth between 0 and 1,048,575. Below or above that, and removeMovieClip wont work. If you're using components in _root, _root.getNextHighestDepth() can return numbers greater than 1,048,575 because of highest depth elements like tool tips."-senocular
http://actionscript.org/forums/showthread.php3?t=97908&highlight=removeMovieClip

thanks senocular!

senocular
02-27-2006, 08:30 PM
welcome, and thanks for reiterating the solution. Im sure it will be helpful to others reading this thread :)