Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Instantiate a class from a dynamic variable

New Here ,
Apr 12, 2015 Apr 12, 2015

Hi everyone. I am just working on a little tilebased project, and I would like to be able to place any of my tile objects with one bit of code.

Right now I've just got this in my left click function:

__________________________________

if(tileType == "grass"){

    var grasstile:grass = new grass;

    addObj(grasstile, absMX, absMY);

}else if(tileType == "dirt"){

    var dirttile:dirt = new dirt;

    addObj(dirttile, absMX, absMY);

}

__________________________________

Where tileType is a variable that changes depending on what tile the user has select from the menu, and addObj is a custom function for adding objects.

However there are many tiles, and I'd much prefer to be able to use one bit of generation code rather than duplicating it over and over again for every tile.

Is there a way to do this?

Thank you very much for your time.

Brendan

TOPICS
ActionScript
281
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 13, 2015 Apr 13, 2015

I think the following sample will answer that as far as using a string to dynamically define a new class object, just translate it to your situation...

var ClassRef:Class = Class(getDefinitionByName("className"));

var classInstance:* = new ClassRef();

addChild(classInstance);

Translate
LEGEND ,
Apr 13, 2015 Apr 13, 2015

Why do you have to create new code for each tile?  What about the code you show makes it unique to each tile?  If there is something unique to each tile then that is the portion of the code that you need to focus on making generic so that it can be applied for any tile.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2015 Apr 13, 2015

Sorry, maybe I didn't explain the issue properly. Each tile is unique in that it has a different graphic and properties corresponding to what it is: grass, dirt, wood, etc. Each one with properties like friction, breakable, movable, etc.

Right now I am using the code as such:

if(tileType == "grass"){

    var grasstile:grass = new grass;

    addObj(grasstile, absMX, absMY);

}else if(tileType == "dirt"){

    var dirttile:dirt = new dirt;

    addObj(dirttile, absMX, absMY);

}else if(tileType == "wood"){

    var woodtile:wood = new wood;

    addObj(woodtile, absMX, absMY);

}else if(tileType == "sand"){

    var sandtile:sand = new sand;

    addObj(sandtile, absMX, absMY);

}

...etc


If I do this for each tile, it's going to be a huge list, and it seems like a very ineffiecnt way of doing things. I was hoping for something more like this:


var tileType:String = "grass";

var [tileType+"tile"]:tileType = new tileType;

addObj([tileType+"tile"], absMX, absMY);

That way I can just change the tileType variable and generate each tile with a single bit of code, rather than duplicating it over and over.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2015 Apr 13, 2015

I think the following sample will answer that as far as using a string to dynamically define a new class object, just translate it to your situation...

var ClassRef:Class = Class(getDefinitionByName("className"));

var classInstance:* = new ClassRef();

addChild(classInstance);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2015 Apr 13, 2015

Thank you very much! That works perfectly.

Much appreciated.

Brendan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2015 Apr 13, 2015
LATEST

You're welcome Brendan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines