2014-02-23 52 views
0

這有點難以解釋,但我會嘗試。Actionscript 3:錯誤#1009:無法訪問空對象引用的屬性或方法。 (父母)

在我的Main()類中,假設我創建了主動畫片段(bg_image),並且還創建了一些創建另一個類的實例的行。 (看下面的代碼)

var route = Route(Airport.return_Route); 

這條路線的實例,然後意要動態地添加精靈兒童的從主()類的主要背景。

我試圖將它與以下行: new_flight = new Flight(coordinates) Main.bg_image.addChild(new_flight);

這似乎工作得很好。讓我們將焦點切換到new_flight實例。在Flight類中,這條線trace(this.parent)會返回「[object Image]」,我認爲這是成功的,因爲我的意圖是將new_flight作爲孩子添加到bg_image中,我認爲這是一個「對象圖像」。

但是,嘗試刪除「new_flight」 - 實例時會出現此問題。顯然,這樣做的理想方法是通過帶有removeChild的bg_image。

但是,當我的腳本到達這一行時:Main.bg_image.removeChild(this),我的腳本停止並返回ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.,在該行。我也嘗試用this.parent.removeChild(this)代替Main.bg_image.removeChild(this),但沒有運氣。

我認爲解決方案可能是使用某種「EventDispatching」 - 方法。但我還沒有完全理解這個概念呢......

代碼如下。請幫忙。我不知道如何使這項工作...

主類:

package 
{ 
import flash.display.MovieClip; 
import flash.utils.Dictionary; 
import flash.events.MouseEvent; 
import flash.display.Sprite; 
import flash.text.TextField; 
import fl.controls.Button; 
import flash.display.DisplayObject; 

import Airport; 

public class Main extends Sprite 
{  
    // ------------------------ 
    // Buttons 

    public var create_new_route; 
    public var confirm_new_route; 

    // images 
    public static var bg_image:MovieClip; 
    public var airport:MovieClip; 
    //------------------------------- 

    public var routeArray:Array; 

      public static var airportDict:Dictionary = new Dictionary(); 
    public static var collectedAirportArray:Array = new Array(); 


    public function Main() 
    { 
     addEventListener(Event.ADDED_TO_STAGE, init); 

     create_new_route = new Button(); 
     create_new_route.label = "Create new route"; 
     create_new_route.x = 220; 
     create_new_route.y = 580; 
     this.addChild(create_new_route) 
     create_new_route.addEventListener(MouseEvent.CLICK, new_route) 


     confirm_new_route = new Button(); 
     confirm_new_route.label = "Confirm route"; 
     confirm_new_route.x = 220; 
     confirm_new_route.y = 610; 
     this.addChild(confirm_new_route) 
     confirm_new_route.addEventListener(MouseEvent.CLICK, confirm_route) 
    } 

    public function init(e:Event):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 

     bg_image = new Image(); 
     addChild(bg_image); 

     S_USA["x"] = 180.7; 
     S_USA["y"] = 149.9; 
     S_USA["bynavn"] = "New York"; 

     S_Norway["x"] = 423.7; 
     S_Norway["y"] = 76.4; 
     S_Norway["bynavn"] = "Oslo"; 

     S_South_Africa["x"] = -26; 
     S_South_Africa["y"] = 146; 
     S_South_Africa["bynavn"] = "Cape Town"; 

     S_Brazil["x"] = 226; 
     S_Brazil["y"] = 431.95; 
     S_Brazil["bynavn"] = "Rio de Janeiro"; 

     S_France["x"] = 459.1; 
     S_France["y"] = 403.9; 
     S_France["bynavn"] = "Paris"; 

     S_China["x"] = 716.2; 
     S_China["y"] = 143.3; 
     S_China["bynavn"] = "Beijing"; 

     S_Australia["x"] = 809.35; 
     S_Australia["y"] = 414.95; 
     S_Australia["bynavn"] = "Sydney"; 

     // ---------------------------------------------------- 

     airportDict["USA"] = S_USA; 
     airportDict["Norway"] = S_Norway; 
     airportDict["South Africa"] = S_South_Africa; 
     airportDict["Brazil"] = S_Brazil; 
     airportDict["France"] = S_France; 
     airportDict["China"] = S_China; 
     airportDict["Australia"] = S_Australia; 

     for (var k:Object in airportDict) 
     { 
      var value = airportDict[k]; 
      var key = k; 
      startList.addItem({label:key, data:key}); 
      sluttList.addItem({label:key, data:key}); 
      var airport:Airport = new Airport(key,airportDict[key]["bynavn"]); 
      airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]); 
      collectedAirportArray.push(airport); 
      airport.scaleY = minScale; 
      airport.scaleX = minScale; 
      bg_image.addChild(airport); 
     } 

     // -------------------------------------------- 
     // -------------------------------------------- 
     // ----------------------------------------------- 


    } 


    private function new_route(evt:MouseEvent):void 
    { 
     Airport.ROUTING = true; 
    } 

    public function confirm_route(evt:MouseEvent):void 
    { 
     Airport.ROUTING = false; 
     trace(Airport.return_ROUTE); 
     var route = new Route(Airport.return_ROUTE); // *** 
     this.addChild(route); // ** 
     Airport.return_ROUTE = new Array(); 
    } 

} 
} 

機場類:

package 
{ 
import flash.display.MovieClip; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.display.SimpleButton; 
import flash.display.Stage; 
import flash.text.TextField; 
import flash.text.TextFormat; 
import flashx.textLayout.formats.Float; 

public class Airport extends MovieClip 
{ 

    public static var ROUTING = false; 
    public static var return_ROUTE:Array = new Array(); 
    //----------------------------------------------------------------------------- 

    // ---------------------------------------------------------------------------- 

    protected var navn:String; 
    protected var bynavn:String; 

    // ---------------------------------------------------------------------------- 

    public function Airport(navninput, bynavninput) 
    { 
     this.bynavn = bynavninput; 
     this.navn = navninput; 

     this.addEventListener(MouseEvent.CLICK, clickHandler); 
     this.addEventListener(MouseEvent.MOUSE_OVER, hoverHandler); 
    } 

    public function zoomHandler():void 
    { 
     trace("testing complete"); 
    } 

    public function koordinater(xc, yc) 
    { 
     this.x = (xc); 
     this.y = (yc); 
    } 

    private function clickHandler(evt:MouseEvent):void 
    { 
     trace(ROUTING) 

     if (ROUTING == true) 
     { 
      return_ROUTE.push([this.x, this.y]) 
     } 
    } 

    private function hoverHandler(evt:MouseEvent):void 
    { 
     if (ROUTING == true) 
     { 
      this.alpha = 60; 
      this.width = 2*this.width; 
      this.height = 2*this.height; 
      this.addEventListener(MouseEvent.MOUSE_OUT, awayHandler); 
     } 
    } 

    private function awayHandler(evt:MouseEvent):void 
    { 
      this.width = 13; 
      this.height = 13; 
    } 




} 
} 

Route類

package 
{ 

import flash.events.TimerEvent; 
import flash.utils.Timer; 
import flash.display.MovieClip; 
import Main; 

public class Route 
{ 
    public var income:Number; 
    public var routePoints:Array; 
    private var routeTimer:Timer; 
    private var new_flight:Flight; 

    public function Route(route_array:Array) 
    { 
     this.routePoints = route_array 
     routeTimer = new Timer(2000);// 2 second 
     routeTimer.addEventListener(TimerEvent.TIMER, route_function); 
     routeTimer.start(); 
    } 

    private function route_function(event:TimerEvent):void 
    { 
     for (var counter:uint = 0; counter < routePoints.length - 1; counter ++) 
     { 
      trace("Coords: ", routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]); 
      new_flight = new Flight(routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]); 
      Main.bg_image.addChild(new_flight); 

      var checkTimer:Timer = new Timer(15);// 1 second 
      checkTimer.addEventListener(TimerEvent.TIMER, check_function); 
      checkTimer.start(); 

      function check_function(event:TimerEvent):void 
      { 
       if (new_flight.finished = true) 
       { 
        checkTimer.stop(); 
       } 
      } 
     } 
    } 

} 

} 

飛行類

package 
{ 
import flash.display.MovieClip; 
import flash.display.Sprite; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.display.SimpleButton; 
import flash.display.Stage; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 
import fl.controls.Button; 
import flash.display.DisplayObject; 

public class Flight extends MovieClip 

{ 
    public static var speed:uint = 1 
    public var finished:Boolean = false; 

    protected var absvector:Number; 
    protected var vector:Array; 
    protected var myTimer:Timer; 
    //protected var parentContainer:MovieClip; 

    protected var utgangspunkt_x; 
    protected var utgangspunkt_y; 
    protected var destinasjon_x; 
    protected var destinasjon_y; 

    protected var vector_x; 
    protected var vector_y; 

    public function Flight(utgangspunkt_x, utgangspunkt_y, destinasjon_x, destinasjon_y):void 
    { 
     addEventListener(Event.ADDED_TO_STAGE, init);   
     this.utgangspunkt_x = utgangspunkt_x; 
     this.utgangspunkt_y = utgangspunkt_y; 

     this.x = utgangspunkt_x; 
     this.y = utgangspunkt_y;  

     this.destinasjon_x = destinasjon_x + 10; 
     this.destinasjon_y = destinasjon_y + 10; 

     this.vector_x = Math.abs(this.destinasjon_x-this.utgangspunkt_x); 
     this.vector_y = Math.abs(this.destinasjon_y-this.utgangspunkt_y); 

     this.height = 20; 
     this.width = 20; 
    } 

    public function init(evt:Event):void 
    { 
     trace(this.parent) 
     if (utgangspunkt_x < destinasjon_x) 
     { 
      this.rotation = -(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180; 
     } 
     else 
     { 
      this.rotation = 180-(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180; 
     } 

     absvector = Math.sqrt(Math.pow((destinasjon_x - utgangspunkt_x),2) + Math.pow((utgangspunkt_y - destinasjon_y),2)); 
     vector = [(destinasjon_x - utgangspunkt_x)/absvector,(utgangspunkt_y - destinasjon_y)/absvector]; 

     stage.addEventListener(Event.ENTER_FRAME, movement) 
    } 

    private function movement(evt:Event):void 
    { 
     if (this.vector_x > this.vector_y) 
     { 
      if (destinasjon_x>utgangspunkt_x) 
      { 
       if (this.x < destinasjon_x) 
       { 
        this.x += speed*vector[0]; 
        this.y -= speed*vector[1]; 
       } 
       else 
       { 
        finished = true 
        Main.bg_image.removeChild(this) 
       } 
      } 
      else if (destinasjon_x<utgangspunkt_x) 
      { 
       if (this.x > destinasjon_x) 
       { 
        this.x += speed*vector[0]; 
        this.y -= speed*vector[1]; 
       } 
       else 
       { 
        finished = true 
        Main.bg_image.removeChild(this) 
       } 
      } 
     } 
     else 
     { 
      if (destinasjon_y>utgangspunkt_y) 
      { 
       if (this.y < destinasjon_y) 
       { 
        this.x += speed*vector[0]; 
        this.y -= speed*vector[1]; 
       } 
       else 
       { 
        finished = true 
        Main.bg_image.removeChild(this) 
       } 
      } 
      else if (destinasjon_y<utgangspunkt_y) 
      { 
       if (this.y > destinasjon_y) 
       { 
        this.x += speed*vector[0]; 
        this.y -= speed*vector[1]; 
       } 
       else 
       { 
        finished = true 
        Main.bg_image.removeChild(this) 
       } 
      } 
     } 
    } 
} 
} 
+0

你正在得到什麼錯誤? –

+0

直到我嘗試通過它的父級(removeChild)刪除new_flight時,我纔會收到錯誤,它應該是bg_image。我試圖追蹤其父母的價值,並顯示爲「空」。 – user3257755

+0

你確定'new_flight'實例被創建? –

回答

0

我很困惑什麼你問什麼你試圖做的,但我會給我最好的拍攝。

this.addchild(new_class2) 

此行將您的對象添加到顯示列表中。將另一個對象添加到顯示中的操作與添加第一個對象的方式相同。 Flash按順序添加對象,所以您需要在後面的對象需要首先聲明和添加。

這可能是你想要什麼:

var new_flight:Flight = new Flight(); 
this.addchild(new_flight); 

而且您忘記了Class2中的類型聲明:

var new_class2:Class2 = new Class2(); 
+0

不,我不想將new_flight添加到class2實例。 – user3257755

0

嘗試更換:

Main.bg_image.addChild(new_flight); 

Main(this.parent).bg_image.addChild(new_flight); 

...假設你是指「主」類被命名爲「主」和逸岸它有一個命名實例名爲「bg_image」

請發表評論,如果它的作品,我可以更詳細地解釋一下實際上發生在這裏。

+0

這給了我錯誤:1119:通過引用與靜態類型Class2 – user3257755

+0

@ user3257755訪問可能未定義的屬性父代,可以儘可能多地爲您的原始問題添加代碼,包括Main類,Class2等。My猜測是在將實際項目添加到舞臺之前,您正試圖訪問'.parent'對象。 –

+0

我現在添加了代碼。可能有一些「垃圾」代碼用於我忘記刪除的測試目的。 – user3257755

0

爲什麼要將new_flight添加到bg_image

無論如何,如果要添加new_flightbg_image,那麼你就bg_image已經被聲明爲公共靜態(我不推薦),

嘗試在你的Flight類刪除飛行它自身像這樣,

Main.bg_image.removeChild(this) // *

您還可以使用Event Dispatching,而不是宣佈bg_imagestatic

如果你想刪除航班,然後從Flight類派遣事件到路線類,那裏你再次派遣事件到主類。

而且,在主要類中捕獲此事件並訪問MovieClip

+0

當然:)我有一些問題,由於連接不好。您能否詳細闡述關於將bg_image聲明爲靜態的問題? – user3257755

+0

編輯:這不起作用;我在錯誤的目錄下編輯了一個類文件..對不起 – user3257755

+0

你是否改變過'package'結構?我假設你有在同一個目錄中的所有類。如果它位於不同的目錄中,則在'Flight'類w.r.t目錄結構中添加import語句。 –

相關問題