Search code examples
actionscript-3actionscriptinclude

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?


Solution

  • In ActionScript, you can define an Interface, which essentially says "these are the methods and properties that this type of object will always have." Interfaces are great for allowing you to create really flexible implementations without the code that uses the implementations having to know about the internals. One advantage of not having to know about the internals is that that knowledge does not need to be compiled into the calling code.

    If it makes sense for your application to load code that's compiled, using Interfaces really frees you up to do whatever you want in the implementation and have as many of them as you need down the road. Here's an answer where I talk more about this.