`
hgfghe4
  • 浏览: 60182 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
社区版块
存档分类
最新评论

SWF间的双向通信

阅读更多

演示文件源码下载:

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:Object):void {
    trace(e.score);
}
private function onGameStartHandler(e:Object):void {
    trace(e.score);
}
请注意处理事件的类型不是 GameEvent 而是 Object,理论上应该是 GameEvent ,但是如果这样声明的话会出现“强制转换类型失败”,因此只能用 Object 来替代
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/5223.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics