左右移動和捲動顯示物件Flash Player 9 以及更新的版本,Adobe AIR 1.0 以及更新的版本 若有對所要顯示的區域來說太大的顯示物件,您可以使用 scrollRect 屬性,定義顯示物件的可見區域。此外,透過變更 scrollRect 屬性以回應使用者輸入,您可以讓內容向左右移動或向上下捲動。 scrollRect 屬性是 Rectangle 類別的實體,是組合定義矩形區域為單一物件所需值的類別。一開始若要定義顯示物件的可見區域,請建立新的 Rectangle 實體,並指定給顯示物件的 scrollRect 屬性。接著,若要上下捲動或左右移動,可將 scrollRect 屬性讀入另外一個 Rectangle 變數中,再變更所需的屬性 (例如,變更 Rectangle 實體的 x 屬性以左右移動,或變更 y 屬性以上下捲動),然後重新指定 Rectangle 實體至 scrollRect 屬性,以通知已變更值的顯示物件。 例如,下列程式碼會定義名為 bigText 之 TextField 物件的可見區域,此物件太大而無法置入 SWF 檔範圍之中。當按一下名為 up 和 down 的兩個按鈕時,就會透過修改 scrollRect Rectangle 實體的 y 屬性,呼叫讓 TextField 物件的內容上下捲動的函數。 import flash.events.MouseEvent;
import flash.geom.Rectangle;
// Define the initial viewable area of the TextField instance:
// left: 0, top: 0, width: TextField's width, height: 350 pixels.
bigText.scrollRect = new Rectangle(0, 0, bigText.width, 350);
// Cache the TextField as a bitmap to improve performance.
bigText.cacheAsBitmap = true;
// called when the "up" button is clicked
function scrollUp(event:MouseEvent):void
{
// Get access to the current scroll rectangle.
var rect:Rectangle = bigText.scrollRect;
// Decrease the y value of the rectangle by 20, effectively
// shifting the rectangle down by 20 pixels.
rect.y -= 20;
// Reassign the rectangle to the TextField to "apply" the change.
bigText.scrollRect = rect;
}
// called when the "down" button is clicked
function scrollDown(event:MouseEvent):void
{
// Get access to the current scroll rectangle.
var rect:Rectangle = bigText.scrollRect;
// Increase the y value of the rectangle by 20, effectively
// shifting the rectangle up by 20 pixels.
rect.y += 20;
// Reassign the rectangle to the TextField to "apply" the change.
bigText.scrollRect = rect;
}
up.addEventListener(MouseEvent.CLICK, scrollUp);
down.addEventListener(MouseEvent.CLICK, scrollDown);
如上述範例所示,當您使用顯示物件的 scrollRect 屬性時,最好是使用 cacheAsBitmap 屬性,指定 Flash Player 或 AIR 應該將顯示物件的內容快取為點陣圖。如此一來,Flash Player 和 AIR 就不必在每次捲動時重新繪製顯示物件的全部內容,而且能夠使用快取的點陣圖,直接在螢幕上呈現必要的部分。如需詳細資訊,請參閱快取顯示物件。 |
|