2011-11-30 83 views
2

我試圖讓一個簡單的AS3應用程序啓動並運行,並且出於某種原因,我無法獲得一個精靈來顯示。在這一點上,我想要做的就是獲得一個紅色的精靈來填補舞臺。AS3 Sprite添加到舞臺但不可見

public class Main extends Sprite 
{  
    public function Main():void 
    { 
     super(); 

     stage.scaleMode = StageScaleMode.NO_SCALE; 
     stage.align = StageAlign.TOP_LEFT; 

     var square:Sprite = new Sprite(); 

     square.width = stage.stageWidth; 
     square.height = stage.stageHeight; 
     square.x = square.width/2; 
     square.y = square.height/2; 

     square.graphics.clear(); 
     square.graphics.lineStyle(3, 0xFF0000); 
     square.graphics.beginFill(0xFF0000); 
     square.graphics.drawRect(0, 0, width, height); 
     square.graphics.endFill(); 

     square.addEventListener(Event.ADDED_TO_STAGE, addedToStage); 
     square.addEventListener(MouseEvent.CLICK, onClick); 

     addChild(square); 
    } 

    private function addedToStage(e:Event):void 
    { 
      trace("Added sprite to stage");   
    } 
    private function onClick(e:Event):void 
    { 
      trace("Got click on sprite");   
    }  
} 

跟蹤顯示精靈被添加到舞臺上,但不顯示任何內容,如果我點擊它,在函數的onClick永遠不會被調用。如果我使用TextField而不是Sprite,它顯示得很好。 Sprites一定有些奇怪的東西。

我在做什麼錯?

謝謝!

回答

2

試圖設置空的寬度和高度Sprite將返回0.看看here

and

請參閱Mr.Marty Wallace的回答。

+0

@Marty華萊士:現在? – Benny

+0

我其實剛剛得到你的意思,對不起。 '+ 1' – Marty

+0

你的鏈接項目解釋得最好。謝謝。 – Greg

2

你的問題就在這裏:

square.graphics.drawRect(0, 0, width, height); 

在此行中的爲widthheight0。原因是他們指的是你正在工作的類的widthheight屬性(Main - 從DisplayObject繼承的屬性),根據我所能看到的將爲零。

你基本上說:

square.graphics.drawRect(0, 0, 0, 0); 

這會使什麼。

+0

好的,但是當我改變drawRect(),所以它使用stage.stageWidth和stage.stageHeight,我仍然沒有看到任何東西。另外 - 看到上面幾行我設置的寬度和高度 - 不會正確設置寬度和高度? – Greg