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!
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
getDefinitionByNameusing 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.