简体   繁体   中英

Flash AS3 - Object not getting set to null

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.

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

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.

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.

如果在运行removeFromPlaylist()时在屏幕上可见selectedPlaylistItem ,请尝试在将显示为空之前将其从显示列表中删除。

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM