Flash AS3 - 对象未设置为null

时间:2021-07-23 21:37:02

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.

我有一个我在Flash中构建的菜单系统,允许您选择一个项目并向上或向下移动它。如果从菜单中删除对象,我希望将所选项目设置为null,因此不会再尝试移动它。

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:

它存储选择的菜单项(菜单项只是影片剪辑),如果删除该项,我将所选项设置为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:

该跟踪将显示null而没有任何问题,但在我的下一个向上移动项目的函数中,在我将其设置为null之后,它仍然设置为删除之前的对象:

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.

但是当跟踪在4秒后发生时,它表示它仍然是所选对象,即使后面的跟踪显示为null,并且我在代码中的任何地方都看到了,但是没有任何地方可以重置。

5 个解决方案

#1


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?

从我可以看到它应该保持为null。你可以发布一些其他代码,即在这两个函数调用的上下文中?

#2


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.

这段代码“活着”在哪里?如果它在时间轴上,则可以再次调用设置selectedPlaylistItem的代码,在任何情况下,我都会在设置selectedPlaylistItem的位置粘贴一些跟踪或断点,以查看是否意外调用了它。

#3


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.

你的代码是不是超过两帧或更多?我的猜测是,它正在重新运行代码。所以在脚本的进程中,它将被设置为null而当前帧还没有渲染,但如果它循环回到(比如说)第1帧,那么它可能被称为一些代码来再次设置该变量,因此你会在4秒后看到它被设置为什么。

#4


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

如果在运行removeFromPlaylist()时屏幕上显示selectedPlaylistItem,请尝试从显示列表中删除该对象,然后将其置零。

#5


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.

弱引用是垃圾收集器不计算的引用(即,它不计入引用计数,并且不遵循标记清除)。这意味着如果对象剩余的唯一引用很弱,则可以在下一次GC扫描时收集它。

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.

与事件侦听器关联的引用经常被开发人员遗忘,这通常会导致侦听器永远不会从内存中删除。这就是为什么弱引用的事件监听器在AS3中非常方便的原因 - 如果您忘记删除监听器,则不会妨碍垃圾收集器收集对象的能力。

// params: eventName, listener, capturePhase, priority, useWeakReference
someObj.addEventListener("eventName",myFunct,false,0,true);

#1


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?

从我可以看到它应该保持为null。你可以发布一些其他代码,即在这两个函数调用的上下文中?

#2


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.

这段代码“活着”在哪里?如果它在时间轴上,则可以再次调用设置selectedPlaylistItem的代码,在任何情况下,我都会在设置selectedPlaylistItem的位置粘贴一些跟踪或断点,以查看是否意外调用了它。

#3


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.

你的代码是不是超过两帧或更多?我的猜测是,它正在重新运行代码。所以在脚本的进程中,它将被设置为null而当前帧还没有渲染,但如果它循环回到(比如说)第1帧,那么它可能被称为一些代码来再次设置该变量,因此你会在4秒后看到它被设置为什么。

#4


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

如果在运行removeFromPlaylist()时屏幕上显示selectedPlaylistItem,请尝试从显示列表中删除该对象,然后将其置零。

#5


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.

弱引用是垃圾收集器不计算的引用(即,它不计入引用计数,并且不遵循标记清除)。这意味着如果对象剩余的唯一引用很弱,则可以在下一次GC扫描时收集它。

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.

与事件侦听器关联的引用经常被开发人员遗忘,这通常会导致侦听器永远不会从内存中删除。这就是为什么弱引用的事件监听器在AS3中非常方便的原因 - 如果您忘记删除监听器,则不会妨碍垃圾收集器收集对象的能力。

// params: eventName, listener, capturePhase, priority, useWeakReference
someObj.addEventListener("eventName",myFunct,false,0,true);