原文链接:http://www.dreamfairy.cn/blog/?p=900---原文配了一个动画
:( 可怜的DreamFairy 被学校强迫性的,无工资去厦门中软海晟的实训1个月…. 每天都是乏味的事情。
=。= 虽然只是一个月,但是DreamFairy 还是很果断的花了200大洋,迁了光线网络。 然后开始了我的pureMVC之路。
接触MVC是早晚之事,之后开发网游,基本都是在相关框架内构建。避免之后工作中跟不上进度,于是开始了pureMVC学习之路。
经过了连续2天不断查阅资料,看了各种文档。终于理解了MVC各个模块的交互方式。 上传上来给自己留个纪念。
PureMVC是在基于模型、视图和控制器MVC模式建立的一个轻量级的应用框架,这种开源框架是免费的,它最初是执行的ActionScript 3语言使用的Adobe Flex、Flash和AIR,现在已经移植到几乎所有主要的发展平台,目前支持两个版本框架:标准和多核,总之,标准版提供了一种简单的编码分离的方法,按照MVC设计概念。除此之外,多版本允许多个PureMVC应用运行在同一个虚拟机。
在PureMVC实现的经典MVC元设计模式中,这三部分由三个单例模式类管理,分别是Model、View和Controller。三者合称为核心层或核心角色。
demo演示: 使用键盘方向键进行操作
类视图
文档类
{
import application .ApplicationFacade ;
import application .controller .StartUpCommand ;
import flash.display . Sprite ;
[SWF ( frameRate= "30" , width= "500" , height= "400" ) ]
public class Main extends Sprite
{
private var facade :ApplicationFacade ;
public function Main ( )
{
//获取facade单例
facade=ApplicationFacade .getInstance ( ) ;
//启动pureMVC
facade .sendNotification (ApplicationFacade .STARTUP , this ) ;
}
}
}
控制中心
{
import application .controller .StartUpCommand ;
import org .puremvc .as3 .interfaces .IFacade ;
import org .puremvc .as3 .patterns .facade .Facade ;
public class ApplicationFacade extends Facade implements IFacade
{
public static const STARTUP : String= "startup" ;
//静态获取facade
public static function getInstance ( ) :ApplicationFacade
{
if (instance== null )instance= new ApplicationFacade ( ) ;
return instance as ApplicationFacade ;
}
//在facade初始化的时候执行,并注册命令StartUpCommand
protected override function initializeController ( ) : void
{
super .initializeController ( ) ;
registerCommand (ApplicationFacade .STARTUP ,StartUpCommand ) ;
}
}
}
多核命令
{
import org .puremvc .as3 .core .View ;
import org .puremvc .as3 .interfaces .ICommand ;
import org .puremvc .as3 .patterns .command .MacroCommand ;
public class StartUpCommand extends MacroCommand implements ICommand
{
//多核MacroCommand
public function StartUpCommand ( )
{
//加载Command队列
//初始化逻辑COmmand
addSubCommand (ModelCommand ) ;
//初始化界面Command
addSubCommand (ViewCommand ) ;
}
}
}
界面中介
{
import application .view .ApplicationMediator ;
import org .puremvc .as3 .interfaces .ICommand ;
import org .puremvc .as3 .interfaces .INotification ;
import org .puremvc .as3 .patterns .command .SimpleCommand ;
public class ViewCommand extends SimpleCommand implements ICommand
{
public override function execute (notification :INotification ) : void
{
//注册界面中介
var main :Main=notification .getBody ( ) as Main ;
facade .registerMediator ( new ApplicationMediator (ApplicationMediator .NAME ,main ) ) ;
}
}
}
业务逻辑
{
import application .model .RockProxy ;
import org .puremvc .as3 .interfaces .ICommand ;
import org .puremvc .as3 .interfaces .INotification ;
import org .puremvc .as3 .patterns .command .SimpleCommand ;
public class ModelCommand extends SimpleCommand implements ICommand
{
public override function execute (notification :INotification ) : void
{
//注册数据代理
facade .registerProxy ( new RockProxy (RockProxy .NAME ) ) ;
}
}
}
界面中介
{
import application .ApplicationFacade ;
import application .model .RockProxy ;
import flash.display . StageScaleMode ;
import flash.events . Event ;
import flash.events . KeyboardEvent ;
import flash.ui . ContextMenu ;
import org .puremvc .as3 .interfaces .IMediator ;
import org .puremvc .as3 .interfaces .INotification ;
import org .puremvc .as3 .patterns .mediator .Mediator ;
public class ApplicationMediator extends Mediator implements IMediator
{
public static const NAME : String= "applicationmediator" ;
public static const KEYDOWN : String= "keydown" ;
public static const KEYUP : String= "keyup" ;
public static const ROCKLOADED : String= "rockloaded" ;
private var rockProxy :RockProxy ;
public function ApplicationMediator (mediatorName : String= null , viewComponent : Object= null )
{
super (mediatorName , viewComponent ) ;
//初始化视图
main . stage . scaleMode= StageScaleMode . SHOW_ALL ;
//注册键盘监听
main . stage . addEventListener ( KeyboardEvent . KEY_DOWN ,_keyDown ) ;
main . stage . addEventListener ( KeyboardEvent . KEY_UP ,_keyUp ) ;
//绘制场景
drawAssets ( ) ;
}
private function _keyUp (e : KeyboardEvent ) : void
{
//发送键盘按键松开的消息
sendNotification (ApplicationMediator .KEYUP ,e . keyCode ) ;
}
private function _keyDown (e : KeyboardEvent ) : void
{
//发送键盘按键按下的消息
sendNotification (ApplicationMediator .KEYDOWN ,e . keyCode ) ;
}
public function get main ( ) :Main
{
return viewComponent as Main ;
}
private function drawAssets ( ) : void
{
rockProxy=facade .retrieveProxy (RockProxy .NAME ) as RockProxy ;
main . addChild (rockProxy .rockVO ) ;
rockProxy .rockVO . x= 200 ;
rockProxy .rockVO . y= 0 ;
rockProxy .setRockValue ( .98 , 12 , 5 , 0 ) ;
//创建rock的中介
facade .registerMediator ( new RockMediator (rockProxy .rockVO ) ) ;
//发送洛克加载完毕的通知
sendNotification (ApplicationMediator .ROCKLOADED ,rockProxy .rockVO ) ;
}
}
}
洛克视图中介
{
import application .ApplicationFacade ;
import application .model .VO .Rockman ;
import flash.events . Event ;
import flash.ui . Keyboard ;
import org .puremvc .as3 .interfaces .IMediator ;
import org .puremvc .as3 .interfaces .INotification ;
import org .puremvc .as3 .patterns .mediator .Mediator ;
public class RockMediator extends Mediator implements IMediator
{
public static const NAME : String= "rockVOmediator" ;
private var key : Object ;
private var onJump : Boolean= false ;
private var onWalk : Boolean= false ;
public function RockMediator (viewComponent : Object= null )
{
super (NAME , viewComponent ) ;
key= new Object ( ) ;
}
override public function listNotificationInterests ( ) : Array
{
//表示对按键按下,松开,洛克加载完毕的事件感兴趣,进行订阅
return [ApplicationMediator .KEYDOWN ,ApplicationMediator .ROCKLOADED ,ApplicationMediator .KEYUP ] ;
}
override public function handleNotification (notification :INotification ) : void
{
switch (notification .getName ( ) )
{
case ApplicationMediator .KEYDOWN :
var DownkeyCode : int=notification .getBody ( ) as int ;
key [DownkeyCode ]= true ;
break ;
case ApplicationMediator .KEYUP :
var UpkeyCode : int=notification .getBody ( ) as int ;
delete key [UpkeyCode ] ;
onWalk= false ;
if ( !onJump )rockVO .rock . gotoAndStop ( "stand" ) ;
break
case ApplicationMediator .ROCKLOADED :
rockVO .rock . gotoAndStop ( "stand" ) ;
rockVO . addEventListener ( Event . ENTER_FRAME ,onEnter ) ;
break ;
}
}
private function onEnter (e : Event ) : void
{
rockVO .dropSpeed +=rockVO .gravity ;
rockVO . y +=rockVO .dropSpeed ;
if (rockVO .y < 200 )
{
onJump= true ;
} else
{
onJump= false ;
rockVO . y= 200 ;
rockVO .dropSpeed= 0 ;
if ( !onWalk )rockVO .rock . gotoAndStop ( "stand" ) ;
}
if (key [ Keyboard . LEFT ] )
{
if ( !onJump )
{
onWalk= true ;
rockVO .rock . gotoAndStop ( "walk" ) ;
}
rockVO . scaleX= - 1 ;
rockVO .x -=rockVO .moveSpeed ;
}
if (key [ Keyboard . RIGHT ] )
{
if ( !onJump )
{
onWalk= true ;
rockVO .rock . gotoAndStop ( "walk" ) ;
}
rockVO . scaleX= 1 ;
rockVO . x +=rockVO .moveSpeed ;
}
if ( !onJump && key [ Keyboard . UP ] )
{
onJump= true ;
rockVO .rock . gotoAndStop ( "jump" ) ;
rockVO .dropSpeed= -rockVO .jumpPower ;
}
if (rockVO .x <- 10 )
{
rockVO . x= 510 ;
}
if (rockVO .x > 510 )
{
rockVO . x= - 10 ;
}
}
public function get rockVO ( ) :Rockman
{
return viewComponent as Rockman
}
}
}
洛克数据代理
{
import application .model .VO .Rockman ;
import application .view .ApplicationMediator ;
import org .puremvc .as3 .interfaces .IProxy ;
import org .puremvc .as3 .patterns .proxy . Proxy ;
public class RockProxy extends Proxy implements IProxy
{
public static const NAME : String= "RockProxy" ;
private var _rock :Rockman ;
public function RockProxy (proxyName : String= null , data : Object= null )
{
super (proxyName ,_rock ) ;
}
//设定一个批量赋值RockmanVO的api
public function setRockValue (gravity : Number ,jumpPower : Number ,moveSpeed : Number ,dropSpeed : Number ) : void
{
rockVO .gravity=gravity ;
rockVO .jumpPower=jumpPower ;
rockVO .moveSpeed=moveSpeed ;
rockVO .dropSpeed=dropSpeed ;
}
public function get rockVO ( ) :Rockman
{
if (_rock== null )_rock= new Rockman ( ) ;
return _rock as Rockman ;
}
}
}
洛克VO数据
{
import flash.display . Sprite ;
public class Rockman extends Sprite
{
private var _rock :Rock ;
private var _gravity : Number ;
private var _moveSpeed : Number ;
private var _dropSpeed : Number ;
private var _jumpPower : Number ;
public function Rockman ( )
{
loadRock ( ) ;
}
public function get dropSpeed ( ) : Number
{
return _dropSpeed ;
}
public function set dropSpeed ( value : Number ) : void
{
_dropSpeed = value ;
}
public function get rock ( ) :Rock
{
return _rock ;
}
public function set rock ( value :Rock ) : void
{
_rock = value ;
}
public function get gravity ( ) : Number
{
return _gravity ;
}
public function set gravity ( value : Number ) : void
{
_gravity = value ;
}
public function get moveSpeed ( ) : Number
{
return _moveSpeed ;
}
public function set moveSpeed ( value : Number ) : void
{
_moveSpeed = value ;
}
public function get jumpPower ( ) : Number
{
return _jumpPower ;
}
public function set jumpPower ( value : Number ) : void
{
_jumpPower = value ;
}
private function loadRock ( ) : void
{
_rock= new Rock ( ) ;
addChild (_rock ) ;
}
}
}
pureMVC 的 View,Controller 部分通过消息机制进行交互,在 Controller 中注册 Model中 VO 的 Proxy,获取实际数据。 其中 Model 无法接收消息,只能发送消息,因此可以用在数据更新时进行广播。
由于 Controller 是 Command 的集合,但是 Command 是无状态的,因此像ENTER_FRAME 之类的状态监听是无法使用的。
也是由于这个原因,本因写在 Controller部分的逻辑,被写在了 View 的 Mediator 中了。 期待自己以后更加了解 pureMVC后再做改动吧~