Fenrir370 0 Newbie Poster

so i have a class assignment to link all our game files, im trying to load a "lose.swf" after the car crashes. our teacher only gave us this line of code to use and i cant seem to figure out how to get the MovieClip(stage.getChildAt(0)).loadThisScreen("lose.swf");
part to work properly ( we did not learn flash coding we just learned to animation part ) but from my c++ class i can kinda understand whats going on.

import ash.net.URLRequest;
// Creating the loader ----------------------------------------------------/
var loader:Loader = new Loader();
loader.load(new URLRequest("homeScreen.swf")); // adding your homeScreen
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
// Function that loops variables for percent loaded text and scaling the progress bar -----------------------------------/
function loop(e:ProgressEvent):void
{
var perc:Number = e.bytesLoaded / e.bytesTotal;
var s:String = Math.ceil(perc*100).toString()+"%";
percent_txt.text = s;
bar_mc.scaleX = perc;
}
//Unloading the loader and removing the pre-loader elements --------------/
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
function done(e:Event):void
{
removeChildAt(getChildIndex(percent_txt));
removeChildAt(getChildIndex(gear_mc));
removeChildAt(getChildIndex(bar_mc));
// nally, add the loader to the stage to display it ------------------------------------/
stage.addChild(loader);
}
function loadThisScreen(screen:String):void
{
 loader.unloadAndStop();
 loader.load(new URLRequest(screen));
 addChild(percent_txt);
 addChild(bar_mc);
}
// Code that goes on the end of the crash frame ------------------------------------/
MovieClip(stage.getChildAt(0)).loadThisScreen("lose.swf");
// Code that goes on the last frame of the main timeline when you win ------------------------------------/
MovieClip(stage.getChildAt(0)).loadThisScreen("win.swf");

Dani AI

Generated

— The call using stage.getChildAt(0) is brittle: that index often isn't the MovieClip that defines your loading function, so the loaded game SWF can't reliably find and call it. Loaded SWFs run as children of the Loader you added to stage, and the display-list indices and parent chain differ from what you expect. Cross-SWF calls also fail if the sandboxes/domains differ.

Two safer approaches:

  • Use a bubbling event (recommended). From the game SWF dispatch a simple event when the car crashes; let the main movie listen on stage and perform the actual load. This avoids fragile indexing and keeps responsibilities separate.
// in the loaded game (crash frame)
dispatchEvent(new Event("showLose", true));
// in the main/preloader movie (run once during init)
stage.addEventListener("showLose", onShowLose);
function onShowLose(e:Event):void {
    // call your loading routine here (unload old content, load "lose.swf")
}
  • Inject a host reference into the loaded content when you complete the Loader. After the Loader finishes, set a public property on the loaded root pointing back to the main movie; the child can then call that reference directly. This is more tightly coupled but simple.

Troubleshooting tips: add tracing to inspect the display list (print stage.numChildren and stage.getChildAt(i)), confirm the Loader and its content types, and watch for security errors (cross-domain SWFs must be allowed). Prefer the event approach for clarity and reliability.

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.