0

私のコードであなたの助けを求めたいです。コンパイルは問題ありませんが、以下のコードに到達すると、このエラーがスローされます。

エラー #1009: HiddenObjects_fla::MainTimeline/myFunction()[HiddenObjects_fla.MainTimeline::frame12:9] で null オブジェクト参照のプロパティまたはメソッドにアクセスできません

メイン タイムラインにフレーム 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();
}
};

}
4

2 に答える 2

2

あなたの問題はおそらくこれです:

addEventListener(Event.ENTER_FRAME,myFunction);

を呼び出してもstop()myFunctionアプリケーションのフレーム レートに従ってフレームごとに 1 回実行し続けます。したがって、フレーム レートが 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();

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];
        }
    }
}
于 2013-03-27T19:03:35.483 に答える
0

myfunction 関数を削除してみてください。機能である限り、プログラム全体で有効です。それを取り出すと、特定のフレームに対してのみライブになります。そうすれば、あなたが退去するとすぐに、プログラムはこのコードを尋ねたり、チェックしたり、気にしたりしなくなります。コードの上部にあるフレーム入力コマンドにより、フレームに入るとすぐにライブになります。他のすべてをそのままにしておきます...解決策が機能したことを確認できてうれしいです!!! :p

于 2013-03-27T18:43:03.330 に答える