简体   繁体   English

调用AS3包中的函数

[英]Call a function in AS3 package

Below is a MXML code built from Flash Builder 4.5 with AIR SDK 3.0. 下面是从Flash Builder 4.5和AIR SDK 3.0构建的MXML代码。 Using Starling framework to create 2D animation and wonder how do I call a addText function without create a new instance of Game ? 使用Starling框架创建2D动画,想知道如何在不创建Game新实例的情况下调用addText函数?

main.mxml is a main application: 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 is a package that create sprite: 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);
        }
    }
}

how do I call a addText function without create a new instance of Game? 如何在不创建Game新实例的情况下调用addText函数?

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. 因为在方法中未定义mMainMenu You won't be able to access instance variables on a class inside a static method. 您将无法在静态方法内的类上访问实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM