|
||||||
|
||||||
Tutorial - Communication with Flash
144 Intermediate
Flash Compatibility: MX 2004, MX and Flash 5
Written by: Rabid Lemming
Length: 2,728 Words
Assumed Knowledge: Some knowledge HTML, JavaScript and Flash ActionScript
Level: Medium
Access: Free Tutorial
Note: This tutorial has not yet been edited - so that I cannot be sure it is error free or that the instruction are easy to follow. It will be edited soon. Thanks - Site Editor: Phil Schulz.
The aim of the tutorial is to learn different methods of communication with flash
NOTE: IMPORTANT READ!!! I strongly recommend you read the following related tutorials:
Cross Ref: FS Commands enables the Flash movie to send JavaScript Commands to the web page or the Browser: FS Commands
Cross Ref: Control flash though web links to play sound in flash: Flash Sound on HTML Web Pages
Cross Ref: Learn how to send any information to Flash with JavaScript: Send Information to Flash with JavaScript
Cross Ref: Learn how to send information from one Flash Movie to another part 1: Communication between Flash Movies: Part 1
Cross Ref: Learn how to send information from one Flash Movie to another part 2: Communication between Flash Movies: Part 2
Cross Ref: Learn how to control a Flash Movie using standard HTML links on a web page using JavaScript: Web Page Controls Flash Movie
Cross Ref: Navigate through your flash movie using the browser back and forth history buttons: History buttons used with Flash
Example: Download the Flash file Int 144a
Change the colour of this table by rolling over the colour's on the left |
^^Flash Movie^^ ^^Web Page Table^^
You can communicate between any language like javascript, php, asp, VBScript or etc... In this tutorial you will learn how to create simple Flash buttons that can change the background colour of the table or HTML web page in with our flash movie in. Before going further you have to know that Flash Scripting requires Netscape Navigator 3.x and above (LiveConnect and Java-enabled; Windows 95/98/NT/2000 or Mac OS; ) or Internet Explorer 3.x and above (ActiveX enabled; Windows 95/98/NT/2000 only). Netscape 6 does not support LiveConnect, that's why Flash scripting in Netscape 6 is currently very buggy. But "JavaScript:" in getURL technique doesn't require LiveConnect plug-in, and it works in Netscape 6. Also this technique does not require Active-X, and it works in IE 5 on Mac.
Step one: The Flash
Understanding the code:
In flash you can call the javascript code using the getURL method like so:
getURL("JavaScript:YourJavaScriptFunction(InformationYourWantToPassToTheJavaScriptFunction)"); The JavaScript: tells the browser that its a javascript function your calling The parts in the brackets MyTableCellName,'#FFFFFF' in the flash file are the values your passing to the javascript function. So your saying call a javascript function on this web page by the name of ChangeTableBackgroundColour and pass on the information to that function that the first argument or variable known as MyTableCellName is equal to the name MyTableCellName and the second argument or variable known as TheTableCellColour is equal to #FFFFFF that's what the getURL("JavaScript:ChangeTableBackgroundColour(MyTableCellName,'#FFFFFF')"); in flash is doing. On the web page the function when called by flash say's ok I have to set a table cell background colour called MyTableCellName to the colour of #FFFFFF. That what the function in step 6 above is doing. You can call your table cell anything you like instead of calling it MyTableCellName you could call it any thing you like. i just used MyTableCellName as i hope it makes more sense. You can call any javascript function and pass any information to it. To change the back ground of your web page you can use: //In flash use: on (press) { getURL ("JavaScript:ChangeTheWebPageBackgroundColor('#336600')"); } On your webpage use: <noscript>You need Java Script Support To View This Page</noscript>
<script language="javascript" type="text/javascript">
<!--
function ChangeTheWebPageBackgroundColor(BackgrounColor) {
if (window.document && window.document.bgColor) {
document.bgColor = BackgrounColor;
}
}
//-->
</script> This script works with IE 4.0 and above, Netscape 4.0 and above and also DOM compliant browsers like IE 5.5 and Netscape 6.0.The "document.bgColor" property defines a document's background colour. In this example the background colour of our HTML page. The argument #336600 is a string that can contain either the hexadecimal definition of the color (like "#cccccc") or it's literal description (like "lime"). You can call basic javascript functions with out needing any code on your web page like window pop ups or alerts as flash MX 2004 now fully supports javascript. Examples: //Alert message on (press) { getURL ("JavaScript:alert('This is Flash!')"); } //link to an anchor on a web page on (release) { getURL ("myPage.htm?#bottom"); } //history back and forward buttons on (release) { getURL ("javascript:history.back()"); } on (press) { getURL ("javascript:history.back()"); } Sending variables from flash to a new page through phpExample: Download the Flash file Int 144b Important!!! NOTE: You have to disable pop ups for this example to work!
Step one: The Flash
Receive.php is the name of my PHP document which flash is sending the variables value to. the "new" is the target window, "new" opens a new window. GET specifies the method we're using to send our variables. You can use either GET or POST. Basically GET shows up in the URL line and is easier to debug while POST doesn't and isn't.
Step two: The Flash
Note: You MUST use this code or the it will not work !!
Note: The Flash movie that you created earlier (the swf file) must be in the same location as your web page. If it is in the same location as the web page then you only use the button name. If however the file is in a different location you must include the file path. If you don't know about HTML file paths just make sure your Flash Movie is located in the same folder as your web page.
You can set any variable value in flash though the html code for the flash movie.
Example: Download the Flash file Int 144c
Step One: The Flash
Note: You MUST use this code or the it will not work !!
Note: The Flash movie that you created earlier (the swf file) must be in the same location as your web page. If it is in the same location as the web page then you only use the button name. If however the file is in a different location you must include the file path. If you don't know about HTML file paths just make sure your Flash Movie is located in the same folder as your web page.
You can set multiple variables all at the same time like so:
Example: Download the Flash file Int 144d
Step One: The Flash
Note: You MUST use this code or the it will not work !!
Note: The Flash movie that you created earlier (the swf file) must be in the same location as your web page. If it is in the same location as the web page then you only use the button name. If however the file is in a different location you must include the file path. If you don't know about HTML file paths just make sure your Flash Movie is located in the same folder as your web page.
Understanding the different parts:
TheFirstVariableNameInFlash=InformationToBePassedToThisVariableHere&TheSecondVariableNameInFlash=InformationToBePassedToThisVariableHere&TheThirdVariableNameInFlash=InformationToBePassedToThisVariableHere
TheFirstVariableNameInFlash Is the name of the variable in flash
= Sets the message or values of the variable in flash equal to what ever follows
InformationToBePassedToThisVariableHere Is the information or values you want to set that variable equal to
& This concatenates or in other words adds the next variable in flash.
Example: Download the Flash file Int 144e
Step One: The Flash
Note: You MUST use this code or the it will not work !!
Note: The Flash movie that you created earlier (the swf file) must be in the same location as your web page. If it is in the same location as the web page then you only use the button name. If however the file is in a different location you must include the file path. If you don't know about HTML file paths just make sure your Flash Movie is located in the same folder as your web page.
Understanding the different parts:
YourFileName.swf?TheFirstVariableNameInFlash=InformationToBePassedToThisVariableHere&TheSecondVariableNameInFlash=InformationToBePassedToThisVariableHere&TheThirdVariableNameInFlash=
InformationToBePassedToThisVariableHere
YourFileName.swf The name and location of the swf movie
? Sends the variable values to flash
TheFirstVariableNameInFlash Is the name of the variable in flash
InformationToBePassedToThisVariableHere Is the information or values you want to set that variable equal to
& This concatenates or in other words adds the next variable in flash.
Example: Download the Flash file Int 144f
Step One: The Flash
Understanding the different parts:
TheNameOfTheTextBoxVariable = "<A HREF=\"asfunction:TheNameOfTheFunctionYourCalling, AnyParametersYouWishToPassToTheFunction\">The text you want to appear in the text box goes here </A>";
TheNameOfTheTextBoxVariable Is the var name in the property panel for the text box
asfunction: Is the type of script your calling in this case a flash action script function but you could call other functions like javascript, php, asp or what ever. See below for examples
TheNameOfTheFunctionYourCalling is the function name in this example the function is called RunMe
AnyParametersYouWishToPassToTheFunction after the TheNameOfTheFunctionYourCalling if you will to pass some information back to the function add a comma followed by the parameters value you wish to send back.
You can send more than one parameter at one like so: TheNameOfTheFunctionYourCalling, parmValue1, parmValue2, parmValue3 etc...
Example: MyVarFunction1 = "<A HREF=\"asfunction:RunMe, 1,2,3\">Run One Function But Pass Three Values At Once To It </A>";
The text you want to appear in the text box goes here This is self explanatory
The function NameOfTheFunction(Num) { The Num in the brackets is set to the first parmValue1 sent by the function call. In the example Num is set to a number but you can set it to any thing you like
You could have if you where passing multiple values: function RunMe(Num1,Num2,Num3) { Where Num1 would be equal to parmValue1, Num2 would be equal to parmValue2 and Num3 would be equal to parmValue3
You don't have to use Num you can call Num any thing you like and the parmValue can be a number, word, true/false statement or any thing you like.
Think of it as calling a function by its name and as you call it your setting its values
Examples of calling other scripts:
MyVarFunction2 = "<A HREF=\"JavaScript:alert('Hello')\">Run Function Two</A>"; this will call a javascript alert message
MyVarFunction3 = "<A HREF=\"http://www.webwasp.co.uk\">Random Color</A>"; this will load a web page
Note: always best to keep function on the main time line an not inside movie clips as it can get messy. how ever should you for some reason ever need to run a function inside a movie clip you can do so like so:
MyVarFunction1 = "<A HREF=\"asfunction:InstanceNameOfTheMovieClip.FunctionNameInsideTheMovieClip\">Run Function One</A>"
Also you don't have to pass values back to the function if you don't want to. Just don't add the comma followed by the value after the function call and when creating a function name leave out the Num in the brackets
Example:
MyVarFunction1 = "<A HREF=\"asfunction:RunMe\">Function Call </A>";
function RunMe(Num) {
OutPut.text = "Hello this is my message";
}
Ok by now after reading this tutorial and the others i recommended at the top you should now be able to control and communicate with flash in ways others can only dream of.
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
7666 visitors to this page since
Oct 04 •
|
|
|

Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend:
Click here
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.
|