AS3:#1009:无法访问空对象引用的属性或方法

时间:2013-03-27 18:33:40

标签: actionscript-3 flash actionscript air

我想请求您帮助我的代码。它编译好,但在达到以下代码时会抛出此错误:

  

错误#1009:无法访问null对象的属性或方法   参考   HiddenObjects_fla :: MainTimeline / myFunction的()[HiddenObjects_fla.MainTimeline :: frame12:9]

我的主时间轴中没有第12帧。

import flash.display.Sprite;
import flash.events.*;

addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event):void {
var lazania:int = 0;
typewriter_piece1.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
typewriter_piece1.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
typewriter_piece2.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2); 
typewriter_piece2.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
typewriter_piece3.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
typewriter_piece3.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
typewriter_piece4.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
typewriter_piece4.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
typewriter_piece5.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
typewriter_piece5.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
typewriter_piece6.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
typewriter_piece6.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
stop();

//typewriter piece 1 = K 
function start_drag_piece1(event:MouseEvent):void{
typewriter_piece1.startDrag();
};
function stop_drag_piece1(event:MouseEvent):void{
if (Target1.hitTestObject(typewriter_piece1.Target1_Button)){
    typewriter_piece1.stopDrag();
    typewriter_piece1.x = Target1.x;
    typewriter_piece1.y = Target1.y;
    typewriter_piece1.mouseEnabled = false;
    typewriter_piece1.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }
    //checklazania(lazania);
}
else{
    typewriter_piece1.x = 25;
    typewriter_piece1.y = 139,95;
    typewriter_piece1.stopDrag();
}
};

//typewriter piece 2 = Y 
function start_drag_piece2(event:MouseEvent):void{
typewriter_piece2.startDrag();
};
function stop_drag_piece2(event:MouseEvent):void{
if (Target2.hitTestObject(typewriter_piece2.Target2_Button)){
    typewriter_piece2.stopDrag();
    typewriter_piece2.x = Target2.x;
    typewriter_piece2.y = Target2.y;
    typewriter_piece2.mouseEnabled = false;
    typewriter_piece2.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }
    //checklazania(lazania);

}
else{
    typewriter_piece2.x = 25;
    typewriter_piece2.y = 179,95;       
    typewriter_piece2.stopDrag();
}
};

//typewriter piece 3 = Π
function start_drag_piece3(event:MouseEvent):void{
typewriter_piece3.startDrag()
};
function stop_drag_piece3(event:MouseEvent):void{
if (Target3.hitTestObject(typewriter_piece3.Target3_Button)){
    typewriter_piece3.stopDrag();
    typewriter_piece3.x = Target3.x;
    typewriter_piece3.y = Target3.y;
    typewriter_piece3.mouseEnabled = false;
    typewriter_piece3.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }
    //checklazania(lazania);
}
else{
    typewriter_piece3.x = 25;
    typewriter_piece3.y = 219,95;       
    typewriter_piece3.stopDrag();
}
};

//typewriter piece 4 = P
function start_drag_piece4(event:MouseEvent):void{
typewriter_piece4.startDrag()
};
function stop_drag_piece4(event:MouseEvent):void{
if (Target4.hitTestObject(typewriter_piece4.Target4_Button)){
    typewriter_piece4.stopDrag();
    typewriter_piece4.x = Target4.x;
    typewriter_piece4.y = Target4.y;
    typewriter_piece4.mouseEnabled = false;
    typewriter_piece4.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }   
    //checklazania(lazania);
}
else{
    typewriter_piece4.x = 25;
    typewriter_piece4.y = 259,95;
    typewriter_piece4.stopDrag();
}
};

//typewriter piece 5 = O
function start_drag_piece5(event:MouseEvent):void{
typewriter_piece5.startDrag()
};
function stop_drag_piece5(event:MouseEvent):void{
if (Target5.hitTestObject(typewriter_piece5.Target5_Button)){
    typewriter_piece5.stopDrag();
    typewriter_piece5.x = Target5.x;
    typewriter_piece5.y = Target5.y;
    typewriter_piece5.mouseEnabled = false;
    typewriter_piece5.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }
    //checklazania(lazania);
}
else{
    typewriter_piece5.x = 25;
    typewriter_piece5.y = 299,95;

    typewriter_piece5.stopDrag();
}
};

//typewriter piece 6 = Σ
function start_drag_piece6(event:MouseEvent):void{
typewriter_piece6.startDrag()
};
function stop_drag_piece6(event:MouseEvent):void{
if (Target6.hitTestObject(typewriter_piece6.Target6_Button)){
    typewriter_piece6.stopDrag();
    typewriter_piece6.x = Target6.x;
    typewriter_piece6.y = Target6.y;
    typewriter_piece6.mouseEnabled = false;
    typewriter_piece6.mouseChildren = false;
    lazania++;
    if(lazania == 6){
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
        typewriter_piece1.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2);
        typewriter_piece2.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
        typewriter_piece3.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
        typewriter_piece4.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
        typewriter_piece5.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
        typewriter_piece6.removeEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
        gotoAndStop(1, "Scene 5");
    }
    //checklazania(lazania);
}
else{
    typewriter_piece6.x = 25;
    typewriter_piece6.y = 339,95;
    typewriter_piece6.stopDrag();
}
};

}

2 个答案:

答案 0 :(得分:2)

你的问题很可能是这样的:

addEventListener(Event.ENTER_FRAME,myFunction);

即使您拨打stop(),您的myFunction也会按照您的应用帧速率继续每帧运行一次。因此,如果您将帧速率设置为24,则myFunction每秒运行24次。这意味着你每秒钟反复添加所有这些听众24次。 (假设你的myFunction在那些鼠标事件监听器之后结束,你错过了结束}所以我不得不猜测。

你也永远不会删除那个ENTER_FRAME听众,所以即使你已经转移到程序的另一部分(通过gotoAndStop行),myFunction也会继续被称为每秒24次。 1009错误可能是因为您已转移到另一个框架,其中typewriter_piece实例不再存在,但ENTER_FRAME处理程序仍在反复运行该函数。

请使用以下代码:

typewriter_piece1.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece1);
typewriter_piece1.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece1);
typewriter_piece2.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece2); 
typewriter_piece2.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece2);
typewriter_piece3.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece3);
typewriter_piece3.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece3);
typewriter_piece4.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece4);
typewriter_piece4.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece4);
typewriter_piece5.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece5);
typewriter_piece5.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece5);
typewriter_piece6.addEventListener(MouseEvent.MOUSE_DOWN, start_drag_piece6);
typewriter_piece6.addEventListener(MouseEvent.MOUSE_UP, stop_drag_piece6);
stop();

out of myFunction(因此它只在框架上运行),并删除ENTER_FRAME监听器代码行。


作为效率和理智的提示,请尝试执行以下操作:

//This will add all your listeners with just 3 lines of code
for(var i:int=1;i<7;i++){
    this["typewriter_piece"+i].addEventListener(MouseEvent.MOUSE_DOWN, startDrag);
}

//you can use the same drag handler function, and get the item with event.currentTarget
function startDrag(event:MouseEvent):void {
    var typewriter:MovieClip = event.currentTarget as MovieClip; //gets the one that was clicked/mouse downed

    var target:MovieClip = getTarget(typewriter); //get the appropriate target

    if(target.hitTestObject(typewriter)){
        typewriter.stopDrag();
        //do the rest
    }
}

//get the appropriate target
function getTarget(typeWriter:MovieClip):MovieClip {
    for(var i:int=1;i<7;i++){
        if(typeWriter == this["typewriter_piece"+i]){
            return this["Target"+i];
        }
    }
}

答案 1 :(得分:0)

尝试删除myfunction功能。只要它是一个功能,它就可以用于整个程序。如果你拿出来它只适用于特定的框架。这样,一旦你搬出,程序就不会询问,检查或打扰这段代码。由于代码顶部的输入框架命令,它会在您进入框架后立即生效。让其他一切都是......很高兴看到解决方案有效!!! :p