2010-12-09 46 views
1

我有一個煩人的問題。我搜索了一下,找不到任何好的答案,所以我希望能在這裏找到一些幫助。「上傳」不是函數

我得到這個錯誤,這似乎是相當普遍:definently

TypeError: Error #1006: update är inte en funktion. // = update is not a function (english) 
at se.qmd.spaceInvaders::ShipHandler/onEnterFrame() 

更新是一個函數,所以我真的不明白是什麼問題。

我的設置。我正在使用Flashdevelop。我有一個類,它被設置爲我的fla文件中的文檔類。 我在se.qmd.spaceInvadersse.qmd.starLight我在SRC文件夾主類,和我的子類的封裝結構。我還使用TweenMax並將其保存在src文件夾的「正常」包裝結構中。

在主I類添加到舞臺的實例類LeGun的 - 這也是在FLA文件的「輸出」影片剪輯,該類StarHandler的實例和類ShipHandler的一個實例。我還有一個鍵盤監聽器,它在enterframe函數的幫助下移動我的LeGun類的實例。 好,沒問題。 StarHandler的工作方式應該是這樣,ShipHandler的工作方式應該如此,直到我在Ship類中調用1或2種方法 - 這也是fla文件中的「導出」動畫片段。 Ship類當然是包含由ShipHandler處理的Ship的類。

我在下面包括我的ShipHandler和Ship類,並希望它不會讓這個帖子超過一定的長度。 ShipHandler.as:

package se.qmd.spaceInvaders { 

import flash.display.MovieClip; 
import se.qmd.spaceInvaders.Ship; 
import flash.display.Stage; 
import flash.events.Event; 

public class ShipHandler extends MovieClip { 

    private var _shipArray:Array = []; 
    private const NUM_START_SHIPS:int = 25; 
    private var s:Stage = null; 

    private var _directionX:Number = 1; 
    private var _directionY:Number = 1; 
    private var _velocityX:Number; 
    private var _velocityY:Number; 

    private const YBORDER_TOP:int = 50; 
    private const YBORDER_BOTTOM:int = 525; 

    public function ShipHandler(o:Stage) { 

     s = o; // tar emot stage från main 
     createObjects(); 

     this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
    } 

    private function createObjects():void { 

     for (var i:int = 0; i < NUM_START_SHIPS; i++) { 

      var aShip:Ship = new Ship(); 
      aShip.x = Math.random() * (s.stageWidth - aShip.width) + aShip.width/2; 
      aShip.y = Math.random() * (s.stageHeight - aShip.height -135) + aShip.height/2 + 55; 
      this.addChild(aShip); 
      _shipArray.push(aShip); 
      aShip.startMove(Math.random(), Math.random()); 
     }   
    } 

    private function onEnterFrame(e:Event):void { 

     for (var i:int = 0; i < _shipArray.length; i++) { 

      var aShip:Ship = _shipArray[i] as Ship; 
      aShip.update(); 
     } 
    } 
} 
} 

Ship.as:

package se.qmd.spaceInvaders { 

import flash.display.MovieClip; 
import flash.events.Event; 

public class Ship extends MovieClip { 

    private var _directionX:Number = 1; 
    private var _directionY:Number = 1; 
    private var _velocityX:Number; 
    private var _velocityY:Number; 

    private const YBORDER_TOP:int = 50; 
    private const YBORDER_BOTTOM:int = 525; 

    public function Ship() { 

     //s = o; // tar emot stage 
     this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
    } 

    private function onAddedToStage(e:Event):void { 

     removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 

     this.width = 50; 
     this.height = 18;   
    } 

    public function update():void { 

     // x led vänster kant 
     if(this.x <= this.width/2) { 

      this.x = this.width/2; 
      _directionX *= -1; 
     } 

     // x led höger kant 
     if(this.x >= 800 - this.width/2) { 

      this.x = 800 - this.width/2; 
      _directionX *= -1; 
     } 

     // y led top kant 
     if(this.y <= YBORDER_TOP + this.height/2) { 

      this.y = YBORDER_TOP + this.height/2; 
      _directionY *= -1; 
     } 

     // y led botten kant 
     if(this.y >= YBORDER_BOTTOM - this.height/2) { 

      this.y = YBORDER_BOTTOM - this.height/2; 
      _directionY *= -1; 
     } 

     this.x += _velocityX * _directionX; 
     this.y += _velocityY * _directionY; 
    } 

    public function startMove(directionX:Number, directionY:Number):void { 

     trace(directionX, directionY); 
     _velocityX = 10; 
     _velocityY = 10; 

     if (directionX < 0.5) { 

      _directionX = -1; 
     } 

     if (directionY < 0.5) { 

      _directionY = -1; 
     } 
    } 

    public function changeDirection():void { 

     _directionX *= -1; 
     _directionY *= -1; 
    } 
} 
} 

,只要我調用其中的方法startMove或更新從ShipHandler,我得到我上面寫的錯誤。 將不勝感激任何幫助,因爲在我看來,我必須做一些根本性的錯誤...

回答

2

這聽起來對我來說就像是Ship只是一個MovieClip(它是一個動態類,因此當您嘗試訪問編譯時不存在的方法時,編譯器不會抱怨。)動畫片段沒有update()方法,因此在嘗試調用它時,無法在Ship實例上找到它。

您FLA文件中的MovieClip是否真的導出爲se.qmd.spaceInvaders.Ship(而不僅僅是Ship)?如果它只是Ship,那就可以解釋爲什麼你的書寫類沒有被指定爲你的船舶圖形的類,而只是一個簡單的MovieClip來解釋這個錯誤。

0

我試過你的原代碼,它對我來說工作正常。我不需要改變任何東西。

您是否正在從Flash IDE進行編譯?在這種情況下,嘗試清除緩存與控制>刪除ASO文件這樣的:

alt text


嘗試鑄造的船,而不是像這樣:

private function onEnterFrame(e:Event):void { 

    for (var i:int = 0; i < _shipArray.length; i++) { 

      var aShip:Ship = Ship(_shipArray[i]); 
      if(aShip) 
       aShip.update(); 
      else 
       throw Error("ship is null at position " + i) 
    } 
} 

或者使用向量代替陣列:

var _shipArray : Vector.<Ship> = new Vector.<Ship>(); 
+0

獲取相同的錯誤,如果我嘗試你的第一個例子:( 不知道如何使用矢量,但我在我的StarHandler類以非常類似的方式使用數組,並且它在那裏工作.. 。 – Poppe76 2010-12-09 13:19:53

+0

當你使用我的代碼時,你會得到同樣的錯誤,還是你得到自定義錯誤(ship位置爲空)? – Mattias 2010-12-09 14:02:42