2009-11-23 70 views
0

我想讓我的主類調整到瀏覽器窗口大小。我正在舞臺上聽Event.RESIZE,更新我的寬度/高度以匹配stageWidth/stageHeight,並繪製矩形以顯示它的大小。流體閃存佈局

當我調整大小時,它會在事件觸發的時候在大小之間閃爍。在這兩種情況下,寬度和高度都是正確的,但在「小」情況下,一切都在一個小框中。

Big Size http://files.seanhess.net/questions/browserresizebig.png

Small Size http://files.seanhess.net/questions/browserresizesmall.png

這裏是我的代碼

package 
{ 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.text.TextField; 

    public class main extends Sprite 
    { 
     public function main() 
     { 
      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 

      stage.addEventListener(Event.RESIZE, onResize); 

      var text:TextField = new TextField(); 
       text.text = "Some Text"; 

      addChild(text); 
     } 

     private function onResize(event:Event):void 
     { 
      this.width = stage.stageWidth; 
      this.height = stage.stageHeight; 

      trace("RESIZE " + width + " " + height); 

      this.graphics.clear(); 
      this.graphics.beginFill(0x000000, 0.5); 
      this.graphics.drawRect(0, 0, this.width, this.height); 
     } 
    } 
} 

什麼是做到這一點的正確方法?我究竟做錯了什麼?謝謝!

回答

3

問題是,寬度和高度的制定者是默認的實現只是一個單純的別名的scaleX和scaleY ...當你有一個寬度爲100的Sprite,並將其寬度設置爲200,而不是簡單地將其水平拉伸2倍......同時,默認getter只是簡單地返回屏幕上的有效寬度和高度,所以如果您繪製到精靈,它的寬度和高度都相應地更新...

這應該是完美的工作:

  private function onResize(event:Event):void 
      { 
        this.graphics.clear(); 
        this.graphics.beginFill(0x000000, 0.5); 
        this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); 
      } 
+0

這很有道理。我已經工作了很長時間(這裏是flex人)。寬度和高度真的很沒用,沒有一些額外的幫助。謝謝! – 2009-11-25 15:12:23

+0

我最終添加了一個變量來存儲explicitWidth和explicitHeight。然後在更改寬度和高度時更新該變量,並根據變量繪製圖形,而不是「實際」寬度/高度。 – 2009-11-25 15:13:41

1

看起來你的Sprite正在做正確的事情,但TextField是導致問題的原因。所以,如果你對更新的寬度和高度,以及它的工作原理:

package 
{ 
import flash.display.Sprite; 
import flash.display.StageAlign; 
import flash.display.StageScaleMode; 
import flash.events.Event; 
import flash.text.TextField; 

public class asProj extends Sprite 
{ 
    private var text:TextField; 

    public function asProj() 
    { 
    stage.align = StageAlign.TOP_LEFT; 
    stage.scaleMode = StageScaleMode.NO_SCALE; 

    stage.addEventListener(Event.RESIZE, onResize); 
    text = new TextField(); 
    text.text = "Some Text"; 

    addChild(text); 
    } 

    private function onResize(event:Event):void 
    { 
    this.width = stage.stageWidth; 
    this.height = stage.stageHeight; 

    text.width = this.width; 
    text.height = this.height; 

    trace("RESIZE " + this.width + " " + this.width); 

    this.graphics.clear(); 
    this.graphics.beginFill(0x000000, 0.5); 
    this.graphics.drawRect(0, 0, this.width, this.height); 
    } 
} 
} 
+0

太棒了...但它沒有任何意義。設置文本字段的寬度和高度使它保持小(這是我想要的,但它爲什麼會起作用?)。而且,這顯然是在做一些奇怪的事情。如果我快速縮小窗口,文本框會縮小一會兒,然後再變大。 – 2009-11-23 18:15:30

+0

好吧,TextField的大小和字體大小是兩個不同的東西。使用我的代碼,TextField正在改變大小,但字體大小保持不變。你可以看到我的意思,如果你給TextField一個背景顏色: text.background = true; text.backgroundColor = 0xff6666; 而且我認爲當你快速調整窗口大小時,你所看到的僅僅是調整大小和文本字段得到更新之間的延遲。 back2dos的解決方案是一個更好的主意。 – 99miles 2009-11-24 00:06:02