I've been playing around with some strange ActionScript behaviour I discovered a while back. In search for the ultimate "infinite loop" I had a SWF load itself into a level and what do you know, with a little bit of tweaking we've created ourselves a new level.

I only tested it on Flash MX for the time being though I have some indications that it also works on MX 2004. It appears that the newly created level can only be used to store variables, things like onEnterFrame don't work on it. Also good to note is that any movieclip assets you might have on _level0 are *not* loaded into this higher level so you don't have duplicate assets.

I have no idea why you would use this and certainly wouldn't recommend it as good coding practice though it's an interesting piece of code to say the least. One question does remain: why does this work?

createLevel = function (level, cb) {
  if (this._level == _level0) {
    loadMovieNum(_url, level);
    this.onEnterFrame = function() {
      var loadLevel = eval("_level"+level);
      if (loadLevel.getBytesLoaded()>=loadLevel.getBytesTotal()) {
        if(loadLevel != undefined) {
          this.onEnterFrame = null;
          this[cb]();
        }
      }
    };
  }
};

createLevel(2,"levelCB");

levelCB = function() {
  _level2.myVariable = "Why does this work?";
  trace(_level2.myVariable);
}
Posted
AuthorPeter
CategoriesFlash