2013-10-12 81 views
0

我剛剛開始學習actionscript 3,下面是我的代碼,當我激活代碼時,它仍然報告錯誤在框架36中,但沒有找到哪條線路的錯誤。請幫助我調試它們.... TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。在flashmo_217_v_shape_fla :: MainTimeline/frame36()Flash ActionScript 3代碼錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法

以下是在frame36

stop(); 
fm_button.visible = false; 


import caurina.transitions.*; 

var menu_label_North:Array = new Array("Animals Collection", 
                       "Flowers Collection", 
                       "Leaves Collection", 
                       "Mixed Collection", 
                       "Extra Collection", 
                       "Special Awards", 
                       "Company Background", 
                       "Contact Information"); 


var total_north:Number = menu_label_North.length; 
var i_north:Number = 0; 
var page_north:Number; 
var main_menu_North:MovieClip = new MovieClip(); 
stage.addChild(main_menu_North); 


for (i_north = 0; i_north < total_north; i_north++) 
{ 
      var btn_north = new flashmo_button(); 
      btn_north.name = "btn" + i_north; 
      btn_north.x = fm_button.x + i_north * (fm_button.width + 12); 
      btn_north.y = fm_button.y; 
      btn_north.buttonMode = true; 
      btn_north.item_no = i_north; 
      btn_north.flashmo_click_area.addEventListener(Event.ENTER_FRAME, btn_enter_north); 


      var each_substring_north:Array = menu_label_North[i_north].split("|"); 
      btn_north.flashmo_button_label.fm_label.text = each_substring[0]; 
      btn_north.item_url = each_substring[1]; 
      main_menu_North.addChild(btn_north); 
} 


function btn_over_north(e:MouseEvent):void 
{ 
      e.target.parent.over = true; 
} 


function btn_out_north(e:MouseEvent):void 
{ 
      e.target.parent.over = false; 
} 


function btn_click_north(e:MouseEvent):void 
{ 
      var mc= e.target.parent; 
      if (mc.item_url != undefined) 
        navigateToURL(new URLRequest(mc.item_url), "_parent"); 
      else 
        change_page_north(mc.item_no); 
} 


function btn_enter_north(e:Event):void 
{ 
      var mc_north = e.target.parent; 
      if (mc_north.over == true) 
        mc_north.nextFrame(); 
      else 
        mc_north.prevFrame(); 
} 


function change_page_north(no:Number):void 
{ 
      for (var i:Number = 0; i < main_menu_North.numChildren; i++) 
      { 
        var mc = MovieClip(main_menu_North.getChildAt(i)); 
        mc.over = false; 
        mc.flashmo_click_area.visible = true; 
        mc.flashmo_click_area.addEventListener(MouseEvent.ROLL_OVER, btn_over_north); 
        mc.flashmo_click_area.addEventListener(MouseEvent.ROLL_OUT, btn_out_north); 
        mc.flashmo_click_area.addEventListener(MouseEvent.CLICK, btn_click_north); 
      } 
      var mc_selected = MovieClip(main_menu_North.getChildAt(no)); 
      mc_selected.over = true; 
      mc_selected.flashmo_click_area.visible = false; 
      mc_selected.flashmo_click_area.removeEventListener(MouseEvent.ROLL_OVER, btn_over_north); 
      mc_selected.flashmo_click_area.removeEventListener(MouseEvent.ROLL_OUT, btn_out_north); 
      mc_selected.flashmo_click_area.removeEventListener(MouseEvent.CLICK, btn_click_north); 

      page_north = no + 1; 
      play(); 
} 


change_page_north(0);// default page on load 


flashmo_credit.addEventListener(MouseEvent.CLICK, goto_fm_north); 


function goto_fm_north(e:MouseEvent):void 
{ 
      navigateToURL(new URLRequest("http://www.flashmo.com"), "_parent"); 
} 


music_credit.addEventListener(MouseEvent.CLICK, goto_music_north); 


function goto_music_north(e:MouseEvent):void 
{ 
      navigateToURL(new URLRequest(
      "http://www.premiumbeat.com/royalty_free_music/byPiece.php?id=2614"), 
      "_blank"); 
} 

回答

0

的代碼,我認爲你的錯誤可能來自var mc = MovieClip(main_menu_North.getChildAt(i));

孩子會應該是一個flashmo_button吧?如果您使用MovieClip(obj),則表示您要將obj轉換爲MovieClip。如果它已經是MovieClip(或MovieClip的後代),則應使用as MovieClip

E.g.

var mc:MovieClip = main_menu_North.getChildAt(i) as MovieClip 

事實上,你應該把它轉換爲DisplayObject類型,因爲在未來,你不能保證它永遠是一個MovieClip

我可以看到的潛在空對象的其他潛在區域是使用e.target.parent時。如果您試圖獲得點擊按鈕,則應使用e.currentTarget,請參閱here。測試在調用方法之前是否獲得了有效的對象。

if(e.currentTarget && e.currentTarget.parent) { 
    // now do something 
} 
相關問題