Hi, i'm having a problem with my flash file but i'm not sure what it is that i'm doing wrong.

The site starts with a preloader and loads up a simple shell, then once thats loaded i want it to dynamically load the home.swf into an empty movieclip, only it doesn't, but when i click on any of the links the .swf's will load into the blank movieclip just fine, so i know they work alright.

Here's the actionscript for the first frame:

stop();

myInterval = setInterval(preloader,10);
function preloader(){
	if(getBytesLoaded()>=getBytesTotal()){
		play();
		clearInterval(myInterval);
	}
	preloaderText.text=Math.round(getBytesLoaded()/getBytesTotal()*100)+"%"
	preLoadBar.mask._yscale=(getBytesLoaded()/getBytesTotal()*100);
}

After that's loaded it goes onto the next frame with this actionscript:

stop();

onLoad = function(){
	external_swf_mc.loadMovieNum("home.swf", 0);
}

button_home.onRelease = function(){
	external_swf_mc.loadMovie("home.swf");
}

I've tried using google to find an answer but no luck so far, i tried using onEnterFrame but that seems to break it, does anyone have any ideas?

Cheers.

Dani AI

Generated

Short summary and practical fix:

A common AS2 gotcha caused this: the code on the timeline tried to trigger a load at the wrong time. As discovered, the navigation buttons worked because their click handlers ran after the shell and its target clip existed. The automatic attempt using an onLoad-style approach didn’t reliably fire. The right, reliable tool in Flash 8 / AS2 is MovieClipLoader — it gives onLoadProgress, onLoadComplete and onLoadInit callbacks so you only interact with the child SWF once it’s actually ready.

Why onLoad approaches fail (concise):

  • onLoad is a movie-clip event, not a guaranteed frame initializer; assigning it on the main timeline can miss the moment you expect.
  • loadMovie/loadMovieNum give no guaranteed “initialized and ready” event; code may run before the loaded SWF’s scripts have executed.
  • MovieClipLoader.onLoadInit is the safe place to call functions or manipulate the loaded SWF.

Minimal MovieClipLoader pattern (drop this on the shell frame after the preloader finishes):

var loader:MovieClipLoader = new MovieClipLoader();
var listener:Object = new Object();

listener.onLoadProgress = function(mc:MovieClip, bytesLoaded:Number, bytesTotal:Number){
  var pct = Math.round(bytesLoaded/bytesTotal*100);
  // update progress UI here
};

listener.onLoadInit = function(mc:MovieClip){
  // loaded movie is initialized and safe to call into
  if (typeof mc.init == "function") mc.init();
};

loader.addListener(listener);
loader.loadClip("home.swf", external_swf_mc);

Quick troubleshooting tips: make sure the target clip (external_swf_mc) exists (stage instance or createEmptyMovieClip) before loadClip; if the child SWF references _root, set _lockroot = true in the child or avoid root-dependent code; prefer onLoadInit for communication with the child (not onLoadComplete) so you don’t hit timing issues. As noted, switching to MovieClipLoader resolves the problem in most cases.

Recommended Answers

All 3 Replies

Sorry if this is a "duh" question, but why do you have an onload script AND a button doing the same thing?

Sorry if this is a "duh" question, but why do you have an onload script AND a button doing the same thing?

I shortened that second bit of actionscript as i didn't think the rest was important, the button is one of five that load in the other pages (external swf's) of the site into the shell, home, classes, contact, etc.

But i want the home page to load up automatically once the preloader has loaded in the shell, otherwise you have a shell with no content, which looks a little boring and causes the site to look broken.

Anyway, it doesn't matter now, someone over at actionscript.org sent me a link to a tutorial on kirupa.com that solved the problem, i needed to use MovieClipLoader instead of onLoad, which was a huge relief, i was tearing my hair out here trying to get it to work!

Cheers anyway, peace :)

Great! Thanks for sharing the solution here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.