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

时间:2013-12-09 07:28:34

标签: actionscript-3 flash-cs6

我正在创建一个应用程序,使用Loader类加载所有页面,而当我尝试加载时,我的metro.as加载正常,

  

'chords.swf'通过URLRequest(var chordReq:URLRequest)我得到了这个   错误:TypeError:错误#1009:无法访问的属性或方法   null对象引用。在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加载正常,而另一个不会加载。

chords.as看起来像这样......

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

1 个答案:

答案 0 :(得分:0)

将变量声明垂直居中,水平居中放在顶部并将这些行移到function initChords

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

希望它有所帮助。