views:

1051

answers:

5

I have menu system that I built in Flash that allows you to select an item and move it up or down. If the object is removed from the menu, I want the selected item to be set to null, so it won't try to be moved anymore.

I have a global (for the current movie clip timeline) variable:

var selectedPlaylistItem:MovieClip;

That stores which menu item is selected (menu items are just movie clips), and if that item is removed, I set the selected item to null:

function removeFromPlaylist(sender:playlist_content_item) {
    if(sender == selectedPlaylistItem) {
     //Not sure why this isn't working, but at some point I need to figure it out.
     selectedPlaylistItem = null;
     trace(selectedPlaylistItem);

That trace will show null without any issues, but in my next function that moves items up, after I have set it to null, it is still set to the object that it was before it was removed:

function playlistUp(sender:MovieClip) {
    trace(selectedPlaylistItem);

That trace will show the original object.

Does anyone know why that wouldn't work? Why wouldn't it just stay null after it was set that way?

Thanks


UPDATE:

So I tried this to see if I could figure out what's going on:

 this.selectedPlaylistItem = null;
 trace(selectedPlaylistItem);
 setTimeout(function() {trace(selectedPlaylistItem);}, 4000);

But when the trace happens 4 seconds later, it says it's still the selected object even though the trace right after shows it as null, and I've looked everywhere in the code, but there's nowhere it would be getting reset.

+1  A: 

From what I can see it should stay null. Can you post some of your other code, i.e. in what context are these two functions being called?

Cameron
They are getting called from button clicks. They are both functions at the same place in the movie clip, so I'm really at a loss as to why that wouldn't work.
Ryan Smith
I'm at a loss too. The only thing I can think of is if it isn't getting in the if in removeFromPlaylist... but you said the trace outputs null. Could also be a scoping problem, where you accidentally have two variables named selectedPlaylistItem, but that's unlikely. Try stepping through it in the debugger (Ctrl+Shift+Enter in Flash CS3/CS4)
Cameron
Ya, I'm definitly hitting the place where it sets it to null. I stepped through on the debugugger, it it was exactly the same as what the traces said. The variable isn't declared anywhere else (I looked and I would assume it would throw an error anyway) so it isn't a scoping issue.
Ryan Smith
Looks like a real doozy. Just keep at it, I'm sure it's something simple that you'll find eventually. Worst comes to worst, just rewrite that part of the code a different way
Cameron
+1  A: 

Where does this code 'live'? If it's on the timeline, your code setting the selectedPlaylistItem could be being called again, in any case, I'd stick some traces or breakpoints in where you set selectedPlaylistItem to see if it's unexpectedly being called.

quoo
A: 

Have all event listeners been removed from the object and/or are you using Weak References?:

A weak reference is one that is not counted by the Garbage Collector (ie. it is not counted in reference counting, and it is not followed for mark sweeping). This means that if the only references remaining to an object are weak, it will be available for collection on the next GC sweep.

References associated with event listeners are often forgotten by developers, which normally results in the listener never being removed from memory. This is why weakly referenced event listeners are so handy in AS3 - if you forget to remove the listener, you will not impede the Garbage Collector's ability to collect the object.

// params: eventName, listener, capturePhase, priority, useWeakReference
someObj.addEventListener("eventName",myFunct,false,0,true);
Jeff Winkworth
I'm not really seeing how this is relevant, he's not asking why the variable isn't being garbage collected. An event listener wouldn't keep the variable from being set to null.
quoo
A: 

Does you code happen to be over two frames or more? My guess is that it's running the code over again. So in the progression of the script, it would be set to null while the current frame has yet to render, but if it looped around back to (say) frame 1, then it may be called some code to set that variable again, hence you'd see it set to something 4 seconds later.

Typeoneerror
It's on a movie clip with just one frame. I've found a workaround, but I still don't understand why this wasn't staying null.
Ryan Smith
A: 

If selectedPlaylistItem is visible on the screen while you run removeFromPlaylist(), try removing the object from the display list before nulling it.

Niko Nyman