Peter Elst

Flash Platform Consultant
  • Home
  • About me
  • Articles
  • Downloads
  • Contact me

Creating levels through ActionScript

28 11 2003

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);
}

« Drivers license Flash MX 2004 developer certification »



Actions

Informations

  • Date : 28 November 2003
  • Categories : Flash

9 responses to “Creating levels through ActionScript”

28 11 2003
stef (12:18:39) :

Weird… that level doesn’t appear in the debugger…

And it can be access from any clip like a real level…

Funny…

28 11 2003
eokyere (14:10:23) :

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

28 11 2003
Peter Elst (14:17:31) :

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.

28 11 2003
eokyere (19:07:02) :

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.

28 11 2003
Peter Elst (19:12:52) :

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.

28 11 2003
eokyere (22:39:33) :

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?

28 11 2003
eokyere (22:46:53) :

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

28 11 2003
Peter Elst (23:41:16) :

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 :)

29 11 2003
Matthias Kannengiesser (01:11:46) :

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+”





Training Partners

multimediacollege

SkillsMatter

  • Categories

    • AIR RSS category feed
    • Ajax RSS category feed
    • CSS RSS category feed
    • Eclipse RSS category feed
    • Events RSS category feed
    • Flash RSS category feed
    • Flash Lite RSS category feed
    • Flex RSS category feed
    • Gadgets RSS category feed
    • General RSS category feed
    • JSFL RSS category feed
    • Mac RSS category feed
    • Open Source RSS category feed
    • PHP RSS category feed
    • Podcasts RSS category feed
    • Publications RSS category feed
    • Rants RSS category feed
    • Reviews RSS category feed
    • Thought of the Day RSS category feed
    • Training RSS category feed
    • Video RSS category feed
  • Resources

    • Jonathan Kaye
    • Edwin van Rijkom
    • LordAlex Leon
    • Peldi Guilizzoni
    • Lee Brimelow
    • Rich Tretola
    • AndrĂ© Michelle
    • Ted Patrick
    • Matthew David
    • Serge Jespers
    • Keith Peters
    • Jesse Warden
    • Sas Jacobs
    • Burak Kalayci
    • Simon Barber
    • Jon Williams
    • Robert M. Hall
    • Brajeshwar Oinam
    • Branden Hall
    • Chafic Kazoun

     
     

    Adobe Community Expert

    See my profile on LinkedIn



    Harz Ferienwohnung Suchmaschinenoptimierung Geschenkideen Harz Ferienwohnung Pagerank Webkatalog Webhosting