簡體   English   中英

AS3-錯誤#1009:無法訪問空對象引用的屬性或方法

[英]AS3 -Error #1009: Cannot access a property or method of a null object reference

我正在創建一個應用程序,所有頁面都使用Loader類加載,而當我的metro.as加載正常時,當我嘗試加載時,

我通過URLRequest(var chordReq:URLRequest)的“ chords.swf”出現此錯誤:TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 在Chords()

package  {

import flash.display.MovieClip;
import com.greensock.TweenMax;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;



public class Main extends MovieClip {
    var chordsIcon:navBTN;
    var clockIcon:navBTN;
    var metroIcon:navBTN;

    var logo:logoMC;
    var _back:backBTN;

    var metroLoader:Loader;
    var metroReq:URLRequest;

    var chordLoader:Loader;
    var chordReq:URLRequest;



    public function Main() {
        // constructor code
        if(stage){
            initMain();
        }else{
             this.addEventListener(Event.ADDED_TO_STAGE, initMain);
        }

        function initMain(){
            chordsIcon = new navBTN('Chord Charts','Chords');
            metroIcon = new navBTN('Metronome','Metronome');
            clockIcon = new navBTN('Session Timer','Clock');
            logo = new logoMC;
            _back = new backBTN;

            chordLoader = new Loader();
            chordReq = new URLRequest('chords.swf');



            metroLoader = new Loader();
            metroReq = new URLRequest('metro.swf');

            addChild(chordLoader);
            chordLoader.x = 0;
            chordLoader.y = 0;



            addChild(metroLoader);
            metroLoader.x = 320;
            metroLoader.y = 0;
            metroLoader.load(metroReq);

            initInterface();

        }//end initMain

    function initInterface(){

        addChild(logo);
        logo.x = 160;
        logo.y = stage.stageHeight - (logo.height / 2);

        addChild(chordsIcon);
        chordsIcon.x = stage.stageWidth / 2;
        chordsIcon.y = 234;


        addChild(clockIcon);
        clockIcon.x = 90;
        clockIcon.y = 350;


        addChild(metroIcon);
        metroIcon.x = 230;
        metroIcon.y = 350;

        chordsIcon.addEventListener(MouseEvent.CLICK, onClick);
        clockIcon.addEventListener(MouseEvent.CLICK, onClick);
        metroIcon.addEventListener(MouseEvent.CLICK, onClick);





    }
    function removeInterface(){
        removeChild(logo);
        removeChild(clockIcon);
        removeChild(chordsIcon);
        removeChild(metroIcon);
    }

    function onClick(e:MouseEvent){
            removeInterface();

            addChild(_back);
            _back.x = 30;
            _back.y = 22;
            _back.addEventListener(MouseEvent.CLICK, onBackClick);


                if(e.target == chordsIcon.hitSpot){
                    trace('Chords Clicked');

                    chordLoader.load(chordReq);

                }
                else if(e.target == metroIcon.hitSpot){
                    trace('Metronome Clicked');
                    metroLoader.x = 0;

                } 
                else if(e.target == clockIcon.hitSpot){
                    trace('Clock Clicked');

                }




        }

        function onBackClick(e:MouseEvent){
            metroLoader.x = 320;
            initInterface();
        }
    }//end public function
}

}

不知道為什么這不能正常工作,因為metroLoader可以很好地加載,而另一個則無法運行。

看起來像這樣...

package  {

import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import com.greensock.easing.*;


public class Chords extends MovieClip {
    //declare vars


    var cMaj:MovieClip  = new cMaj_MC;
    var cMin:MovieClip  = new cMin_MC;
    var c7:MovieClip    = new c7_MC;
    var dMaj:MovieClip  = new dMaj_MC;
    var dMin:MovieClip  = new dMin_MC;
    var d7:MovieClip    = new d7_MC;
    var eMaj:MovieClip  = new eMaj_MC;
    var eMin:MovieClip  = new eMin_MC;
    var e7:MovieClip    = new e7_MC;
    var fMaj:MovieClip  = new fMaj_MC;
    var fMin:MovieClip  = new fMin_MC;
    var f7 :MovieClip   = new f7_MC;

    var slideRight:rightBTN;
    var slideLeft:leftBTN;

    var chordList:Array = [cMaj, cMin, c7, dMaj, dMin, d7, eMaj, eMin, e7, fMaj, fMin, f7];
    var currentMC = chordList[i];
    var i:int = 0;


    var verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
    var horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);

    public function Chords() {
        // constructor code
        if(stage){
            initChords();
        }else{
             this.addEventListener(Event.ADDED_TO_STAGE, initChords);
        }


        function initChords() {
            slideRight = new rightBTN;
            slideLeft = new leftBTN;

            addChild(slideRight);
            slideRight.x = stage.stageWidth - (slideRight.width);

            addChild(slideLeft);
            slideLeft.x = 0;

            slideRight.addEventListener(MouseEvent.CLICK, onRight);
            slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
            slideRight.alpha = .5;
            slideLeft.alpha = .5;

            addChild(currentMC);
            currentMC.x = horizontalCenter;
            currentMC.y = verticalCenter;

        }//end initChords


        function onRight(e:MouseEvent){
                trace(i);
                trace(currentMC);



                if(i == 11) {
                    slideRight.removeEventListener(MouseEvent.CLICK, onRight);

                }else{
                    slideRight.addEventListener(MouseEvent.CLICK, onRight);
                    TweenMax.to(slideRight, .05, {alpha:1, yoyo:true, repeat:1});
                    TweenMax.to(currentMC, .25, {x:-320, alpha:0, onComplete:nextChord});
                }


        }
        function nextChord(){

            removeChild(currentMC);

            i++;

            addChild(currentMC);
            currentMC.x = 320;
            currentMC.y = verticalCenter;
            currentMC.alpha = 0;


            TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});

        }
        function lastChord(){

            removeChild(currentMC);

            i--;

            addChild(currentMC);
            currentMC.x = -320;
            currentMC.y = verticalCenter;
            currentMC.alpha = 0;

            TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});

        }

        function onLeft(e:MouseEvent){

            if(i > 0) {
                slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
                TweenMax.to(slideLeft, .05, {alpha:1, yoyo:true, repeat:1});
                TweenMax.to(currentMC, .25, {x:320, alpha:0, onComplete:lastChord});
            }

        }

        }//end public function


}//end public class

}//end package

在頂部聲明變量verticalCenter,horizo​​ntalCenter並將這些行移入function initChords

verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM