Checking File Availability Before Loading in ActionScript
When building a Flash preloader or any dynamic media sequence, skipping a missing asset keeps the user experience smooth. If a .jpg, .swf or even a text file isn’t available, the next frame can’t render and the whole chain stalls. Rather than let the onLoad error bubble up, you can query the server for the file first. ActionScript offers a simple way to probe a file’s presence with the LoadVars class and its onLoad callback. This method works for any URL - images, SWFs, XML, or plain text - because the request is a generic HTTP GET. If the file returns a 404, the callback receives false and you can immediately advance to the next item in your cue.
The trick is that the traditional MovieClip.onLoad event doesn’t fire for external assets; it only signals when the main SWF finishes loading. By using LoadVars you trigger a lightweight HTTP request that only asks the server if the file exists. No data is transferred unless the file is present, so the cost is negligible.
Below is a step‑by‑step illustration that checks for an SWF file and loads it into an empty clip when found. The same pattern applies to any other asset type.
Start by creating a LoadVars object. Keep a reference to the caller so that the callback can create a clip in the right scope. The example uses this as the parent, but you can replace it with any display object.
Define the onLoad function, which receives a boolean indicating whether the request succeeded. If the file is present, success is true; otherwise it’s false. Inside the success branch you create an empty clip and load the target asset into it. On failure, you simply skip to the next asset in your queue.
Finally, call load() on the LoadVars object with the asset URL. This sends the HTTP request. The callback will fire once the server responds. If you’re running the movie in a browser, the file will first be cached. On the next load attempt, the browser delivers it instantly, so the preloader doesn’t wait.
Because LoadVars doesn’t parse the file’s content, the request remains lightweight even for large SWFs or images. The network overhead is minimal, and the method works across Flash Player versions that support LoadVars (ActionScript 2 and 3 via URLLoader).
Images, XML, and text files follow the same logic. Replace the loadMovie call with the appropriate loader: MovieClip.loadMovie for images, Loader.load for binary assets, or URLLoader.load in AS3. The onLoad flag still indicates existence, so you can guard the rest of your loading routine behind it.
For example, to load a JPEG and skip it if missing:
Sometimes the server may be unreachable or respond with a status code other than 404. In these cases, onLoad will still report false. To avoid a silent failure, log the error or display a placeholder. You can use the URLLoader error events in AS3 for finer control, but the LoadVars pattern remains the simplest for quick checks.
Because the request completes before you try to use the asset, the user never sees a broken link. The preloader can continue to the next item instantly, maintaining a fluid experience.
Each LoadVars.load() call initiates a round‑trip to the server. For large asset libraries, batch your checks or rely on the browser cache. If you know that certain assets are mandatory, skip the existence check and handle failures at runtime. The cost‑benefit trade‑off depends on your application’s tolerance for missing files.
For high‑traffic sites, consider a lightweight server‑side endpoint that returns a simple 200 OK or 404 Not Found for asset paths. That keeps the HTTP response tiny and reduces bandwidth.
Imagine a slideshow that pulls images from a CDN. Before displaying each image, you run a LoadVars test. If the image exists, you load it into the slide container. If not, you fetch the next one. This guarantees that the slideshow never stalls on a missing file and automatically replaces broken links without user intervention.
Because the existence check is asynchronous, you can queue multiple tests in parallel, limiting the number of simultaneous requests to avoid overloading the server. Once all tests resolve, your preloader can begin rendering the ready assets.
Using this technique keeps your Flash application robust, responsive, and ready for the unpredictable nature of web resources. Guy Watson, known as FlashGuru, pioneered many best practices for asset loading and preloading in Flash. His tutorials and community work have helped developers create smoother, more reliable Flash experiences. For deeper insights into Flash preloading strategies, visit
Tags





No comments yet. Be the first to comment!