イナヅマTVログ

ActionScript, stageのリサイズを監視するクラスを作ってみた

| 0件のコメント

フルFlashサイトを作成する時にstageのリサイズ・イベントを監視し、windowの幅、高さに合わせてゴニョゴニョする時はstageでEvent.RESIZEを監視しなくてはいけません。
毎回同じような関数を書いていたので後々でも使えそうなクラスを書いてみることにしました。

書いたクラスは2つ。
■Spriteを継承したResizeManager
 Singletonで作っています。

■Eventを継承したResizeEvent

ResizeEvent

package com.inazumatv.events
{
	import flash.events.Event;
 
	public class ResizeEvent extends Event
	{
		//---------------------------------------
		// CLASS CONSTANTS
		//---------------------------------------
		public static const STAGE_RESIZE:String = "stage_resize";
		//---------------------------------------
		// PRIVATE VARIABLES
		//---------------------------------------
		private var _width:Number;
		private var _height:Number;
		//---------------------------------------
		// CONSTRUCTOR
		//---------------------------------------
		public function ResizeEvent(type:String, width:Number, height:Number, bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
			_width = width;
			_height = height;
		}
		//---------------------------------------
		// GETTER / SETTERS
		//---------------------------------------
		public function get width():Number
		{
			return _width;
		}
 
		public function get height():Number
		{
			return _height;
		}
		//---------------------------------------
		// PUBLIC METHODS
		//---------------------------------------
		public override function clone():Event
		{
			return new ResizeEvent(type, width, height, bubbles, cancelable);
		}
 
		public override function toString():String
		{
			return "ResizeEvent{width:" + width + ", height:" + height + "}";
		}
	}
}

ResizeManager

package com.inazumatv.events
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
 
	public class ResizeManager extends Sprite
	{
		private static var _instance:ResizeManager;
		private static var allowInstantiation:Boolean;
		private static var _container:DisplayObjectContainer;
		//---------------------------------------
		// CONSTRUCTOR
		//---------------------------------------
		public function ResizeManager()
		{
			if (!allowInstantiation)
			{
				throw new Error("Error: new演算子では無くResizeManager.getManager(container:DisplayObjectContainer) を使ってインスタンスを取得して下さい.");
			}
		}
		//---------------------------------------
		// PUBLIC METHODS
		//---------------------------------------
		public static function getManager(container:DisplayObjectContainer=null):ResizeManager {
			if (_instance == null && container == null) {
				throw new Error("Error: 引数にDisplayObjectContainerが必要です.");
			}
			else if (container is DisplayObjectContainer && container.stage == null)
			{
				throw new Error("Error: 引数に有効なDisplayObjectContainerが必要です.");
			}
			else if (_instance == null)
			{
				allowInstantiation = true;
				_instance = new ResizeManager();
				_container = container;
				container.stage.addEventListener(Event.RESIZE, onEventCall);
			}
			return _instance;
		}
		public static function stop():void
		{
			if (allowInstantiation)
				_container.stage.removeEventListener(Event.RESIZE, onEventCall);
		}
		public static function resume():void {
			if (allowInstantiation)
				_container.stage.addEventListener(Event.RESIZE, onEventCall);
		}
		//---------------------------------------
		// PRIVATE METHODS
		//---------------------------------------
		private static function onEventCall(event:Event):void {
			var w:Number = _container.stage.stageWidth;
			var h:Number = _container.stage.stageHeight;
			_instance.dispatchEvent(new ResizeEvent(ResizeEvent.STAGE_RESIZE, w, h));
		}
	}
}

使い方はこんな感じ。
フレームスクリプトです。

import com.inazumatv.events.ResizeManager;
import com.inazumatv.events.ResizeEvent;
 
var manager:ResizeManager = ResizeManager.getManager(this);
manager.addEventListener(ResizeEvent.STAGE_RESIZE, resizeHandler);
 
function resizeHandler (e:ResizeEvent) {
	trace("resizeHandler", e.width, e.height);
}

1回ResizeManager.getManagerする時DisplayObjectContainerが引数として渡されれば2回目のインスタンスを作る時からは必要なくなります。

var manager2:ResizeManager = ResizeManager.getManager();
manager2.addEventListener(ResizeEvent.STAGE_RESIZE, resizeHandler2);
 
function resizeHandler2 (e:ResizeEvent) {
	trace("resizeHandler2", e.width, e.height);
}

ResizeManager.getManagerの引数DisplayObjectContainerは表示オブジェクトとしてaddChildされている必要があります。
初めてインスタンスを作る時に以下のようにするとエラーになります。

var manager:ResizeManager = ResizeManager.getManager(new Sprite());

Event.RESIZE通知を一時的に無効にすることも可能です。

// 無効
ResizeManager.stop();
// 再開
ResizeManager.resume();

Singletonクラス作成はgskinner先生のgBlog: AS3: Singletonsを参考にさせていただきました。

update 2010-06-17
public static function stop, public static function resumeif を追加しました。

コメントを残す

必須欄は * がついています


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください