|
||||||
|
||||||
Flash Tutorial - Communication between Flash Movies: Part 1

Free Flash Tutorial
![]()
This tutorial shows you how to send information or instructions from one Flash Move to another on the same web page. There is theoretically no limit to what information can be sent between each Flash Movie. You can control and communicate between any two Flash Movies on the same page or on different pages. It is also be possible to communicate between more than two Flash Movies.
Note: There is a second part to this tutorial which deals with how to send information from Flash Movies that are on different web pages or Pop Up Windows: Part 2
Example:
Download the Flash file
Int 121a
| Movie 1 This Movie controls Movie 2 ->> |
Movie 2 This Movie is Controlled by Movie 1 <<- |
The above movies are what I will show you how to create in this
part of the tutorial. The blue Movie on the right has 50 frames and simply
plays automatically. It is the pink Movie that controls the blue Movie.
Example: Download the Flash file Int 121b
| Movie 3 This Movie controls Movie 4 ->> |
Movie 4 This Movie controls Movie 3 <<- |
Example: Download the Flash file Int 22a
Example of opening and closing Pop Up Windows.
Note: This last example is explained in the next tutorial. Opening and closing Pop Up Windows involves JavaScript so it has been separated from this tutorial. This tutorial only uses ActionScript.
Step One: Creating the Controller Movie
This is the Movie that you will use to control the other Movies.

on (release) {
_root.mySender.send("myConnections", "myAction", 2);
}
on (release) {
_root.mySender.send("myConnections", "myAction", 1);
}
Note: The only difference between the two bits of code is the number at the end of the second line. In one case it is a number: 1 and in the other it is a number: 2
The code above sends the number to a function in the other Movie. The number you send, will determine what the function will do. In this case either Play or Stop. It is possible to have additional buttons with additional numbers. Each individual command or set of 'Actions' would need its own unique number.
mySender = new LocalConnection();
Note: This sets up the variable: mySender
Step Two: Creating the Second Flash Movie
Note: Do not use Instance Name, it is not the same as a variable name.

Your Property Inspector should look similar to this.
onEnterFrame = function () {
_root.displayNumber = _root._currentframe;
};
myReceiver = new LocalConnection();
myReceiver.connect("myConnections");
myReceiver.myAction = function(doThis) {
if (doThis == 1) {
play();
} else if (doThis == 2) {
stop();
}
};
The ActionScript Explained
The code below displays the current frame number. It sends the frame number to the Dynamic Text Box that is on stage. The only reason for this is so that you can see if the Movie is Playing or not.
onEnterFrame = function () {
_root.displayNumber = _root._currentframe;
};
It is the function below that does all the work. If Movie 1 sends a parameter (the numbers 1 or 2) it does the appropriate action. In this case either: play or stop
myReceiver = new LocalConnection();
myReceiver.connect("myConnections");
myReceiver.myAction = function(doThis) {
if (doThis == 1) {
play();
} else if (doThis == 2) {
stop();
}
};
If you wanted additional actions you would add the extra action like this:
myReceiver = new LocalConnection();
myReceiver.connect("myConnections");
myReceiver.myAction = function(doThis) {
if (doThis == 1) {
play();
} else if (doThis == 2) {
stop();
} else if (doThis == 3) {
do some other action;
}
};
If you wanted two way communication between Flash Movies like in the second example (see example: Int121b) at the top of this page, you need to edit the code. Each time you use the code you would need to change all the variable names otherwise chaos will ensue! You would do this like this:
//code coming from Movie 1
myReceiver_1 = new LocalConnection();
myReceiver_1.connect("myConnection_1");
myReceiver_1.doAction_1 = function(doThat_1) {
if (doThat_1 == 1) {
play();
} else if (doThat_1 == 2) {
stop();
}
};
//code going to Movie 2
mysender_2 = new LocalConnection();
If you find this confusing download the example files Int121b and look how the variable names have been changed. Remember to look in both the buttons and the Timeline: Download Flash Samples
Step Three: Creating the Web Page
To test your Movies you need to place the swf files that you created onto a web page.
The potential for individual Flash movies interacting with one another is endless. This very simple code opens up the interaction of Flash Movies to a completely different level. It is highly adaptable and versatile and is simple to use. It seems to work no matter where you put the flash movies.
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|