调用AS3包中的函数

发布于 2024-12-08 22:41:36 字数 2321 浏览 4 评论 0 原文

下面是使用 AIR SDK 3.0 从 Flash Builder 4.5 构建的 MXML 代码。使用 Starling 框架创建 2D 动画,想知道如何在不创建新的 Game 实例的情况下调用 addText 函数?

main.mxml 是一个主应用程序:

<?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"
                       applicationComplete="windowedapplication1_applicationCompleteHandler(event)"
                       backgroundAlpha="0" showStatusBar="false" height="700" frameRate="60" width="800">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import starling.core.Starling;

            private var mStarling:Starling;

            protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
            {

                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;
                this.y=0;

                mStarling = new Starling(Game, stage);
                mStarling.start();
            }

            private function gaa():void {
                //How to access addText() in Games.as?
            }

        ]]>
    </fx:Script>
    <s:Button x="693" y="19" label="Add Text" click="gaa()"/>

</s:WindowedApplication>

Games.as 是一个创建精灵的包:

package 
{
    import scenes.Scene;
    import starling.display.Button;
    import starling.display.Image;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.textures.Texture;

    public class Game extends Sprite
    {
        private var mMainMenu:Sprite;
        private var mCurrentScene:Scene;

        public function Game()
        {
            var bg:Image = new Image(Assets.getTexture("Background"));
            addChild(bg);

            mMainMenu = new Sprite();   //create new sprite
            addChild(mMainMenu);

        }
        public function addText():void {
            var logo:Image = new Image(Assets.getTexture("Logo"));  //add logo
            logo.x = int((300 - logo.width) / 2);
            logo.y = 50;
            mMainMenu.addChild(logo);
        }
    }
}

Below is a MXML code built from Flash Builder 4.5 with AIR SDK 3.0. Using Starling framework to create 2D animation and wonder how do I call a addText function without create a new instance of Game?

main.mxml is a main application:

<?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"
                       applicationComplete="windowedapplication1_applicationCompleteHandler(event)"
                       backgroundAlpha="0" showStatusBar="false" height="700" frameRate="60" width="800">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import starling.core.Starling;

            private var mStarling:Starling;

            protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
            {

                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;
                this.y=0;

                mStarling = new Starling(Game, stage);
                mStarling.start();
            }

            private function gaa():void {
                //How to access addText() in Games.as?
            }

        ]]>
    </fx:Script>
    <s:Button x="693" y="19" label="Add Text" click="gaa()"/>

</s:WindowedApplication>

Games.as is a package that create sprite:

package 
{
    import scenes.Scene;
    import starling.display.Button;
    import starling.display.Image;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.textures.Texture;

    public class Game extends Sprite
    {
        private var mMainMenu:Sprite;
        private var mCurrentScene:Scene;

        public function Game()
        {
            var bg:Image = new Image(Assets.getTexture("Background"));
            addChild(bg);

            mMainMenu = new Sprite();   //create new sprite
            addChild(mMainMenu);

        }
        public function addText():void {
            var logo:Image = new Image(Assets.getTexture("Logo"));  //add logo
            logo.x = int((300 - logo.width) / 2);
            logo.y = 50;
            mMainMenu.addChild(logo);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-12-15 22:41:37

如何在不创建新实例的情况下调用 addText 函数
游戏?

您需要使用静态方法来调用类上的方法,而不创建它的实例。像这样的:

    public static function addText():void {
        var logo:Image = new Image(Assets.getTexture("Logo"));  //add logo
        logo.x = int((300 - logo.width) / 2);
        logo.y = 50;
        mMainMenu.addChild(logo);
    }

然后你可以这样调用该方法:

Games.addText()

当然,该方法,如所写,会抛出一个错误;因为方法中没有定义mMainMenu。您将无法访问静态方法内的类的实例变量。

how do I call a addText function without create a new instance of
Game?

You need to use static methods to call a method on a class without creating an instance of it. Something like this:

    public static function addText():void {
        var logo:Image = new Image(Assets.getTexture("Logo"));  //add logo
        logo.x = int((300 - logo.width) / 2);
        logo.y = 50;
        mMainMenu.addChild(logo);
    }

Then you can call the method like this:

Games.addText()

Of course, the method, as written, will throw an error; because mMainMenu is not defined in the method. You won't be able to access instance variables on a class inside a static method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文