Vladimir Despotovic Vladimir Despotovic - 1 year ago 88
ActionScript Question

In action script 3, how can I dynamically include a file with certain name, based on a value of another variable?

I have a variable "level", and based on the value of that Number variable, I want a file included, with my level settings for THAT particular level. Level files are called "Level1Definition.as", "Level2Definition.as", "Level3Definition.as", and so on. When I try to form that line of code like this:

include "Level" + level + "Definition.as";

, I get an error:

Expecting semicolon before plus.

How can I include file whose name (name of the file) is formed using a variable, during runtime?

Answer Source

That doesn't work, because the include statement is executed before/during compilation, but the value of your variable determined at runtime.

The core of your problem is that you mix data with logic. You should not have different classes for individual levels. Instead, create one level class that represents a general level object. Create instances of that class and pass the data specific to that level to the object. This data is usually loaded at runtime from a file or server, then deserialized (from json, XML or whatever format is used) before it is used. Hard coding large amounts of data as you do it if not recommended. It also makes it necessary to recompile everything when a level is changed.