Creating levels through ActionScript
28 11 2003I’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);
}




Weird… that level doesn’t appear in the debugger…
And it can be access from any clip like a real level…
Funny…
omission… [cb]() shd be either
this[cb]() or eval(cb)()… pretty wierd code… it appears all you are really doing is creating a pointer to _level0… but pretty wierd code
Thanks for that, I’ve just fixed the typo!
It’s not a reference pointer in the sense that all variables and functions from _level0 are copied into the new level but from that point on they function independently.
yes… i realized that too when i tested it… but it isn’t anything different from loading “foo.swf” into _levelBar, as long as “foo.swf” exists. In this case, u are sure _url (meaning this._url) exists, otherwise why woudl the swf be on in the first place ;P… what is interesting is it is listed as _levelX (_level2 in your case) but a trace of (_levelX._level, again _level2._level in your case) returns _level0… on a more serious note though, i don’t think this is by design… the flash player shouldn’t allow this type of behavior… unless it can be put to some good use? maybe others might have other views on this… and yes, it happens in player 7 too.
True, though it is different than loading any other external SWF into a level because the movieclip assets are not copied in the new level and events do not function.
You are correct in the fact that the newly created level still considers itself to be _level0, if you run _level2.createLevel it gets stuck in an infinite loop.
Another strange thing is that I needed to add the conditional != undefined after the load process, it initially returns undefined but the next iteration of the script returns a pointer to the new level.
i would think (and i’m only speculating) the assets are not loaded into the new level because initially (or when loading), the movie still points to _level0 as it’s level. If that is the case, then all assets (objects) loaded in the new movie (_level2 as in your example) would just be references to the original assets (objects) already at _level0… that is to say, _level2 is _level0 until _level2 is finally loaded and becomes its own level. I would think that is why it has references to the objects in _level0 and then breaks away on its own… thus objects created on it after it’s loaded are not ‘back-refrenceable’ at _level0 (as _level0.fooObjs)… u get my drift?
about the conditional != undefined, that is just how it should be. the first condition… if (loadLevel.getBytesLoaded()>=loadLevel.getBytesTotal())… should decompose to… if (undefined >= undefined)–which is always true– when loadLevel (_level2) has not started loading… and that would be the situation for a couple of _level0.enterFrames… so that condition is necessary.
btw… nice blog u have here
Some great points there eokyere, I’ll certainly look into those! Love to hear if anyone has any other speculations as to what happens or perhaps even an idea if it could be useful for anything at all
Hi Peter,
I noticed that nice germ in June. Try this and you will see it even works with onEnterFrame, functions and more.
loadMovie(”nofile”, “_level100″);
this.onEnterFrame = function() {
if (_level100) {
(trace(_level100+”