Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.

Similar presentations


Presentation on theme: "Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your."— Presentation transcript:

1 Basic ActionScript and PHP Cis 126

2 Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your remote server

3 Setting Up for Publishing Next we're going to create a fla and set up the export options as we want them to be. So, open a fla, name it dogYears.fla save it in the flash folder you just created. within the flash file go to file -> publish settings and click on the first tab: formats.

4 Setting up for Publishing Make sure the flash and html boxes are ticked, then click on the folder to the right of the flash box. A popup window appears navigate to the swf folder you just created, as we want to save our swf as dogYears.swf in that swf folder. Now repeat the above process for html. All our html files are going to go directly in to the root folder Publish the movie ( file -> publish ) and check that the swf and html end up in the correct folders. They did?

5 In DreamWeaver Create the Basic PHP File <?PHP $calculation= $_POST['years']*7; echo $calculation; ?>

6 Creating the Flash File On the first frame of the main timeline create a small input textbox with an instance name of years. Now create a simple button and give it an instance name of myBtn.

7 The user will be able to enter their age in the input box and press the button. This sends our number to the PHP script, which will multiply it by seven. The result will be sent to an HTML page for testing

8 Setting Up a LoadVars object The first thing we want to do is set up a LoadVars object We'll also set up a variable called path - this has nothing to do with LoadVars. It only serves to make it easier for us to change our site folder structure in the future, if we should want to, with the minimum of hassle.

9 ActionScript // INITIALIZE path = "../nwjv/php/"; //declare path to php files lvOut = new LoadVars(); //create lv object

10 Button Code Load the following in the same frame: // BTN CODE myBtn.onRelease = function(){ //assign user-input value to lv property called years lvOut.years = years.text; //send to a blank window lvOut.send(path + "dogyears.php","_blank"); };

11 Explaination In the above code we specify an onRelease event for our button. When the button is released we create a new property called years, and we assign it the value of our user input box. Next we use the send method of our LoadVars object to send our object to whatever script we specify. The two arguments supplied are URL and TARGET. For the URL we use our path variable (which is set to equal "../nwjv/php/"), plus the name of our script (dogyears.php); and for the target we specify "_blank" which forces a new HTML browser window to open up. In the PHP script we created earlier. It uses a built in PHP function called "echo" - which does the same as "print" and prints out stuff to an HTML page. We've told it to multiply our number by 7 and then echo the result, so it should print the result of 7*year to the HTML page. Save your fla and lets try it.

12 Test it Once you can see the Flash interface enter a number - let's say 7 - into the input box and press the button. A new browser window should pop open, and that page should have the number 49 printed to it.

13 Passing Variables back to Flash Comment out the line echo $calculation; and replace it with the following: echo "&returnVal=$calculation";

14 Variables out of PHP Flash can receive data in name-value pairs (variables in the format of name=value). If there is more than one pair then each name-value pair is separated by an ampersand, and so a typical string of name-value pairs may look like this: &name1=Neil&location1=Bristol&name2=Justin&lo cation2=London& The final ampersand is optional.

15 Sending back to Flash If Flash is expecting a value to be returned then it will pick up this returned value and we can use it in our Flash movie. We still use the PHP language construct echo to output our value from PHP, –We echo it in "name-value pair" format so that Flash can understand it.

16 Create Container Create a dynamic textbox on stage, –make it multiline –wordwrap –give it the instance name of output.

17 Replace the code on frame 1 stop(); INIT path = "../nwjv/php/"; //declare path to php files lvOut = new LoadVars(); //create lv object lvIn = new LoadVars(); //create lv object lvIn.onLoad = function (success) { if(success){ //PHP variable value to textbox output.text = lvIn.returnVal; }else{ //...or notify of failure output.text = "fail"; } } // BTN myBtn.onRelease = function(){ //assign user-input value to lv property called years lvOut.years = years.text; //send to a blank window... // lvOut.send(path + "dogyears.php","_blank"); //get results returned to lvIn lvOut.sendAndLoad(path + "dogyears.php", lvIn, "POST"); }; Text Available on Class Web Page

18 Code explained First we store our path, as a string in a variable called path. Next we create two loadVars objects. One object will be sent from Flash to PHP, and the other object receives the value sent back from PHP to Flash. Next we use the onLoad method of our LoadVars object to tell Flash what to do if the load is successful but basically if the sendAndLoad is successful our returnVal variable will have been sent from PHP to FLASH, and will automatically be stored inside our loadVars object called lvIn

19 Send and Load Method When our button is released we take the value the user has inputted in the Flash input textbox, and add it to our [outgoing] LoadVars object as a new property. To make things easy for ourselves we decide to call the new property years lvOut.years = years.text; lvOut.sendAndLoad(path + "dogyears.php", lvIn, "POST"); We use the built in sendAndLoad() method of the loadVars object to send data and receive data. Let me explain in more detail:

20 Send and Load We use lvOut's sendAndLoad() method instead of its load() method. –This allows us to send variables and get them posted back to Flash. –To use sendAndLoad we need to send it three arguments; the path of our script, the LoadVars object to which we want our PHP data returned our method of sending, which is "POST". –At the beginning of the code we created an LoadVars object called loadIn, and we get out Flash data sent here because we specified it in the sendAndLoad() method.

21 We've set up lvIn's onLoad method, so it knows what to do when data is loaded in to it. – If you remember, in the PHP code we set it up to echo a name-value pair, which was basically: returnVal=$calculation; The name-value pair is returned to Flash – –the variable name becomes a property name of the lvIn instance (lvIn.returnVal), and the value gets stored in that property (so lvIn.returnVal will equal $calculation).

22 Test the file Generate the files and upload them to the server. Then open up dogYears.html Enter a number into the input box and press the button. The value of the calculation should now appear in the output textbox –you can see that the data has gone to PHP, been manipulated and then sent back and displayed in Flash!

23


Download ppt "Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your."

Similar presentations


Ads by Google