将字符串解释为函数名称 - ActionScript 3.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要导入
flash.display.Bitmap
。如果我没记错的话,您实际上无法像您所做的那样从 String 引用实例化一个类。但是,您可以执行
getDefinitionByName
使用字符串引用,并以这种方式实例化一个类。(我认为这是正确的。可能有一些错误。)(编辑:有 - 错过了“as Class”部分。)
祝你好运。
You'll need to import
flash.display.Bitmap
.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.(I think that's correct. There may be some errors.) (Edit: There was - missed the "as Class" part.)
Good luck.
我认为你需要这样的东西
如果你需要通过字符串获取类的引用:
如果你想通过字符串调用函数:
希望这对你有帮助。
更新
I think you need something like this
If you need get reference of class by string:
If you want call function by string:
Hope this help you.
UPDATE