zoukankan      html  css  js  c++  java
  • PureMVC实例入门

    看完了PureMVC的源码,学习的官方的示例,就该自己整个demo出来了。我用它来实现一个非常简单的小游戏。一个小车(就是长方形),用键盘的左右方向键控制它的转向,向上键控制前进。

    大概的效果图如下:

    第一步,新建包目录,包括controller,model和view,view下面有个子package components。(见上图左半部分)

    第二步,编写ApplicationFacade。里面初始化StartupCommand

        override protected function initializeController( ) : void 
        {
          super.initializeController();            
          registerCommand( STARTUP, StartupCommand );
        }
        
        public function startup( stage:Object ):void
        {
          sendNotification( STARTUP, stage );
        }

    第三步,编写StartupCommand,里面初始化小车的视图,注册相关的Mediator

    public class StartupCommand extends SimpleCommand implements ICommand
      {
        override public function execute(notification:INotification):void{
          var stage:Stage = notification.getBody() as Stage;
          facade.registerMediator(new StageMediator(stage));
          
          var car:Car = new Car();
          facade.registerMediator(new CarMediator(car));
          
          stage.addChild(car);
        }
      }

    第四步,编写小车的视图类Car,里面设计了相关键盘按下的操作变量,在EnterFrame里面更新小车的状态

        private function updatePos(evt:Event):void{
          if(leftArrow){
            this.rotation -= TURN_SPEED;
          }else if(rightArrow){
            this.rotation += TURN_SPEED;
          }else if(upArrow){
            var angle:Number = Math.PI*(this.rotation/180);
            var dx:Number = FORWARD_SPEED*Math.cos(angle);
            var dy:Number = FORWARD_SPEED*Math.sin(angle);
            this.x+=dx;
            this.y+=dy;
          }
        }

    第五步,编写小车的Mediator CarMediator,里面监听键盘事件更新后的Notification,然后更新Car的相关变量

        override public function handleNotification(notification:INotification):void{
          switch(notification.getName()){
            case ApplicationFacade.TURN_LEFT:
              car.leftArrow = true;
              break;
            case ApplicationFacade.TURN_RIGHT:
              car.rightArrow = true;
              break;
            case ApplicationFacade.FORWARD:
              car.upArrow = true;
              break;
            
            case ApplicationFacade.STOP_TURN_LEFT:
              car.leftArrow = false;
              break;
            case ApplicationFacade.STOP_TURN_RIGHT:
              car.rightArrow = false;
              break;
            case ApplicationFacade.STOP_FORWARD:
              car.upArrow = false;
              break;
          }
        }

    第六步,编写舞台的Mediator -StageMediator,里面监听键盘按下事件,然后发送相关的Notification

        private function handleKeyDown(evt:KeyboardEvent):void{
          switch(evt.keyCode){
            case Keyboard.LEFT:
              sendNotification(ApplicationFacade.TURN_LEFT);
              break;
            case Keyboard.RIGHT:
              sendNotification(ApplicationFacade.TURN_RIGHT);
              break;
            case Keyboard.UP:
              sendNotification(ApplicationFacade.FORWARD);
              break;
          }
        }
        
        private function handleKeyUp(evt:KeyboardEvent):void{
          switch(evt.keyCode){
            case Keyboard.LEFT:
              sendNotification(ApplicationFacade.STOP_TURN_LEFT);
              break;
            case Keyboard.RIGHT:
              sendNotification(ApplicationFacade.STOP_TURN_RIGHT);
              break;
            case Keyboard.UP:
              sendNotification(ApplicationFacade.STOP_FORWARD);
              break;
          }

    用了puremvc后,感觉代码间的耦合性低了,便于管理。不过用的过程中还是存在很多问题了,可以参考一下官方的那份《最佳实践》文档,里面提供了一些好的建议。

  • 相关阅读:
    Linux vim 跳转到指定行
    Linux安装lamp过程中出的问题
    centos 7.4源码安装mysql5.5.20+apache 2.4.29+php5.3.28
    centos install vsftpd常见的错误:vsftpd: refusing to run with writable root inside chroot ()错误
    python join函数
    enumerate函数
    实战项目 1:5 行代码爬取国内所有上市公司信息
    python yield用法理解
    python time模块
    yolov5-OSError: [WinError 1455] 页面文件太小,无法完成操作。 Error loading "C:Anaconda3libsite-packages orchlibcaffe2_detectron_ops_gpu.dll"
  • 原文地址:https://www.cnblogs.com/tinytiny/p/2528044.html
Copyright © 2011-2022 走看看