mxml使用.swc库文件将addChild组件添加到panel / canvas / etc并运行游戏代码

时间:2014-05-19 14:11:41

标签: actionscript-3 flash flex flash-builder mxml

我正在使用flash builder来构建一个flex项目,我刚刚进入mxml。我有一个我在Flash Professional CC中制作的Flash游戏 - 我从Flash CC发布了一个swc文件 - 所有符号都在第一帧中导出,并导出为actionscript(在符号属性中) - 它们驻留在与swc文件相同的文件夹。

在Flash Builder 4.6中 - 我将swc文件添加到库路径,以便我可以访问这些符号。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <!-- <s:VGroup>
        <s:TextArea id="textArea" width="300" height="300"/>
        <s:Button label="Connect" click="onConnectClick()"/>
        <s:Button label="Send" click="onSendClick()"/>
        <s:Button label="Disconnect" click="onDisconnectClick()"/>
    </s:VGroup> -->
    <fx:Script>
        <![CDATA[
            import Avatar;
            import Enemy;

            import flash.display.*;

            import com.adobe.serialization.json.JSON;
            import com.hurlant.util.der.Integer;

            import io.socket.flash.ISocketIOTransport;
            import io.socket.flash.ISocketIOTransportFactory;
            import io.socket.flash.SocketIOErrorEvent;
            import io.socket.flash.SocketIOEvent;
            import io.socket.flash.SocketIOTransportFactory;
            import io.socket.flash.WebsocketTransport;
            import io.socket.flash.XhrPollingTransport;

            import flash.utils.Timer;
            import flash.events.TimerEvent;

            import flash.system.Security;

            private var _socketIOTransportFactory:ISocketIOTransportFactory = new SocketIOTransportFactory();
            private var _ioSocket:ISocketIOTransport;

            Security.loadPolicyFile("xmlsocket://localhost:10843");

            private function onConnectClick():void
            {
                _ioSocket = _socketIOTransportFactory.createSocketIOTransport(XhrPollingTransport.TRANSPORT_TYPE, "localhost:8000/socket.io", this);
                _ioSocket.addEventListener(SocketIOEvent.CONNECT, onSocketConnected);
                _ioSocket.addEventListener(SocketIOEvent.DISCONNECT, onSocketDisconnected);
                _ioSocket.addEventListener(SocketIOEvent.MESSAGE, onSocketMessage);
                _ioSocket.addEventListener(SocketIOErrorEvent.CONNECTION_FAULT, onSocketConnectionFault);
                _ioSocket.addEventListener(SocketIOErrorEvent.SECURITY_FAULT, onSocketSecurityFault);
                _ioSocket.connect();
            }

            private function onSocketConnectionFault(event:SocketIOErrorEvent):void
            {
                trace(event.type + ":" + event.text);
            }

            private function onSocketSecurityFault(event:SocketIOErrorEvent):void
            {
                trace(event.type + ":" + event.text);
            }

            private function onDisconnectClick():void
            {
                _ioSocket.disconnect();
            }

            private function onSocketMessage(event:SocketIOEvent):void
            {
                if (event.message is String)
                {
                    trace(String(event.message));
                }
                else
                {
                    trace(com.adobe.serialization.json.JSON.encode(event.message));
                }
            }

            private function onSendClick():void
            {
                _ioSocket.send({type: "chatMessage", data: "Привет!!!"});
                _ioSocket.send({type: "chatMessage", data: "Delirium tremens"});
                _ioSocket.send("HELLO!!!");
            }

            private function onSocketConnected(event:SocketIOEvent):void
            {
                //logMessage("Connected" + event.target);
                trace("connected");
            }

            private function onSocketDisconnected(event:SocketIOEvent):void
            {
                //logMessage("Disconnected" + event.target);
                trace("disconnected");
            }

            /*private function logMessage(message:String):void
            {
                textArea.text = textArea.text + message + "\n";
            }*/         

            public function init():void {

                var enemy:Enemy = new Enemy();
                var avatar:Avatar = new Avatar();
                var gameTimer:Timer;    

                myGroup.addChild(enemy);
                myGroup.addChild(avatar);   

                avatar.x = mouseX;
                avatar.y = mouseY;

                gameTimer = new Timer( 25 );
                gameTimer.addEventListener( TimerEvent.TIMER, onTick );
                gameTimer.start();

                function onTick( timerEvent:TimerEvent ):void 
                {
                    enemy.moveDownABit();
                    avatar.x = mouseX;
                    avatar.y = mouseY;
                    var score:int;

                    if ( avatar.hitTestObject( enemy ) ) 
                    {
                        score = gameTimer.currentCount;
                        gameTimer.stop();
                    }
                }
            }
        ]]>
    </fx:Script>
    <s:Group id="myGroup" width="400" height="300" initialize="init();"/>
</s:Application>

我没有错误,但是它出现了一个空白页面(这是一个有化身和敌人的游戏 - 当化身在屏幕上移动时,化身试图避开敌人。)

我做错了什么?我是否还需要为游戏嵌入swf文件?

另外 - 你可以忽略所有套接字的东西 - 一切正常(我已经测试过,但我试图将socket.io-flash与我的游戏集成)。感谢。

1 个答案:

答案 0 :(得分:1)

尝试像这样嵌入您的SWF文件:

[Embed(source="foo.swf")]
[Bindable]
public var game:Class;

当您不需要嵌入式应用程序与嵌入式应用程序交互时,通常会嵌入Flex应用程序。如果嵌入式应用程序需要与嵌入式应用程序交互,您可以考虑将其实现为自定义组件,而不是单独的应用程序。

[被修改]

否则:

[Bindable]
public var loadedSM:SystemManager;

// Initialize variables with information from
// the loaded application.
private function initNestedAppProps():void {
     loadedSM = SystemManager(myLoader.content);
}

.
.
.

<mx:SWFLoader id="myLoader" width="100%" height="100%"
source="foo.swf"
creationComplete="initNestedAppProps();"/>

我不是100%确定这是你正在寻找的,这个解决方案将是一个适应的bugger。 对不起,如果我无法帮助