Von Says: Tutorial Send Information to Flash with JavaScript

September 4th, 2008

The aim of the tutorial is to learn how to send any information to Flash with JavaScript. This info could be a variable or a text message. There are two examples in this tutorial. One sends a text message and the other a variable. For those unfamiliar with programming: a variable is something that “varies”. It could be anything you want.

In the example below it is the width of the car:

Example:Download the Flash file Int 120a


Example of a variable controlled via the JavaScript.

 

Using JavaScript to send a Text Message to Flash

Below is the example of what you will build in this tutorial.

Example:Download the Flash file Int 120b


Example of sending information to Flash with JavaScript.

NOTE: This method works in Netscape 7.1, Mozzila”s Fire Fox 1.0 and Microsoft Internet Explorer 6.0 service pack 2 but not Opera 7.54


Method One: Send Information to Flash with JavaScript:

  1. Open a new: Flash Movie
    I set my width and height to: 410 x 160 pixels.
  2. Save your Flash Movie as: send-info
  3. With the text tool create a: Dynamic Text Box
  4. Note: The dynamic text box has to be big enough to hold a sentence.

  5. Select the: Text box
  6. In the Property Inspector set the text box Variable Name (Var) to: text
  7. Note: Don”t get the Variable Name mixed up with the Instance Name.

  8. If you wish you can set the Text Box to re-size automatically depending on the number of words sent to it. To do this add the following to frame 1 of the Timeline:
  9. this.text.autoSize = “left”;

    Note: This will only work if the Text Box is set to Single Line in the Property Inspector.

    As an alternative you can also use the one of the following:

    this.text.autoSize = “center”; //or
    this.text.autoSize = “right”;

  10. Open up your normal web editor: Dreamweaver or FrontPage etc.
  11. Create a new: Web Page
  12. Go to code view in your web editor and add the following to the Head section of your web page:

  13. Change the User Prompt… or the Please Enter… as appropriate. They change the text in the dialog box that appears.
    The first section displays a message above the Dialog”s Input Box and the second section changes the message in the Input Box. See illustration below:


    Dialog text and message box text is determined by what you type in the JavaScript code on your web page.

  14. If your text box (or variable) is not called text, change the code above accordingly.
  15. Add the following HTML code to the Body section of the HTML in code view of your web page editor:

    GetInfo” classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″ codebase=”http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0″ width=”410” height=”160“>
    send-info
    .swf”>


    send-info
    .swf” quality=”high” menu=”true” pluginspage=”http://www.macromedia.com/shockwave/download/index.cgi’P1_Prod_Version=ShockwaveFlash” type=”application/x-shockwave-flash” width=”410” height=”160” swLiveConnect=”true” NAME=”GetInfo“>

    Click Here To Send Your Own Message To Flash” onClick=”SendMyInfo();”>

    // Note: You can also use:
    Click Here To Set the car width to 50
    ” onclick=”window.document.GetInfo.SetVariable(”carWidth“”50″);” />
    // If you want to set a variable in the flash movie with out using a prompt or giving the user an option to enter their own value
    // This just allows you other ways to send information to flash. You could use a text link or anything you like =0)

      Note: You MUST use this code or the Flash Movie will not work !

  16. Now change the 410 and 160 values for so that the width and height values are how you want your Flash Movie to display.
  17. If your movie has a different name then replace send-info with the file name of your movie.
  18. Note: Be careful with file names as they are case sensitive and never use spaces for file names that are used on the web.

  19. If you wish you can replace the default message: Click Here To Send Your Own Message To Flash

  20. If you wish you can define a start up message too by adding by adding the following into the Body Tag like this:
  21. GetInfo.SetVariable(”text”"Hello this is a start up message!”);”

    If you wish you can view an example of what the HTML source code would look like: View source code

 


Please indicate what you thought of this tutorial 
10 is the best: 
10 9 8 7 6 5 4 3 2 1


Method Two: Send Information to Flash with JavaScript

NOTE: This method is said to work in all browsers but I tested it and found it to only works in Microsoft Internet Explorer 6.0 service pack 2. I personally recommend the above method but you may want to try it and may find it works for you

  1. Open a new: Flash Movie
    I set my width and height to: 410 x 160 pixels.
  2. Save your Flash Movie as: send-info
  3. With the text tool create a: Dynamic Text Box
  4. Note: The dynamic text box has to be big enough to hold a sentence.

  5. Select the: Text box
  6. In the Property Inspector set the text box Variable Name (Var) to: text
  7. Note: Don”t get the Variable Name mixed up with the Instance Name.

  8. Add this code to frame one of your movie:
  9. #include “setvariables.as”
  10. If you wish you can set the Text Box to re-size automatically depending on the number of words sent to it. To do this add the following to frame 1 of the Timeline:
  11. this.text.autoSize = “left”;

    Note: This will only work if the Text Box is set to Single Line in the Property Inspector.

    As an alternative you can also use the one of the following:

    this.text.autoSize = “center”; //or
    this.text.autoSize = “right”;

  12. Create a new web page and use this code:
  13. /* ———————————————————–
    ———-Supporting Browsers———-
    PC:
    IE 5 and higher
    Netscape 6 and higher
    Moz/Firebird all
    Opera 7 and higher Mac OSX:
    IE 5.2
    Safari all
    Netscape 6 and higher
    Moz/Firebird/Camino all
    Opera 6 and higher

    Linux:
    Konqueror assumed
    ———————————————————– */

    var ua = navigator.userAgent.toLowerCase();
    var is_pc_ie = ((ua.indexOf(”msie”) != -1) && (ua.indexOf(”win”) != -1) && (ua.indexOf(”opera”) == -1) && (ua.indexOf(”webtv”) == -1));

    /* ———————————————————–
    function setFlashVariables(movieid, flashquery)

    movieid: id of object tag, name of movieid passed in through FlashVars
    flashquery: querystring of values to set. example( var1=foo&var2=bar )
    ———————————————————– */

    function setFlashVariables(movieid, flashquery) {
    var i, values;
    if (is_pc_ie) {
    var chunk = flashquery.split(”&”);
    for (i in chunk) {
    values = chunk.split(”=”);
    document.SetVariable(values, values);
    }
    } else {
    var divcontainer = “flash_setvariables_”+movieid;
    if (!document.getElementById(divcontainer)) {
    var divholder = document.createElement(”div”);
    divholder.id = divcontainer;
    document.body.appendChild(divholder);
    }
    document.getElementById(divcontainer).innerHTML = “”;
    var divinfo = “YourFileName
    .swf” FlashVars=”lc=”+movieid+”&fq=”+escape(flashquery)+”" width=”0″ height=”0″ type=”application/x-shockwave-flash”>“;
    document.getElementById(divcontainer).innerHTML = divinfo;
    }
    }

  14. Only on the line where it reads: var divinfo = “YourFileName.swf” FlashVars=”lc=”+movieid+”&fq=”+escape(flashquery)+”" width=”0″ height=”0″ type=”application/x-shockwave-flash”>“;
  15. Change the: YourFileName.swf to the name of your flash movie
  16. Now save the file as: setvariables.js in the same location as your flash movie and web page
  17. Now open note pad and add this code:
  18. // ———————————————————–
    // Universal method for javascript->flash setvariable
    // ———————————————————–

    if(!_level0.$jslisten_init){
    Stage.$jsvarlistener = new LocalConnection();
    Stage.$jsvarlistener.setVariables = function(query) {
    var i, values;
    var chunk = query.split(”&”);
    for (i in chunk) {
    values = chunk.split(”=”);
    _root[values] = values;
    }
    };
    Stage.$jsvarlistener.connect(_level0.movieid);
    _level0.$jslisten_init = true;
    }
  19. Save the file as: setvariables.as in the same location as your flash movie and web page
  20. Save your Flash Movie: File > Save
  21. Publish the Movie: File > Publish

Step Two: Setting up the HTML Web Page

  1. Open up your normal web editor: Dreamweaver or FrontPage etc.
  2. Create a new: Web Page
  3. Go to code view in your web editor and add the following to the Head section of your web page:


  4. Change the User Prompt… or the Please Enter… as appropriate. They change the text in the dialog box that appears.
    The first section displays a message above the Dialog”s Input Box and the second section changes the message in the Input Box. See illustration below:


    Dialog text and message box text is determined by what you type in the JavaScript code on your web page.

  5. If your text box (or variable) is not called text change the code above accordingly.
  6. Add the following HTML code to the Body section of the HTML in code view of your web page editor:

    GetInfo” classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″ codebase=”http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0″ width=”410” height=”160“>
    send-info
    .swf”>


    send-info
    .swf” quality=”high” menu=”true” pluginspage=”http://www.macromedia.com/shockwave/download/index.cgi’P1_Prod_Version=ShockwaveFlash” type=”application/x-shockwave-flash” width=”410” height=”160” swLiveConnect=”true” NAME=”GetInfo“>

    Click Here To Send Your Own Message To Flash” onClick=”SendMyInfo();”>

    // Note: You can also use:
    Click Here To Set the car width to 50
    ” onclick=”setFlashVariables(”GetInfo“,”carWidth=50″)” />
    // If you want to set a variable in the flash movie without using a prompt or giving the user an option to enter their own value
    // This just allows you other ways to send information to flash. You could use a text link or anything you like =0)

      Note: You MUST use this code or the Flash Movie will not work !

  7. Now change the 410 and 160 values for so that the width and height values are how you want your Flash Movie to display.
  8. If your movie has a different name then replace send-info with the file name of your movie.
  9. Note: Be careful with file names as they are case sensitive and never use spaces for file names that are used on the web.

  10. If you wish you can replace the default message: Click Here To Send Your Own Message To Flash

  11. If you wish you can define a start up message too by adding the following into the Body Tag like this:
  12. GetInfo“,”text=Hello this is a start up message!”);”>

If you wish you can view an example of what the HTML source code would look like: View source code

That”s it, you can send any message to Flash. It is worth noting that you can also send a variable to Flash using this method.

For more information on other ways of interaction between Flash and JavaScript see JavaScript in the: webwasp index

See these sites here for the more information on the second method:

http://www.mustardlab.com/developer/flash/jscommunication/

http://www.robertpenner.com/experiments/backbutton/backbutton_code.html

http://www.actionscript.org/tutorials/intermediate/Enabling_a_back_button_within_flash/index.shtml

 


Please indicate what you thought of this tutorial 
10 is the best: 
10 9 8 7 6 5 4 3 2 1

… Read More of this tutorial at WebWasp.co.uk Tutorials

Von Says: AS and Flash Tutorials - Sound in Flash without Action Script

August 31st, 2008

Allways, when you import a sound into a Flash, it seems very “cool” and “grace”. The sound is very important when you using it for some elements in your web site (flash menu, flash intro, some presentation..), becouse it makes your web site more powerful and it will leave a better impression.

This tutorial will teach you how to import a sound into a flash and how to create a button which will make a sound on click. This tutorial is for beginners who have no experience of using Flash.



Step 1

Create a new flash document, take the Rectangle Tool (R) and create a shape that would represent a button. Look at the picture below!



Step 2

Take the Text Tool (A), and type: sound on, on the button.



Step 3

Select the button (Ctrl+A), and press F8 on the keyborad (Convert to Symbol) to convert it into a Button. Look at the picture below!



Step 4

Click on File > Import > Import to Library, and choose c:disk > Windows > Media > notify, to import a sound into a flash Library.



Step 5

If you now open the Library (Ctrl+L), you”ll see this:



Step 6

Double click on the button or choose right click and Edit in Paste.



Step 7

Double click on layer1 to rename its name into a graphic. Insert a new layer and name it sound. Look at the picture below!



Step 8

Lock graphic layer, click on the sound layer and under Down choose right click > Insert Keyframe.



Step 9

Then, using a drag and drop technique, move your sound (notify) from Library on the stage.



Step 10

Unlock the graphic layer, and under Hit, choose right click > Insert Keyframe.





Step 11

Go back on the main scene (Scene 1), open the Properties Panel (Ctrl+F3) and set the options from the picture below!




Test your movie (Ctrl+Enter)


Download source file (.fla)



Where is Action Script and Flash taking us, particularly in the world of interactive website media, Web 2.0, and newfangled social interenet boom? According to Flash programmer Jim Berringer, as the web continues to grow and reach new demographic groups, Flash will play an extremely important role. Writes Berringer, "The thing about flash is that good designers and Action Script coders can make websites extremely user friendly. Whereas in the past, one had to have an 'introductory' course to the workings of the internet, Flash driven websites are simple: point, click, interact, have fun!". Increasingly, Flash is becoming the choice application for e-commerce, including the highly successful e-wallet www.payspark.com, (run by CSC247) that gives its users the ability to send and receive money, along with an ATM card for withdrawal privileges around the world. Payspark caters mostly to the Euro-gaming community, with focus on the sports betting market vertical. USA clients are not allowed, due to recent legislation enacted by the US Congress now 12 months ago. By far, however, www.payspark.com feeds most of its clients to online casinos. Flash casino and poker platforms are nothing new, but a consolidated flash deposit, play, and withdrawal method is. In effect, the USA is really missing the boat: in the fall, when NFL Betting drives most sports betting operations, USA customers must jump through hoops and clear hurdles imposed by the banking community. Instead of hampering these transactions, the banks should lobby Congress for the right to process money per the will of their clients. Writes Tim Hathaway, a Libertarian think-tank member, "People are being deprived of basic rights to gamble online if they want to. Speaking personally, I love NFL Betting from fall up until the Super Bowl. And why not? It's my right to spend my money as I see fit. Its time for the government to take a more laissez faire approach and leave me alone." Football is not the only sport where betting is popular: in the winter and during the famed "March Madness" college sports season, basketball betting is hugely popular. This means that e-wallets like www.payspark.com need to have solid Flash programming and latency year 'round - because of the importance of financial transaction safety and security. In the end, most people feel the USA legislation only hurts the US economy. According to Dan Lander, a liberal activist, "If I want to play online blackjack it's my business...but, the government can regulate this industry, tax it fairly, and we'll see strong growth in the jobs sector for web hosting, programming, IT, and management."