如何将 Actionscript "Classes" 与 Flex MXML 文件配合使用?



到目前为止,我已经完全被这件事难住了,我已经做了几天了,我关注的大多数链接和搜索都没有结果。

我想制作一个简单的游戏,有一个鼠标界面,但我也想添加一个预加载程序。我最初使用的是minibuilder,因为它是跨平台的,我在Linux上,但我看到的所有添加预加载程序的教程似乎都与它不兼容

因此,我改为直接使用Flex编译器和文本编辑器,但我运气不太好,甚至预加载程序(这似乎是唯一一个真正有效的想法)也只是一个教程的副本,碰巧奏效了
理想情况下,我只想使用MXML文件指向预加载程序-预加载程序有一个CustomPreloader.as文件-并启动Actionscript类,可能会使用FlashPunk和我的代码来提供帮助。

到目前为止,这是除了CustomPreloader.as之外的每个文件的代码,因为预加载程序已经在工作:(注意:所有文件都在~/ASClasses/src中)

File: ASClasses.mxml
--------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#333333"
    creationComplete="init();"
    width="800" height="500"
    frameRate="60"
    preloader="CustomPreloader"
>
<mx:Script>
<![CDATA[
//This part is mostly for testing purposes
//========================================
import mx.controls.Alert;
public function init():void {
    Alert.show("The first function works.");
}
//And this is what I actually wanted to do
//========================================
import Application;
//Whenever I uncomment the following line, some error is thrown and the init function stops working at all.
//public var myApp:Application = new Application;
//addChild(myApp);
]]>
</mx:Script>
</mx:Application>
File: Application.as
--------------------
package{
    import flash.display.Shape;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.display.Sprite;
    public class Application extends Sprite{
        public function Application(){
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
            rotationX = -45;
            var s:Shape = new Shape;
            s.x = 400;
            s.y = 200; 
            s.rotation = 90;
            addChild(s);
        }
        addEventListener('enterFrame', function(e:Event):void{
            s.rotation += 2.5;
        } );
    }
}

然而,取消注释添加Application.as所需的行似乎只是抛出了一个错误,所以我认为我要么缺少了一些代码,要么做错了什么。

有谁能教我更多这方面的知识吗?尽管我想说我对Actionscript有一些经验,但在这一点上,我已经为自己无法做到这一点而感到非常紧张,如果问得不多,我宁愿简单地解释,假设我以前不知道。

此外,如果有任何全面的简单教程可以用这种方式制作简单/简单的游戏/演示,我也会很感激,因为到目前为止,我看到的大多数教程都只记录Flex和Actionscript,在我真正做任何事情之前,很容易变得复杂。

提前谢谢。

编辑1:此外,可能值得一提的是,以目前的方式,它仍然能够在加载后抛出警报。

尝试

this.rawChildren.addChild(myApp);

代替

addChild(myApp);

应用程序扩展Container,当您向Container添加子级时,该子级应实现IUIComponent接口。因为sprite类没有实现接口,所以它会给您一个错误。

以下是有关此Container addChild 的一些信息

编辑

将添加的应用程序代码放入一个函数中,如

public function init():void {
    Alert.show("The first function works.");
    var myApp:Application = new Application();
    this.rawChildren.addChild(myApp);
}

最新更新