将字符串解释为函数名称 - ActionScript 3.0

发布于 2024-12-13 09:23:50 字数 1449 浏览 0 评论 0原文

我正在为大学作业开发一个游戏,我想创建一个类来处理 fla 文件库中的所有资源。我已经使用 OO PHP 开发网站近 4 年了,所以我有编程经验,但我对 AS3 很陌生。

资产处理程序类:

package library {
    public class AssetHandler {
        public var stageWidth: int;
        public var stageHeight: int;

        public function AssetHandler(sw:int, sh:int):void {
            stageWidth = sw;
            stageHeight = sh;
        }
        //Convert asset to bitmap
        public function bm(AssetsName:String):Object {
            var a:Object = new AssetsName(stageWidth, stageHeight);
            return new Bitmap(a);
        }

    }
}

以及 Main.fla 中引用的主类

package {
    import flash.display.*;
    import library.AssetHandler;

    public class Main extends Sprite {
        private var cannon:Cannon = new Cannon();
        private var holder:Holder = new Holder();
            //I want to replace this
        public var bdata = new Char(stage.stageWidth, stage.stageHeight);
        public var char = new Bitmap(bdata);
            //into this
            public var asset = new AssetHandler(stage.stageWidth, stage.stageHeight);
            public var char = asset.bm("Char");

        private var cannonAngle:Number;

        public function Main() {
                }
        }
}

在 php 中,您可以轻松执行 $Class->$name(); AS3中有类似的方法吗?

我收到错误: 1180:调用可能未定义的方法 AssetsName。 1180:调用可能未定义的方法 Bitmap。

我想由于可见性而找不到 Bitmap 方法,但我该如何修复它? 谢谢!

I'm working on a game for a university assignment and I want to create a class that handles all the assets from the fla file's library. I've been developing websites with OO PHP for almost 4 years so I have programming experience, but I'm new to AS3.

The Asset handler class:

package library {
    public class AssetHandler {
        public var stageWidth: int;
        public var stageHeight: int;

        public function AssetHandler(sw:int, sh:int):void {
            stageWidth = sw;
            stageHeight = sh;
        }
        //Convert asset to bitmap
        public function bm(AssetsName:String):Object {
            var a:Object = new AssetsName(stageWidth, stageHeight);
            return new Bitmap(a);
        }

    }
}

And the main class that is referenced in Main.fla

package {
    import flash.display.*;
    import library.AssetHandler;

    public class Main extends Sprite {
        private var cannon:Cannon = new Cannon();
        private var holder:Holder = new Holder();
            //I want to replace this
        public var bdata = new Char(stage.stageWidth, stage.stageHeight);
        public var char = new Bitmap(bdata);
            //into this
            public var asset = new AssetHandler(stage.stageWidth, stage.stageHeight);
            public var char = asset.bm("Char");

        private var cannonAngle:Number;

        public function Main() {
                }
        }
}

In php you can easily do $Class->$name();
Is there a similar approach in AS3?

I get the errors:
1180: Call to a possibly undefined method AssetsName.
1180: Call to a possibly undefined method Bitmap.

I imagine that the Bitmap method is not found due to visibility, but how do i fix it?
Thanks!

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

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

发布评论

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

评论(2

合久必婚 2024-12-20 09:23:50
1180: Call to a possibly undefined method Bitmap.

您需要导入flash.display.Bitmap

1180: Call to a possibly undefined method AssetsName.

如果我没记错的话,您实际上无法像您所做的那样从 String 引用实例化一个类。但是,您可以执行 getDefinitionByName 使用字符串引用,并以这种方式实例化一个类。

public function bm(AssetsName:String):Object {
    var classdef:Class = getDefinitionByName(AssetsName) as Class;
    var a:Object = new classdef(stageWidth, stageHeight);
    return new Bitmap(a);
}

(我认为这是正确的。可能有一些错误。)(编辑:有 - 错过了“as Class”部分。)

祝你好运。

1180: Call to a possibly undefined method Bitmap.

You'll need to import flash.display.Bitmap.

1180: Call to a possibly undefined method AssetsName.

If I remember correctly, you can't actually instantiate a class from a String reference like you're doing. You can, however, do a getDefinitionByName using the string reference, and instantiate a class that way.

public function bm(AssetsName:String):Object {
    var classdef:Class = getDefinitionByName(AssetsName) as Class;
    var a:Object = new classdef(stageWidth, stageHeight);
    return new Bitmap(a);
}

(I think that's correct. There may be some errors.) (Edit: There was - missed the "as Class" part.)

Good luck.

太阳男子 2024-12-20 09:23:50

我认为你需要这样的东西

如果你需要通过字符串获取类的引用:

import flash.utils.getDefinitionByName;
.....
var ClassReference:Class = getDefinitionByName("YourClass") as Class;

如果你想通过字符串调用函数:

var functionName:String = "testFunction";
this[functionName]("hello!");

public function testFunction(param:String):void {
    trace(param);
}

希望这对你有帮助。

更新

PHP's $Class->$name() may be assumed as classInstance["name"](); in ActionScript

I think you need something like this

If you need get reference of class by string:

import flash.utils.getDefinitionByName;
.....
var ClassReference:Class = getDefinitionByName("YourClass") as Class;

If you want call function by string:

var functionName:String = "testFunction";
this[functionName]("hello!");

public function testFunction(param:String):void {
    trace(param);
}

Hope this help you.

UPDATE

PHP's $Class->$name() may be assumed as classInstance["name"](); in ActionScript
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文