Copy link to clipboard
Copied
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
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Thank you very much! That works perfectly.
Much appreciated.
Brendan
Copy link to clipboard
Copied
You're welcome Brendan
Find more inspiration, events, and resources on the new Adobe Community
Explore Now