• [转] SWF 间双向通信 [FL 车在臣]


    演示文件源码下载http://kerryas.googlecode.com/files/swfs_communication.rar

    假设有一个主 SWf 名为 main.swf 加载一个名为 game.swf 的游戏模块:

    1. main 里面使用 Loader 将 game.swf 加载进来;
    2. 在 game 中定义 public function moveBall(speed:Number) 方法,用于开始游戏;
    3. 在 main 里面使用类似 loader["content"].moveBall(speed) 的语句调用 game.swf 里面的方法;
    4. game.swf 与 main.swf 通信的方法,可以使用 dispatchEvent 方法与 main.swf 通信,也可以继续使用上述方法。

    下面请看示例:
    1. 首先创建被调用的 game.swf:

    package {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.system.Security;
    
        public class Game extends Sprite {
            private var ball:Sprite;
            private var speed:Number;
           
            public function Game() {
                // 在 Flash IDE 中执行 Debug
                Security.allowInsecureDomain("*");
               
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
            }
           
            private function init(e:Event = null):void {
                removeEventListener(Event.ADDED_TO_STAGE, init);
                ball = new Sprite();
                ball.graphics.beginFill(0xFF0000);
                ball.graphics.drawCircle(0, 0, 50);
                ball.graphics.endFill();
                addChild(ball);
                ball.x = 50;
                ball.y = stage.stageHeight / 2;
            }
           
            public function moveBall(speed:Number):void {
                this.speed = speed;
                addEventListener(Event.ENTER_FRAME, onGameLoop);
            }
           
            private function onGameLoop(e:Event):void {
                ball.x += speed;
            }
           
            public function stopMove():void {
                removeEventListener(Event.ENTER_FRAME, onGameLoop);
            }
        }
    }
    

    在这个类里创建了一个小球实例 ball,还提供了两个公开的方法 moveBall(speed) 和 stopMove(),用于控制小球的运动与停止。最后,编译该文件将生成好的 game.swf 放到应用服务器的根目录上(http://localhost/game.swf)。

    2. 下面创建主程序,调用 game.swf 并与其通信

    package {
        import flash.display.Loader;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.net.URLRequest;
        import flash.system.Security;
        public class Main extends Sprite {
            private var loader:Loader;
           
            public function Main() {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
            }
           
            private function init(e:Event = null):void {
                removeEventListener(Event.ADDED_TO_STAGE, init);
               
                // 允许访问变量、对象、属性、方法等
                Security.allowDomain("*");
               
                loader = new Loader();
                addChild(loader);
               
                // 加载 http://localhost/game.swf 后面的参数用于防止缓存
                loader.load(new URLRequest("http://localhost/game.swf?" + new Date().time));
                loader.contentLoaderInfo.addEventListener(Event.INIT, onLoadComplete);
            }
           
            private function onLoadComplete(e:Event):void {
                // 调用 game.swf 中的 moveBall(speed) 方法
                loader["content"].moveBall(5);
               
                // 点击舞台后调用 game.swf 中的 stopMove() 方法
                stage.addEventListener(MouseEvent.CLICK, onClickStageHandler);
            }
           
            private function onClickStageHandler(e:MouseEvent):void {
                loader["content"].stopMove();
            }
           
        }
    }

    主程序中先将 game.swf 加载进来,然后调用 game.swf 中的 moveBall(speed) 方法,并侦听在舞台发生点击事件后调用 game.swf 中的 stopMove() 方法。

    3. game.swf 与 main.swf 通信方法:
    调用的方法可以和 main.swf 调用 game.swf 一样,也可以使用事件调度,下面是事件驱动方法:
    (1)定义 GameEvent.as 事件

    package {
        import flash.events.Event;
        public class GameEvent extends Event {
            public static const GAME_START:String = "game_start";
            public static const GAME_OVER:String = "game_over"
           
            public var score:uint;
           
            public function GameEvent(type:String, score:uint = 0) {
                this.score = score;
                super(type);
            }
           
        }
    }
    

    (2)在 Game 中当调用 moveBall(speed) 方法时分发 GameEvent.GAME_START 事件:
    dispatchEvent(new GameEvent(GameEvent.GAME_START));
    在 Game 中当调用 stopMove() 方法时分发 GameEvent.GAME_OVER 事件:
    dispatchEvent(new GameEvent(GameEvent.GAME_OVER, ball.x)); // 将小球的 x 坐标作为 GameEvent 的参数发出

    (3)在 Main 中侦听这两个事件(当 game.swf 被加载进来后):

    loader["content"].addEventListener(GameEvent.GAME_START, onGameStartHandler);
    loader["content"].addEventListener(GameEvent.GAME_OVER, onGameOverHandler);
    private function onClickStageHandler(e:MouseEvent):void {
        loader["content"].stopMove();
    }
    private function onGameOverHandler(e:GameEvent):void {
        trace(e.score);
    }
    private function onGameStartHandler(e:GameEvent):void {
        trace(e.score);
    }
    
    

    原文链接:http://blog.sina.com.cn/s/blog_3ecb9b110100fx4d.html

  • 相关阅读:
    js转盘游戏
    wps excel
    微软笔记本
    图片识别
    数据库 连接 集
    Vue + Element UI 实现权限管理系统 前端篇(十二):用户管理模块
    Vue + Element UI 实现权限管理系统 前端篇(十三):页面权限控制
    Vue + Element UI 实现权限管理系统 前端篇(十四):菜单功能实现
    Vue + Element UI 实现权限管理系统 前端篇(十五):嵌套外部网页
    Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义
  • 原文地址:https://www.cnblogs.com/raywalker/p/1811053.html
Copyright © 2020-2023  润新知