2011-07-01 41 views
1

這裏是我的課至今:AS2:類不工作

class com.RCN.Menu.Menu extends MovieClip { 

    public var title:String; 
    public var menuItems:Array; 
    public var titleField:MovieClip; 

    public function Menu(title:String, menuItems:Array, menuActions:Array) { 
     this.titleField = createTitleBar(title); 
    } 

    public function createTitleBar(title:String) { 
     var titleBar = createEmptyMovieClip("titleBar", 1); 
      var titleText = createTextField("titleText", this.getNextHighestDepth(), 10, 10, 100, 100); 
      titleText.text = title; 
      titleText.autoSize = true; 
      titleText.multiline = false; 
      titleText.wordWrap = false; 
      titleText.border = true; 
      titleText.background = 0xffffff; 
      var format:TextFormat = new TextFormat(); 
      format.font = "Arial"; 
      format.size = 14.5; 
      format.bold = true; 
      format.align = "left"; 
      titleText.setTextFormat(format); 
      return titleBar; 
    } 
} 

然而,在我的SWF,當我運行下面的代碼我得到了一個未定義,任何人都可以看到,爲什麼? (新課程)

import com.RCN.Menu.Menu; 

var Accountability:Menu = new Menu("hello",[a,b,c,d]); 
trace(Accountability.titleField); 

感謝

回答

1

您對createEmptyMovieClip調用失敗,因爲沒有舞臺上創建一個影片剪輯。考慮如何做到這一點這個簡單的例子,沒有你的類連接到一個庫項目:

類:

class MyMenu extends MovieClip { 

    public var titleField:MovieClip; 

    public function MyMenu($target:MovieClip) { 
     this.titleField = createTitleBar($target); 
    } 

    public function createTitleBar($target:MovieClip):MovieClip { 
     var titleBar = $target.createEmptyMovieClip("titleBar", 1); 
     return titleBar; 
    } 
} 

IDE:

import MyMenu; 
var menu:MyMenu = new MyMenu(this); 
trace(menu.titleField);