FLASH教程:SWF间的双向通信

假设有一个主 SWf 名为 main.swf 加载一个名为 game.swf 的游戏模块: 1. main 里面使用 Loader 将 game.swf 加载进来; 2. 在 game 中定义 public function moveBall(speed:Number) 方法,用于开始游戏; 3. 在 main 里面使用类似 loader[content].moveBall(

假设有一个主 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 来替代。

  演示文件源码下载:swfs_communication.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值