[AS3] Progression4でシーンやページの再利用

2010/05/16

こんにちは。きんくまです。
今度はベースとなるシーンクラスやページクラスを作って、それを再利用してみました。

↓今回つくったやつ(画像クリックでとびます)

インデックスがあって、

prog4_reuse1

その下に3ページならびます

prog4_reuse2

サイト構成はこんなかんじ

prog4_reuse3

ライブラリに3つだけならぶ。BasicPageを再利用してPage1~3のそれぞれを作成します。

prog4_reuse4

各ソースです。

■Index

package {
	import jp.progression.casts.CastDocument;
	import jp.progression.config.WebConfig;
	import jp.progression.debug.Debugger;

	/**
	 * ...
	 * @author ...
	 */
	public class Index extends CastDocument 
	{
		public function Index() 
		{
			super( "index", IndexScene, new WebConfig() );
		}
		
		override protected function atReady():void {
			Debugger.addTarget( manager );
			manager.sync = true;
			manager.goto( manager.syncedSceneId );
		}
	}
}

■IndexScene

package  
{
	import jp.progression.commands.display.AddChild;
	import jp.progression.commands.display.RemoveChild;
	import jp.progression.scenes.SceneObject;
	/**
	 * ...
	 * @author KinkumaDesign
	 */
	public class IndexScene extends SceneObject
	{
		public var scene1:BasicScene;
		public var scene2:BasicScene;
		public var scene3:BasicScene;
		public var page:IndexPage;
		
		public function IndexScene( name:String = null, initObject:Object = null ) 
		{
			super( name, initObject );
			title = "index";
			
			var i:int;
			for (i = 0; i < 3; i++) {
				var childPage:BasicPage = new BasicPage("Page " + (i + 1), "index", this.sceneId);
				var childScene:BasicScene = new BasicScene(childPage, "page" + (i + 1));
				childScene.id = "page" + (i + 1);
				this["scene" + (i + 1)] = childScene;
				addScene(childScene);
			}
			
			page = new IndexPage();
		}
		
		override protected function atSceneInit():void 
		{
			addCommand(
				new AddChild(container, page)
			);
		}
		
		override protected function atSceneGoto():void 
		{
			addCommand(
				new RemoveChild(container, page)
			);
		}
	}

}

■IndexPage

package  
{
	import jp.progression.casts.CastSprite;
	import jp.progression.commands.tweens.DoTweener;
	import jp.progression.scenes.getSceneById;
	/**
	 * ...
	 * @author KinkumaDesign
	 */
	public class IndexPage extends CastSprite
	{
		public var naviBtn1:BasicButton;
		public var naviBtn2:BasicButton;
		public var naviBtn3:BasicButton;
		
		public function IndexPage(initObject:Object = null ) 
		{
			super( initObject );
			
			var i:int;
			for (i = 0; i < 3; i++) {
				var btnName:String = "naviBtn" + (i + 1);
				var btn:BasicButton = getChildByName(btnName) as BasicButton;
				this[btnName] = btn;
				btn.sceneId = getSceneById("page" + (i + 1)).sceneId;
				
				var navi:BasicButton = this["naviBtn" + (i + 1)];
				navi.setLabelText("Page" + (i + 1));
			}
		}
		
		override protected function atCastAdded():void 
		{
			alpha = 0;
			naviBtn1.alpha = 1;
			naviBtn2.alpha = 1;
			naviBtn3.alpha = 1;
			addCommand(
				new DoTweener(this, {alpha:1, time:0.5})
			);
		}
		
		override protected function atCastRemoved():void 
		{
			addCommand(
				new DoTweener(this, { alpha:0, time:0.5 } )
			);
		}
	}

}

■BasicScene

package {
	import jp.progression.commands.display.AddChild;
	import jp.progression.commands.display.RemoveChild;
	import jp.progression.scenes.SceneObject;

	/**
	 * ...
	 * @author ...
	 */
	public class BasicScene extends SceneObject 
	{
		public var page:BasicPage;
		
		public function BasicScene( page:BasicPage, name:String = null, initObject:Object = null ) 
		{
			super( name, initObject );
			this.page = page;
		}
		
		override protected function atSceneInit():void 
		{
			addCommand(
				new AddChild(container, page)
			);
		}
		
		override protected function atSceneGoto():void 
		{
			addCommand(
				new RemoveChild(container, page)
			);
		}
	}
}

■BasicPage

package  
{
	import flash.text.TextField;
	import jp.progression.casts.CastSprite;
	import jp.progression.commands.tweens.DoTweener;
	import jp.progression.scenes.SceneId;
	/**
	 * ...
	 * @author KinkumaDesign
	 */
	public class BasicPage extends CastSprite
	{
		public var pageTitle:TextField;
		public var naviBtn:BasicButton;
		
		public function BasicPage(pageTitleText:String, labelText:String, naviSceneId:SceneId, initObject:Object = null ) 
		{
			super( initObject );
			
			pageTitle = getChildByName("pageTitle") as TextField;
			naviBtn = getChildByName("naviBtn") as BasicButton;
			
			pageTitle.text = pageTitleText;
			naviBtn.setLabelText(labelText);
			naviBtn.sceneId = naviSceneId;
		}
		
		override protected function atCastAdded():void 
		{
			naviBtn.alpha = 1;
			alpha = 0;
			addCommand(
				new DoTweener(this, {alpha:1, time:0.5})
			);
		}
		
		override protected function atCastRemoved():void 
		{
			addCommand(
				new DoTweener(this, { alpha:0, time:0.5 } )
			);
		}
	}

}

■BasicButton

package  
{
	import caurina.transitions.Tweener;
	import flash.text.TextField;
	import jp.progression.casts.CastButton;
	import jp.progression.commands.tweens.DoTweener;
	/**
	 * ...
	 * @author KinkumaDesign
	 */
	public class BasicButton extends CastButton
	{
		public var label:TextField;
		
		public function BasicButton( initObject:Object = null ) 
		{
			super( initObject );
			label = getChildByName("label") as TextField;
		}
		
		public function setLabelText(text:String):void
		{
			label.text = text;
		}
		
		override protected function atCastRollOver():void 
		{
			Tweener.removeTweens(this);
			Tweener.addTween(this, { alpha:0.5, time:0.5 } );
		}
		
		override protected function atCastRollOut():void 
		{
			Tweener.removeTweens(this);
			Tweener.addTween(this, { alpha:1, time:0.5 } );
		}
	}

}

>> 今回のソース一式です。

XML(PRML)を利用して、シーンとクラスのひもづけをすることもできるみたいなんで、そのあたりもやってみるともっと生産性があがるかなと思います。

LINEで送る
Pocket

自作iPhoneアプリ 好評発売中!
フォルメモ - シンプルなフォルダつきメモ帳
ジッピー電卓 - 消費税や割引もサクサク計算!

ページトップへ戻る