Some1
Some1

Reputation: 67

AS3 | How to use var for name?

My code is:

for (var i:int=1; i <= 12; i++) {
    MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}

function chooseTool(e:MouseEvent):void {
    Mouse.hide();

    var cursor:MovieClip = new "here is e.target.name"();
    addChild(cursor);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}

function follow(e:MouseEvent):void {
    cursor.x=mouseX;
    cursor.y=mouseY;
}

How to use "e.target.name" for name ? Should become something like:
1) Click on button with name mouse_4
2) Function chooseTool create new MovieClip with name mouse_4();
example: var cursor:MovieClip = new mouse_4();

Upvotes: 0

Views: 98

Answers (3)

Mowday
Mowday

Reputation: 412

You can do it by using getDefinitionByName (Adobe documentation: getDefinitionByName)

for (var i:int=1; i <= 12; i++) {
    MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}

function chooseTool(e:MouseEvent):void {
    Mouse.hide();

    var o:Object = getDefinitionByName(e.target.name);
        // ^ Gets a reference to the class object based on a string

    if(o == null) throw new Error("Unable to find class " + e.target.name);

    var c:Class = o as Class;
    var cursor:MovieClip = new c() as MovieClip;
    addChild(cursor);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}

function follow(e:MouseEvent):void {
    cursor.x=mouseX;
    cursor.y=mouseY;
}

But this feels very ill structured way of doing it. Instead I would use a factory method (Factory pattern on Wikipedia)

for (var i:int=1; i <= 12; i++) {
    MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}

function chooseTool(e:MouseEvent):void {
    Mouse.hide();

    var cursor:MovieClip = getCursor(e.target);
    addChild(cursor);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}

function follow(e:MouseEvent):void {
    cursor.x=mouseX;
    cursor.y=mouseY;
}

function getCursor(target:MovieClip):MovieClip
{
    switch(target.name)
    {
        case "cursor_4":
            return new cursor_4();

        default:
            return new default_mouse();
    }
}

Upvotes: 1

Kaif
Kaif

Reputation: 131

You can't Instantiate like this new "e.target.name"(); But if you want to take the reference of target,you can try this.

var cursor:MovieClip =e.target as MovieClip;

you cannot modify the name property of cursor.

Upvotes: 1

simion314
simion314

Reputation: 1394

You can't do it like that. That is not realy a name would be a new class/type. The solution is to extend the MovieClip and pass that name as a parameter . I do not know what you want to achieve but this is a good way to start.

class MyClip extends MovieClip 
{
    public var name:String="";
    public function MyClip(name:String) 
    {
        super();
    this.name=name;
    }
} 
}

The code will look something like that and you can add all the logic related with this type inside this class.

Upvotes: 1

Related Questions