SpriteVisualElement中的事件侦听器没有响应

时间:2012-11-06 09:19:29

标签: flex actionscript air event-handling

我已将Flashpunk游戏库嵌入到我的Flex / Air项目中,其中一些Flex组件呈现在FP之上。事情正确显示,Flex组件响应鼠标输入。然而,Flashpunk没有。我像这样嵌入它:

    <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" 
                       resize="windowedapplication1_resizeHandler(event)"
                       initialize="init();"
                       applicationComplete="complete();"
                       showStatusBar="false"
                       mouseEnabled="false"
                       xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>
    <s:SpriteVisualElement id="flashpunk" depth="-2" mouseEnabled="true" mouseChildren="true">
    </s:SpriteVisualElement>

    <mx:MenuBar id="myMenubar" width="832" itemClick="menuHandler(event);" labelField="@label" depth="0">
        <fx:XMLList xmlns="">
            <item label="File">
                <item label="New" id="new" />
                <item label="Open" id="open"/>
                <item label="Save" id="save"/>
                <item label="Save As" id="saveas"/>
                <item label="Quit" id="quit"/>
            </item>

            <item label="Edit">
                <item label="Undo" id="undo"/>
                <item label="Redo" id="redo"/>
                <item label="Preferences" id="preferences"/>
            </item>

            <item label="Level">
                <item label="New Scene" id="newroom"/>
                <item label="Properties" id="properties"/>
            </item>

            <item label="Objects">
                <item label="Clickable" id="clickable"/>
                <item label="Character" id="character"/>
                <item label="Door" id="door"/>
                <item label="Treasure" id="treasure"/>
            </item>
        </fx:XMLList>


    </mx:MenuBar>









    <fx:Script>

        <![CDATA[
            import basicui.Global;
            import basicui.NewUI;

            import flash.display.NativeWindow;
            import flash.display.StageScaleMode;
            import flash.filesystem.File;

            import mx.collections.*;
            import mx.controls.Alert;
            import mx.core.IUIComponent;
            import mx.core.InteractionMode;
            import mx.core.mx_internal;
            import mx.effects.effectClasses.FadeInstance;
            import mx.events.FlexNativeWindowBoundsEvent;
            import mx.events.MenuEvent;
            import mx.events.ResizeEvent;
            import mx.utils.object_proxy;

            import net.flashpunk.FP;
            import net.flashpunk.Screen;

            import org.osmf.elements.F4MElement;

            import spark.components.Application;
            import spark.components.supportClasses.InteractionState;


            public var Maini:Main = new Main; 
            public static var windowsize:Point = new Point(640, 480);
            private var started:Boolean = false;





            private function init():void
            {

                flashpunk.addChild(Maini);
                Global.main = this;


            }

            private function complete():void
            {

                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;


                //calculate system chrome
                //nativeWindow.width = 640;
                //nativeWindow.height = 480;
                Global.chromew=nativeWindow.width-nativeWindow.stage.stageWidth;
                Global.chromeh=nativeWindow.height-nativeWindow.stage.stageHeight;

                Global.uibarh = 50;
                //default windowsize
                resizewindow(windowsize.x, windowsize.y);
                started = true;


            }

            public function resizewindow(w:int, h:int):void
            {


                if (w < 640)
                    w = 640;
                if (h < 480)
                    h = 480;
                windowsize.x = w+Global.chromew;
                windowsize.y = h+Global.chromeh+Global.uibarh;



                nativeWindow.width = w+Global.chromew;
                nativeWindow.height = h +Global.chromeh+Global.uibarh;


                myMenubar.width = nativeWindow.width;

                //resize flashpunk
                FP.width = nativeWindow.width; 
                FP.height =  nativeWindow.height; 
                FP.screen = new net.flashpunk.Screen;
                FP.bounds.width = Number(FP.width);
                FP.bounds.height = Number(FP.height);
            }


            private function menuHandler(evt:MenuEvent):void  
            {
                // Don't open the Alert for a menu bar item that 
                // opens a popup submenu.
                if (evt.item.@id == "new")
                {

                    var a:NewUI = new NewUI;
                    a.begin(this);
                    addElement(a);

                }

                if (evt.item.@id == "open")
                {
                    var filter:FileFilter = new FileFilter("Levels", "*.ael");
                    var file:File = new File;
                    file.browseForOpen("",[filter]);

                    file.addEventListener(Event.SELECT, fileselected);

                }



                if (evt.item.@id == "quit")
                {
                    //close the program
                    //could ask if there is unsaved data...?
                    close();

                }


                    /*Alert.show("Label: " + evt.item.@label + "\n" + 
                        "Data: " + evt.item.@data, "Clicked menu item");
                    */

            }

            private function fileselected(event:Event):void
            {
                //user selected a file:


                File(event.currentTarget).addEventListener(Event.COMPLETE, fileloaded)
                File(event.currentTarget).load();

            }

            private function fileloaded(event:Event):void
            {
                //user selected file is loaded:

                Global.levelfile = File(event.currentTarget);
                Global.levelXML = XML(File(event.currentTarget).data.toString());
                //initialize level project
                Global.editor.initlevel();
            }


            protected function windowedapplication1_resizeHandler(event:ResizeEvent):void
            {
                // TODO Auto-generated method stub
                if (started)
                {
                    windowsize.x = nativeWindow.width;
                    windowsize.y = nativeWindow.height
                    myMenubar.width = nativeWindow.width;
                }
            }

        ]]>

    </fx:Script>



</s:WindowedApplication>

Flashpunk库有自己的输入“Input.as”的事件监听器。出于某种原因,他们似乎没有跑。我认为flex组件以某种方式劫持了这些侦听器,或者SpriteVisualElement不允许或识别它们。我如何启用SpriteVisualElement事件侦听器?我需要设置什么设置?请注意,鼠标或键盘输入都不起作用。

编辑:添加了我的主要mxml代码。有没有办法创建一个“catch all”监听器,然后将事件提供给FP的Input类?

0 个答案:

没有答案