Ignas
Ignas

Reputation: 1965

Interpreting String as function name - ActionScript 3.0

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!

Upvotes: 0

Views: 576

Answers (2)

Nek
Nek

Reputation: 41

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

Upvotes: 1

user677526
user677526

Reputation:

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.

Upvotes: 3

Related Questions