website promotion banner
eturnkeys
Your Ad Here
Flash & Swish  Home Flash & Swish Flash Tutorials Using LoadMovie
rss

Using LoadMovie

Author: MicrOchip More by this author


In this tutorial we're going to take a look at the function loadMovie. LoadMovie is a very nifty function, though things can get in a huge mess quickly. You often get questions on the forum about this topic, so that's why this tutorial has been created. In this tutorial we're not only going to take a look at the theory of loadMovie, we're also going to create a picture loader. It will consist of a few buttons, and when the user clicks one of these, the corresponding picture is loaded.

What exactly does loadMovie do?

LoadMovie is capeble of loading jpeg images and swf movies. In our example we are going to load swf's, but loading jpegs works exactly the same.

Preparing the fla

Ok, let's start the work. The fla is basicly just an example, so all we need is a few buttons and a MovieClip. The MovieClip is supposed to be empty, so delete all the content you may have put in it. Now place it where you want the picture to load. Don't forget to give it the instance name load_mc. Download partial source

ActionScript

Now with the fla prepared, we can start on the ActionScript. It's really easy to load a movie. It uses the loadMovie() function. The only parameter it needs is the url to the movie it needs to load. This can be a relative url (so like only the last folder and filename) or a absolute url (http://). So if we have four buttons, that load the swfs swf1 to swf4, all we have to do is use this code:

image 1

on (release) {
_root.load_mc.loadMovie("swf1.swf");
}

All you have to do is change swf1 into swf2 etc. Download partial source

Dangers of loadMovie

LoadMovie has some problems attached to it. If you're just loading a piece of text in a swf, not much can go wrong. But when loading whole parts of like a site, things can get messy. The first problem you can encounter is that if you use _root.gotoAndPlay() in the loaded movie, the main movie will change frame. This is because when you load a swf in a MovieClip, _root doesn't refer to the _root of the swf that is loaded, but it referres to the _root of the swf that has loaded the file. There are a few ways of taking care of this problem. The first is just not to use _root. You can use this and _parent instead. The second, is to use _lockroot. This property sets what _root referres to. So if you set _lockroot in a Movieclip to true, _root in all it's child MovieClips will refer to that MovieClip. So if you set _lockroot to true in _root, then that MovieClip (dont forget: _root is also a MovieClip) will allways be _root, no matter what happens. This will only work in Flash Player 7 or higer.

Some nifty code

A frequently asked question at the forums is how to make a preloader when using loadMovie. This is allmost the same as when making a preloader for the _root, only now you target it to load_mc instead of _root. So let's make a swf loader with loadbar. First put a MovieClip that contains a bar in the _root. Give it the instance name bar_mc. Now put this code in the _root:

function loadSwf(path) {
_root.load_mc.loadMovie(path);
_root.onEnterFrame = function() {
var total = _root.load_mc.getBytesTotal();
var loaded = _root.load_mc.getBytesLoaded();
var pr = loaded/total*100;
_root.bar_mc._xscale = pr;
if (pr == 100) {
delete _root.onEnterFrame;
}
}
}

That's it :)

image 2

Click here to download all the source. If you have any questions, comments, praise or critisism: send me a Private Mail or ask away on the forums



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Using LoadMovie"