2012-12-17 83 views
0

我是ActionScript新手,我正在嘗試創建我的第一個應用程序。我有一個主類 - Sprite的子類。和另一個班級,應該管理一些事件,如鍵盤按鍵。下面是它的初始化:無法創建類

private var controller:GameObjectController; 
... 
controller = new GameObjectController(this); 

下面是controller:GameObjectController本身:

public class GameObjectController extends InteractiveObject 
{ 
    var textField:TextField; 

    public function GameObjectController(mainSprite: Sprite) 
    { 
    addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 

    textField = new TextField(); 
    textField.text = "Hello, World"; 
    mainSprite.addChild(textField); 
    } 

    private function onKeyDown(event: KeyboardEvent):void 
    { 
    textField.text = event.keyCode.toString(); 
    } 

} 

,但是當我試圖運行此,我有一個錯誤:

ArgumentError: Error #2012: Class GameObjectController$ can not be created.

我在做什麼錯誤?

回答

3

從文檔:InteractiveObject

The InteractiveObject class itself does not include any APIs for rendering content onscreen. To create a custom subclass of the InteractiveObject class, extend one of the subclasses that do have APIs for rendering content onscreen, such as the Sprite, SimpleButton, TextField, or MovieClip classes.

所以我建議試圖繼承雪碧代替,看看是否能解決您的問題。

+0

打我把它與報價;) – 2012-12-17 06:16:05

1

首先,不要擴展InteractiveObject。由於API說:

The InteractiveObject class itself does not include any APIs for rendering content onscreen. To create a custom subclass of the InteractiveObject class, extend one of the subclasses that do have APIs for rendering content onscreen, such as the Sprite, SimpleButton, TextField, or MovieClip classes.

所以轉而使用(例如):

public class GameObjectController extends Sprite 
{ 
    .... 
} 

其次,構造函數的參數必須是一樣的擴展類 - 顯示類,這意味着零個參數。

+0

「參數的構造函數必須是一樣的擴展類」 - 這肯定是錯誤的,構造函數可以有任何參數,你只需要用它所期望的參數調用'super()',其他的都可以達到你的意願。 – user1875642

1

flash.display.InteractiveObject是一個抽象類,不能被子類化。相反,從flash.display.Sprite延伸。

不要忘了導入雪碧,並將你的代碼包:

package 
{ 
    import flash.display.Sprite; 

    public class GameObjectController extends Sprite 
    { 
     var textField:TextField; 

     public function GameObjectController(mainSprite: Sprite) 
     { 
      addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 

      textField = new TextField(); 
      textField.text = "Hello, World"; 
      mainSprite.addChild(textField); 
     } 

     private function onKeyDown(event: KeyboardEvent):void 
     { 
      textField.text = event.keyCode.toString(); 
     } 
    } 
}