# But how do I load the NEXT swf? Help I'm an AS2 hack trying to learn this stuff!

**URL:** <https://forum.kirupa.com/t/but-how-do-i-load-the-next-swf-help-im-an-as2-hack-trying-to-learn-this-stuff/300238>\
**Category:** flash\
**Created:** [November 18, 2009, 2:37am UTC](https://forum.kirupa.com/t/but-how-do-i-load-the-next-swf-help-im-an-as2-hack-trying-to-learn-this-stuff/300238 "2009-11-18T02:37:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AS2\_hack](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@AS2\_hack](https://forum.kirupa.com/u/AS2_hack)\
**Post date:** [November 18, 2009, 2:37am UTC](https://forum.kirupa.com/t/but-how-do-i-load-the-next-swf-help-im-an-as2-hack-trying-to-learn-this-stuff/300238/1 "2009-11-18T02:37:43Z")

</div>

I get how to load one and only one swf, but how do I load the next?

I’m trying to code something like this.

1. Click nav button \> preload new section \> play in new section
2. Click **another** nav button \> play out old section \> preload new section \> play in new section

Seems simple enough but I’m a big steaming pile of fail. Here’s what I have. I’m throwing out errors and it’s just not pretty. I’ve learned AS twice now. This is my attempt to learn it for the third time. Brace yourself.

```auto
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;

// section names in the order that they appear on the nav.
// -------------------------------------------------------------------------------
var sectionArray:Array = new Array(null,"homepage", "photos");
// -------------------------------------------------------------------------------

var firstClick:Boolean = true;
var loader:Loader = new Loader();

function loadSection(num:Number):void {
	
	

	var baseURL:String = "http://www.domain.com/";	// just for testing right now
	var fileToLoad:String = baseURL + sectionArray[num] + ".swf"; // format the name of the section into a path
	
	
	if (firstClick) { // this is the first click
		
		firstClick = false;
		
		addChild(loader);
   	 	loader.load(new URLRequest(fileToLoad));
		
		loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
		
		function loadProgress(event:ProgressEvent):void {
			var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
			percentLoaded = Math.round(percentLoaded * 100);
			trace("Loading: "+percentLoaded+"%");
		}
		
		function loadComplete(event:Event):void {
			
			trace("Loading Complete :" + fileToLoad);
			loader.content.initIn(); // play the loaded content in
	
		}
		
	} else { // this is the second (or more) click
	
		loader.content.initOut(); // play OLD content out
		
		addEventListener(Event.ENTER_FRAME,checkPlayOutOfSWF);  
		
		function checkPlayOutOfSWF(event:Event) {  
			
			// watch the content and check to see when it hits the last frame on the timeline
   			if (loader.content.currentFrame == loader.content.totalFrames) {
				
				
				// when the old centent plays out start the preloader for the NEW content
   	 			loader.load(new URLRequest(fileToLoad));
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
				
				function loadProgress(event:ProgressEvent):void {
					var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
					percentLoaded = Math.round(percentLoaded * 100);
					trace("Loading: "+percentLoaded+"%");
				}
				
				function loadComplete(event:Event):void {
					
					trace("Loading Complete :" + fileToLoad);
					loader.content.initIn(); // play the NEW content in
					removeEventListener(Event.ENTER_FRAME, checkPlayOutOfSWF);
			
				}
		
				
			} 

			
		}
		
		
	}
	
}

```

I’ve seen a thousand threads about “how do I preload a file.” But I’ve never seen one that takes into account that a site will ultimately have multiple files (sections).

Can you help?
