2012-01-03 58 views
0

所以我Main.as和Helpers.as的addChild錯誤時不Main.as

我做了一個函數誰增加了兒童階段,但我得到這個錯誤: 「Main.as 1180:呼叫可能未定義的方法addChildToStage」

所以這裏的代碼:

Main.as

package 
{ 

import flash.geom.*; 
import flash.events.*; 
import flash.display.*; 
import flash.media.*; 
import flash.text.*; 
import flash.net.URLRequest; 
import flash.utils.*; 
import flash.ui.Keyboard; 
import flash.events.KeyboardEvent; 

public class Main extends Sprite 
{ 

     private var bus:Bus = new Bus(); 

     public function Main() 
     { 

      Helpers.addChildToStage(bus, 176, 350, 0, 1) 

     } 

    } 


} 

Helpers.as

package 
{ 
import flash.geom.*; 
import flash.events.*; 
import flash.display.*; 
import flash.media.*; 
import flash.net.URLRequest; 
import flash.display.MovieClip; 
import flash.display.Sprite; 
import flash.utils.Timer; 
import flash.ui.Keyboard; 


public class Helpers extends Sprite 
{ 

    public function Helpers() 
    { 


    } 

    public static function addChildToStage(elem, stageX:int, stageY:int, depth:int, opacity:int) { 

     if (elem) 
     { 
      addChild(elem) 
      elem.x = stageX; 
      elem.y = stageY; 
      elem.alpha = opacity; 

      switch(depth) 
      { 
       case 0: 
        depth = 0; // sendToBack 
        break; 

       case 1: 
        depth = numChildren - 1; // bringToFront 
        break; 
       default: 
      } 

      setChildIndex(elem, depth); 
     } 
    } 

} 

回答

2
  1. Helpers.as你沒有爲你的package聲明關閉}

  2. addChildToStagestatic功能,使addChildnumChildrensetChildIndex不會在它裏面工作。

:)

閱讀靜態方法herehere

+0

1.只是輸入錯誤:)! 2.你有另一種方式嗎? – 2012-01-04 08:23:52

+0

因爲你打算添加到舞臺上,你可以通過參數'addChildToStage(stage,elem,...)'和'stage.addChild(elem)'來傳遞舞臺。 – Fabricio 2012-01-04 11:32:41