org.as3s.Document
ドキュメントクラス用の汎用クラス
Download: http://as3s.org/uploads/document/document.zip
AS3では、Stageについての仕様が変更され、
Stage.scaleMode = "noScale";
のように静的クラスに直接アクセスすることはできなくなりました。
Stageのプロパティやメソッドにアクセスするには、
mc.stage.scaleMode = "noScale";
のように、ステージ上に配置されたDisplayObject(MovieClipやSpriteなど)のstageプロパティからアクセスする必要があります。
ただし、stageが参照できるのはあくまでステージ上に配置されている時のみで、そのDisplayObjectがStage上にaddChildされる前、またはremoveChildされた後では、stageプロパティはnullとなってしまいます。
タイミングを気にせずにStageを参照する方法としては、はじめからステージ上に配置されているドキュメントクラスのstageプロパティを参照する方法が考えられますが、この方法でも別のSWFファイルに外部ファイルとしてロードされた場合に、ロードが完了するまでstageプロパティがnullになるという問題があり、汎用的とは言えません。
これらの問題に対処するために、ドキュメントクラス用の抽象クラスを作成しました。
ドキュメントクラスにこのDocumentクラスを継承させるだけで、ドキュメントクラスのコンストラクタを含むどこからでも、
Document.scaleMode = StageScaleMode.NO_SCALE;
Document.align = StageAlign.TOP_LEFT;
Document.addEventListener(Event.RESIZE, onResize);
Document.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboadDown);
Document.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
のように、Stageプロパティを設定・取得したり、Stageイベントを受け取ることができます。
基本的な仕組みとしては、stageプロパティに橋渡しをするだけですが、addToStageされる前(stageがnullの間)に設定されたプロパティやイベントリスナーを一時的にスタックしておき、addToStageされたタイミングでスタックされたプロパティやイベントリスナーを設定します。
スタックが発生するのは外部ファイルとしてロードされた時だけだと思われますが、そのような場合も含めて、コンストラクタだろうとタイミングを気にせずに使えます。
Stageイベントだけでなく、KeyイベントやMouseイベントもAS3では静的クラスにはアクセスできないので有効に使える場面は多いのではないかと思います。
尚、stage自体を参照したい場合はDocument.stageで参照できます。(ロード元SWFにもこのDocumentクラスを利用していれば、ロードされるSWFのドキュメントクラスのコンストラクタでもDocument.stageはnullになりません。)
org.as3s.Document
* Document
* Abstract Class of Document Class for Fla File
*
* Download: http://as3s.org/document/document.zip
*
* Copyright (c) 2007 Hisato Ogata
* Licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*
* @langversion ActionScript 3.0
* @playerversion Flash 9
*
* @version 1.02
* @author Hisato Ogata
* @see http://as3s.org/document/
*
* @example usage:
* <listing version="3.0">
*
* // Your document class must extend this Document Class
* public class MyDocument extends Document {
* // ...
* }
*
* // You can receive Stage events or get/set Stage properties whenever you want
* Document.scaleMode = StageScaleMode.NO_SCALE;
* Document.align = StageAlign.TOP_LEFT;
* Document.addEventListener(Event.RESIZE, onResize);
* Document.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboadDown);
* Document.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
*
* </listing>
**/
package org.as3s {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Stage;
import flash.display.StageQuality;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
public class Document extends Sprite {
private static var addStack:Array = new Array();
private static var removeStack:Array = new Array();
/**
* A reference of the Stage
*/
private static var _stage:Stage;
public static function get stage():Stage {
return _stage;
}
/**
* A value from the StageAlign class that specifies the alignment of the stage in Flash Player or the browser
*/
private static var _align:String;
public static function set align(value:String):void {
_align = value;
if (_stage != null) _stage.align = _align;
}
public static function get align():String {
return _stage != null ? _stage.align : _align;
}
/**
* Gets and sets the frame rate of the stage
*/
private static var _frameRate:Number;
public static function set frameRate(value:Number):void {
_frameRate = value;
if (_stage != null) _stage.frameRate = _frameRate;
}
public static function get frameRate():Number {
return _stage != null ? _stage.frameRate : _frameRate;
}
/**
* A value from the StageQuality class that specifies which rendering quality Flash Player uses
*/
private static var _quality:String;
public static function set quality(value:String):void {
_quality = value;
if (_stage != null) _stage.quality = _quality;
}
public static function get quality():String {
return _stage != null ? _stage.quality : _quality;
}
/**
* A value from the StageDisplayState class that specifies which display state to use
*/
private static var _displayState:String;
public static function set displayState(value:String):void {
_displayState = value;
if (_stage != null) _stage.displayState = _displayState;
}
public static function get displayState():String {
return _stage != null ? _stage.displayState : _displayState;
}
/**
* A value from the StageScaleMode class that specifies which scale mode to use
*/
private static var _scaleMode:String;
public static function set scaleMode(value:String):void {
_scaleMode = value;
if (_stage != null) _stage.scaleMode = _scaleMode;
}
public static function get scaleMode():String {
return _stage != null ? _stage.scaleMode : _scaleMode;
}
/**
* Specifies whether to show or hide the default items in the Flash Player context menu
*/
private static var _showDefaultContextMenu:Boolean;
public static function set showDefaultContextMenu(value:Boolean):void {
_showDefaultContextMenu = value;
if (_stage != null) _stage.showDefaultContextMenu = _showDefaultContextMenu;
}
public static function get showDefaultContextMenu():Boolean {
return _stage != null ? _stage.showDefaultContextMenu : _showDefaultContextMenu;
}
/**
* Specifies whether or not objects display a glowing border when they have focus
*/
private static var _stageFocusRect:Boolean;
public static function set stageFocusRect(value:Boolean):void {
_stageFocusRect = value;
if (_stage != null) _stage.stageFocusRect = _stageFocusRect;
}
public static function get stageFocusRect():Boolean {
return _stage != null ? _stage.stageFocusRect : _stageFocusRect;
}
/**
* The current height, in pixels, of the Stage
*/
public static function get stageHeight():int {
return _stage != null ? _stage.stageHeight : 0;
}
/**
* The current width, in pixels, of the Stage
*/
public static function get stageWidth():int {
return _stage != null ? _stage.stageWidth : 0;
}
/**
* Constructor for Document class
*/
public function Document() {
if (_stage == null && this.stage != null) {
_stage = this.stage;
}
if (_stage != null) {
_align = _stage.align;
_frameRate = _stage.frameRate;
_quality = _stage.quality;
_displayState = _stage.displayState;
_scaleMode = _stage.scaleMode;
_showDefaultContextMenu = _stage.showDefaultContextMenu;
_stageFocusRect = _stage.stageFocusRect;
}
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
/**
* Add and remove the event stacks
*/
private function onAddedToStage(event:Event):void {
if (_stage == null) _stage = this.stage;
if (_align!=_stage.align) _stage.align = _align;
if (_frameRate!=_stage.frameRate) _stage.frameRate = _frameRate;
if (_quality!=_stage.quality) _stage.quality = _quality;
if (_displayState!=_stage.displayState) _stage.displayState = _displayState;
if (_scaleMode!=_stage.scaleMode) _stage.scaleMode = _scaleMode;
if (_showDefaultContextMenu!=_stage.showDefaultContextMenu) _stage.showDefaultContextMenu = _showDefaultContextMenu;
if (_stageFocusRect!=_stage.stageFocusRect) _stage.stageFocusRect = _stageFocusRect;
var o:Object;
while (addStack.length) {
o = addStack.shift();
_stage.addEventListener(o.type, o.listener, o.useCapture, o.priority, o.useWeakReference);
}
while (removeStack.length) {
o = removeStack.shift();
_stage.removeEventListener(o.type, o.listener, o.useCapture);
}
}
/**
* Registers an event listener object
*
* @param type
* @param listener
* @param useCapture
* @param priority
* @param useWeakReference
*
*/
static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
if (_stage != null) {
_stage.addEventListener(type, listener, useCapture, priority, useWeakReference);
} else {
addStack.push({type:type, listener:listener, useCapture:useCapture, priority:priority, useWeakReference:useWeakReference});
}
}
/**
* Remove an event listener object
*
* @param type
* @param listener
* @param useCapture
*
*/
static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
if (_stage != null) {
_stage.removeEventListener(type, listener, useCapture);
} else {
removeStack.push({type:type, listener:listener, useCapture:useCapture});
}
}
}
}