AS3 - TypeError:MethodInfo 中的错误 #1009 “null”

Posted

技术标签:

【中文标题】AS3 - TypeError:MethodInfo 中的错误 #1009 “null”【英文标题】:AS3 - TypeError: Error #1009 "null" at MethodInfo 【发布时间】:2017-05-18 23:32:51 【问题描述】:

我有这个小项目。每次单击按钮时都会更改的帧,以及箭头内的进度条模拟。

stop();
/** initializtios **/
var trans:ColorTransform = next_arrow.transform.colorTransform;
var p:int = 1;

var myColorTransform = new ColorTransform();
myColorTransform.color = 0x42f498;
var trans1:ColorTransform = back_arrow.transform.colorTransform;

/** actual code **/
btn_next_arrow.addEventListener(MouseEvent.CLICK, function goNextLectFrame(e:MouseEvent):void 
    if (p==1) 
        fill_1.transform.colorTransform = myColorTransform;
        fill_22.alpha = 0.00001;
        fill_3.alpha = 0.00001;
        fill_33.alpha = 0.00001;
        fill_5.alpha = 0.00001;
        fill_6.alpha = 0.00001;
        fill_7.alpha = 0.00001;
        fill_8.alpha = 0.00001;
        fill_9.alpha = 0.00001;
        fill_10.alpha = 0.00001;
        fill_11.alpha = 0.00001;
        fill_12.alpha = 0.00001;
        fill_13.alpha = 0.00001;
        fill_14.alpha = 0.00001;
        fill_151.alpha = 0.00001;
        ++p;
    
    switch (currentFrame) 
        case 1:
            gotoAndStop(2);
            break;
        case 2:
            fill_22.transform.colorTransform = myColorTransform;
            gotoAndStop(3);
            break;
        case 3:
            fill_3.transform.colorTransform = myColorTransform;
            gotoAndStop(4);
            break;
        case 4:
            fill_33.transform.colorTransform = myColorTransform;
            gotoAndStop(5);
            break;
        case 5:
            fill_5.transform.colorTransform = myColorTransform;
            gotoAndStop(6);
            break;
        case 6:
            fill_6.transform.colorTransform = myColorTransform;
            gotoAndStop(7);
            break;
        case 7:
            fill_7.transform.colorTransform = myColorTransform;
            gotoAndStop(8);
            break;
        case 8:
            fill_8.transform.colorTransform = myColorTransform;
            gotoAndStop(9);
            break;
        case 9:
            fill_9.transform.colorTransform = myColorTransform;
            gotoAndStop(10);
            break;
        case 10:
            fill_10.transform.colorTransform = myColorTransform;
            gotoAndStop(11);
            break;
        case 11:
            fill_11.transform.colorTransform = myColorTransform;
            gotoAndStop(12);
            break;
        case 12:
            fill_12.transform.colorTransform = myColorTransform;
            gotoAndStop(13);
            break;
        case 13:
            fill_13.transform.colorTransform = myColorTransform;
            gotoAndStop(14);
            break;
        case 14:
            fill_14.transform.colorTransform = myColorTransform;
            gotoAndStop(15);
            break;
        case 15:
            fill_151.transform.colorTransform = myColorTransform;
            gotoAndStop(16);
            break;
        case 16:
            gotoAndStop(17);
            break;
    
);

btn_next_arrow.addEventListener(MouseEvent.MOUSE_OVER, function hovering(e:MouseEvent):void 
    trans.color = uint(0x606266);
    next_arrow.transform.colorTransform = trans;
);

btn_back_arrow.addEventListener(MouseEvent.MOUSE_OVER, function hovering1(e:MouseEvent):void 
    trans1.color = uint(0x606266);
    back_arrow.transform.colorTransform = trans1;
);

btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow(event:MouseEvent):void 
    trans.color = uint(0x999999);
    next_arrow.transform.colorTransform = trans;
);

btn_back_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow1(event:MouseEvent):void 
    trans1.color = uint(0x999999);
    back_arrow.transform.colorTransform = trans1;
);

btn_back_arrow.addEventListener(MouseEvent.CLICK, function goPrevLectFrame(e:MouseEvent):void 
        switch(currentFrame)
        case 2:
            gotoAndStop(1);
            break;
        case 3:
            gotoAndStop(2);
            break;
        case 4:
            gotoAndStop(3);
            break;
        case 5:
            gotoAndStop(4);
            break;
        case 6:
            gotoAndStop(5);
            break;
        case 7:
            gotoAndStop(6);
            break;
        case 8:
            gotoAndStop(7);
            break;
        case 9:
            gotoAndStop(8);
            break;
        case 10:
            gotoAndStop(9);
            break;
        case 11:
            gotoAndStop(10);
            break;
        case 12:
            gotoAndStop(11);
            break;
        case 13:
            gotoAndStop(12);
            break;
        case 14:
            gotoAndStop(13);
            break;
        case 15:
            gotoAndStop(14);
            break;
        case 16:
            gotoAndStop(15);
            break;
        
);

btn_main_menu.addEventListener(MouseEvent.CLICK, function goPrevLectFrame1(e:MouseEvent):void 
    gotoAndStop(17);
);

问题是当我到达最后一帧并单击下一步按钮时,它显示:

TypeError:错误 #1009:...“null”。在 函数/()[RECOVER_shitty_fla.MainTimeline::frame1:114]

这是导致函数的问题:

btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow(event:MouseEvent):void 
    trans.color = uint(0x999999);
    next_arrow.transform.colorTransform = trans; // LINE 114
);

我在这里真的很挣扎,将不胜感激任何帮助,在此先感谢。

【问题讨论】:

很可能,这意味着next_arrow 在该行运行时不存在。此外,侦听器上的内联函数是内存泄漏的快速通道。 @BadFeelingAboutThis 是的,我是这么想的,但我认为 next_arrow 确实存在于该帧上,只是在下一帧中不存在,单击按钮实际上会在将鼠标悬停在 next_arrow 上之后将我们带到。无论如何,谢谢。你的建议是关于内联函数的,我应该如何包装它们? 【参考方案1】:

您需要在进入最后一帧之前移除事件侦听器。您的程序仍在侦听该帧上不存在的对象上的MouseEvents。

此外,我会听取@BadFeelingAboutThis 的建议。 内联函数是不好的做法,通常应该避免。

此代码还有其他问题(大部分是小问题)。我已经从 FLA 文档中调整了您的代码以使程序正常工作。 请参阅下面的更改。


第 1-16 帧

stop();

import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.StageScaleMode; 
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.geom.ColorTransform;

/** initializtios **/
var trans:ColorTransform = next_arrow.transform.colorTransform;
var p:int = 1;
var myColorTransform = new ColorTransform();
myColorTransform.color = 0x42f498;
var trans1:ColorTransform = back_arrow.transform.colorTransform;

/** actual code **/
btn_next_arrow.addEventListener(MouseEvent.CLICK, goNextLectFrame);
function goNextLectFrame(e:MouseEvent):void 
    if (p==1) 

        fill_1.transform.colorTransform = myColorTransform;
        fill_22.alpha = 0.00001;
        fill_3.alpha = 0.00001;
        fill_33.alpha = 0.00001;
        fill_5.alpha = 0.00001;
        fill_6.alpha = 0.00001;
        fill_7.alpha = 0.00001;
        fill_8.alpha = 0.00001;
        fill_9.alpha = 0.00001;
        fill_10.alpha = 0.00001;
        fill_11.alpha = 0.00001;
        fill_12.alpha = 0.00001;
        fill_13.alpha = 0.00001;
        fill_14.alpha = 0.00001;
        fill_151.alpha = 0.00001;
        ++p;
    
    switch (currentFrame) 
        case 1:
            gotoAndStop(2);
            break;
        case 2:
            fill_22.transform.colorTransform = myColorTransform;
            gotoAndStop(3);
            break;
        case 3:
            fill_3.transform.colorTransform = myColorTransform;
            gotoAndStop(4);
            break;
        case 4:
            fill_33.transform.colorTransform = myColorTransform;
            gotoAndStop(5);
            break;
        case 5:
            fill_5.transform.colorTransform = myColorTransform;
            gotoAndStop(6);
            break;
        case 6:
            fill_6.transform.colorTransform = myColorTransform;
            gotoAndStop(7);
            break;
        case 7:
            fill_7.transform.colorTransform = myColorTransform;
            gotoAndStop(8);
            break;
        case 8:
            fill_8.transform.colorTransform = myColorTransform;
            gotoAndStop(9);
            break;
        case 9:
            fill_9.transform.colorTransform = myColorTransform;
            gotoAndStop(10);
            break;
        case 10:
            fill_10.transform.colorTransform = myColorTransform;
            gotoAndStop(11);
            break;
        case 11:
            fill_11.transform.colorTransform = myColorTransform;
            gotoAndStop(12);
            break;
        case 12:
            fill_12.transform.colorTransform = myColorTransform;
            gotoAndStop(13);
            break;
        case 13:
            fill_13.transform.colorTransform = myColorTransform;
            gotoAndStop(14);
            break;
        case 14:
            fill_14.transform.colorTransform = myColorTransform;
            gotoAndStop(15);
            break;
        case 15:
            fill_151.transform.colorTransform = myColorTransform;
            btn_next_arrow.removeEventListener(MouseEvent.MOUSE_OVER, hovering);
            btn_next_arrow.removeEventListener(MouseEvent.MOUSE_OUT, hideShadow);
            btn_back_arrow.removeEventListener(MouseEvent.MOUSE_OVER, hovering1);
            btn_back_arrow.removeEventListener(MouseEvent.MOUSE_OUT, hideShadow1);
            gotoAndStop(16);
            break;
        case 16:
            gotoAndStop(17);
            break;
    


btn_next_arrow.addEventListener(MouseEvent.MOUSE_OVER, hovering);
function hovering(e:MouseEvent):void 
    trans.color = uint(0x606266);
    next_arrow.transform.colorTransform = trans;


btn_back_arrow.addEventListener(MouseEvent.MOUSE_OVER, hovering1);
function hovering1(e:MouseEvent):void 
    trans1.color = uint(0x606266);
    back_arrow.transform.colorTransform = trans1;


btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, hideShadow);
function hideShadow(event:MouseEvent):void 
    trans.color = uint(0x999999);
    next_arrow.transform.colorTransform = trans;


btn_back_arrow.addEventListener (MouseEvent.MOUSE_OUT, hideShadow1);
function hideShadow1(event:MouseEvent):void 
    trans1.color = uint(0x999999);
    back_arrow.transform.colorTransform = trans1;


btn_back_arrow.addEventListener(MouseEvent.CLICK, goPrevLectFrame);
function goPrevLectFrame(e:MouseEvent):void 
        switch(currentFrame)
        case 2:
            gotoAndStop(1);
            break;
        case 3:
            gotoAndStop(2);
            break;
        case 4:
            gotoAndStop(3);
            break;
        case 5:
            gotoAndStop(4);
            break;
        case 6:
            gotoAndStop(5);
            break;
        case 7:
            gotoAndStop(6);
            break;
        case 8:
            gotoAndStop(7);
            break;
        case 9:
            gotoAndStop(8);
            break;
        case 10:
            gotoAndStop(9);
            break;
        case 11:
            gotoAndStop(10);
            break;
        case 12:
            gotoAndStop(11);
            break;
        case 13:
            gotoAndStop(12);
            break;
        case 14:
            gotoAndStop(13);
            break;
        case 15:
            gotoAndStop(14);
            break;
        case 16:
            gotoAndStop(15);
            break;
        


btn_main_menu.addEventListener(MouseEvent.CLICK, goPrevLectFrame1);
function goPrevLectFrame1(e:MouseEvent):void 
    gotoAndStop(17);

第 17 帧

stop();
import flash.events.MouseEvent;

btn_goto_lect1.alpha = 0.00001;
btn_goto_pract.alpha = 0.00001;

btn_goto_lect1.addEventListener (MouseEvent.MOUSE_OVER, showShadow);
function showShadow(event:MouseEvent):void 
    btn_goto_lect1.alpha = 1;


btn_goto_lect1.addEventListener (MouseEvent.MOUSE_OUT, hideShadow_);
function hideShadow_(event:MouseEvent):void 
    btn_goto_lect1.alpha = 0.00001;


btn_goto_pract.addEventListener (MouseEvent.MOUSE_OVER, showShadow1);
function showShadow1(event:MouseEvent):void 
    btn_goto_pract.alpha = 1;


btn_goto_pract.addEventListener (MouseEvent.MOUSE_OUT, hideShadow2);
function hideShadow2(event:MouseEvent):void 
    btn_goto_pract.alpha = 0.00001;


btn_goto_lect1.addEventListener (MouseEvent.CLICK, showShadow3);

function showShadow3(event:MouseEvent):void 
    btn_goto_lect1.removeEventListener (MouseEvent.MOUSE_OVER, showShadow);
    btn_goto_lect1.removeEventListener (MouseEvent.MOUSE_OUT, hideShadow_);
    btn_goto_pract.removeEventListener (MouseEvent.MOUSE_OVER, showShadow1);
    btn_goto_pract.removeEventListener (MouseEvent.MOUSE_OUT, hideShadow2);
    gotoAndStop(1);

【讨论】:

亲爱的@phasma,非常感谢,现在它就像手表一样工作。更重要的是,感谢您澄清 removeListener,我正在慢慢进入 as3。我一定会坚持你关于内联函数的建议。再次感谢您!对不起,你必须为我工作的代码质量:) @Monstryyy 在您听取建议的同时...将代码放入一帧中,将任何多帧视觉效果放入舞台上的 MovieCip(与代码相同的帧 1,但新的不同层),然后使用一帧代码来控制“内容”MClip 本身移动到其他帧...这意味着您的代码不必在 stage 的其他帧上重复,但您可以通过 MClip 显示新帧(所以它必须与舞台大小相同)。 PS:您可以从舞台剪切/粘贴帧到 MC。此外,如果需要,您可以选择对象并右键单击将其转换为 MClip 等。 @VC.One 谢谢建议。是的,我明白你的建议,似乎比我的意大利面条代码更有效率。谢谢,现在我需要考虑一下。

以上是关于AS3 - TypeError:MethodInfo 中的错误 #1009 “null”的主要内容,如果未能解决你的问题,请参考以下文章

AS3 中的“TypeError:错误 #1010:术语未定义且没有属性”

AS3 Starling Framework Texture Atlas TypeError #1007

TypeError:错误#1009:无法访问null对象引用as3的属性或方法

AS3 阶段 = 空?

需要帮助清理代码 (AS 3.0)

AS3 SWF展望(as3swf)