In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

MVCってこんな感じ?

青い四角をクリックすると矢印の向きが変わります。
CがボタンなのでVを含んでしまっています。要検討。
Get Adobe Flash player
by Nicolas 01 Oct 2009
  • Related works: 1
  • Talk

    Nicolas at 01 Oct 2009 07:19
    参考:「ActionScript 3.0 デザインパターン」 のChapter3
    paqos at 01 Oct 2009 23:36
    when was istantiate the rotation variable in View class? How this?? Regards!
    Nicolas at 02 Oct 2009 09:29
    View.rotation is a property inherited from Sprite.
    paqos at 02 Oct 2009 17:31
    Tnx! Always forget this ^^

    Tags

    Embed
/**
 * Copyright Nicolas ( http://wonderfl.net/user/Nicolas )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uFXX
 */

//青い四角をクリックすると矢印の向きが変わります。
//CがボタンなのでVを含んでしまっています。要検討。
package {
	import flash.display.Sprite;
	
	public class MVCTest extends Sprite {
		private var _model:Model;
		public function MVCTest() {
			
			//Mを生成
			_model = new Model();
			
			//VおよびCを生成し、それぞれMへの参照を持たせる
			var view:View = new View(_model);
			view.x = 200;
			view.y = 100;
			addChild(view);
			
			var controller:Controller = new Controller();
			controller.model = _model;
			controller.x = 175;
			controller.y = 300;
			addChild(controller);
			
			
		}
		
	}
	
}

//rotationの数値だけを格納するModel
import flash.display.Graphics;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
class Model extends EventDispatcher {
	private var _rotation:Number = 0;
	
	public function get rotation():Number { return _rotation; }
	
	public function set rotation(value:Number):void {
		_rotation = value;
		dispatchEvent(new Event(Event.CHANGE));
	}
	
	public function Model() {
	}
}

//矢印。Modelから角度の情報を得る
import flash.display.Sprite;
class View extends Sprite {
	private var _model:Model;
	
	public function View(model:Model) {
		_model = model;
		_model.addEventListener(Event.CHANGE, onModelUpdate);
		
		createArrow();
	}
	
	//モデルの更新通知を受け取る
	private function onModelUpdate(e:Event):void {
		rotation = _model.rotation;
	}
	
	private function createArrow():void {
		var g:Graphics = graphics;
		g.lineStyle(3, 0xFF0000);
		g.moveTo(0, -50);
		g.lineTo(0, 50);
		g.lineTo( -20, 30);
		g.moveTo(0, 50);
		g.lineTo(20, 30);
	}
}



import flash.display.Sprite
class Controller extends Sprite {
	//モデルへの参照
	private var _model:Model;
	
	public function set model(value:Model):void {
		_model = value;
		_model.addEventListener(Event.CHANGE, onModelUpdate);
	}
	
	public function Controller() {
		createButton();
	}
	
	private function createButton():void {
		var btn:Sprite = new Sprite();
		var g:Graphics = btn.graphics;
		g.beginFill(0x000066);
		g.drawRect(0, 0, 50, 50);
		g.endFill();
		addChild(btn);
		addEventListener(MouseEvent.CLICK, onClick);
	}
	
	//ボタンクリックでモデルを変更
	private function onClick(e:MouseEvent):void {
		_model.rotation += 45;
	}
	
	//モデルの更新通知を受け取る(現在未使用)
	private function onModelUpdate(e:Event):void {
		
	}
}