Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Passing variables from HTML to Flash CS3

This is a discussion on Passing variables from HTML to Flash CS3 within the Tutorials forums, part of the English Forums category; We have seen how to pass some values from Flash CS3 to PHP that allows us to maintain in communication ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 17-10-07, 06:51
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Passing variables from HTML to Flash CS3

We have seen how to pass some values from Flash CS3 to PHP that allows us to maintain in communication our Flash applications with the server.
We often need to assign to a SWF a value in entry using the HTML of the page in which the SWF itself is inserted.

With Flash 8 all it took, was defining a variable on the timeline and assigning it the value through the HTML.
With Flash CS3, things have a bit changed and we need to work with the LoaderInfo class. So let us follow the next example...

In this example, I use Adobe Dreamweaver to insert a SWF in a HTML page and to pass a value to the SWF.

I create a FLA and save it as "main.fla".
I create an HTML page with Dreamwearver and save it as "main.html".
I add "main.swf" to the HTML page "main.html".
I select the SWF and I open the Parameters Panel as shown in the following pic:



as we can be notice, I insert as a parameter name "flashvars" (in this way Flash knows that there is a parameter - let us see it this way), otherwise it does not work.
In the field Value (Valore), I insert "id=321". "id" will be the name of the variable that Flash will look for and 321 is its value.

Back to "main.fla", I assign a Document Class, an AS file saved as "Main.as", implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.display.LoaderInfo;
	
	public class Main extends MovieClip
	{
		private var id:String;
		
		public function Main()
		{
			getHTMLvars();
		}
		
		private function getHTMLvars():void
		{
			var value:String;
			var obj:Object=LoaderInfo(root.loaderInfo).parameters;
			for (value in obj)
			{
				id=String(obj[value]);
				try_txt.text=id;
			}
		}
	}
}
In brief, I told Flash to retrieve the value of "id" passed by the HTML.

Let us analyse the code.

Properties

a String variable with the same name (we are not obliged to do so) as the variable passed by the HTML
private var id:String;

Methods
getHTMLvars();
this methods implements the logic to retrieve the variable "id" from the HTML
a String variable used in a "for in" cycle
var value:String;
an Object variable to which I assign the property "parameters" of LoaderInfo from the "main.fla""s root
var obj:Object=LoaderInfo(root.loaderInfo).parameters;
with a "for in" cycle, I check if there is a String in the object "obj"
for (value in obj)
{
I assign to the property "id" a value as a "String" type equal to a fake property retrieved from "obj" (we could also see it as "obj.value" but Flash CS3 would not accept it)
id=String(obj[value]);
I assign the value of the property "id" as a text of a text field so to be sure that Flash sees that variable passed by the HTML
try_txt.text=id;
}

Clearly, I added the text field only to demonstrate this example. Once, the value of the variable "id" is retrieved from the HTML and is assigned to the property "id" of "Main.as",
we can use that value as wanted all through our Flash application.

Stay tuned!
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 28-08-08 at 06:25..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 31-12-07, 15:10
Junior Member
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
mxmania is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Hi,
Thx for the first such tutorial for flash CS3.
I am trying to retrieve variable value "id" to obtain the name of the flv file to be run by flv player component in Flash CS3

var id:String;
//id = "images/admin/flv/m.flv";
// loads source property
myFLVPlybk.source = id;
// loads source parameter of load() method
myFLVPlybk.load(id);
// loads source parameter of play() method
myFLVPlybk.play(id);

This works fine when use id = "http://www.flepstudio.org/forum/images/admin/flv/m.flv";
but if commented no values are obtained.
AS file is inluded in the same folder but has no effect. Am I missing something?

on ASP page, i am passing the url value:


" name="Flashvars">


I would be grateful if you could give me some hint. This would also help loads of other people who are trying to run FLV files dynamically.

Regards
Khna
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 31-12-07, 15:16
Junior Member
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
mxmania is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Just small correction:
Quote:
This works fine when use id = images/admin/flv/m.flv;
on ASP page, i am passing the url value:
Quote:

param name="Flashvars" value="id=<%= user %>"
" name="Flashvars">

Thx and Happy New years
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 31-12-07, 15:23
Junior Member
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
mxmania is on a distinguished road
Re: Passing variables from HTML to Flash CS3

somehow the forum code replaces my code lol, haven't red the rules sorry.
Value passed from my ASP page is
param name
name="Flashvars"
value="id=myRetrievedValue"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 02-01-08, 06:53
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Hi,
what you mean with " value passed from ASp page " ?
ASP create HTML code to embed the SWF and pass the value or ASP script must be called by Actionscript ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Flash Multi Gallery
  #6 (permalink)  
Old 02-01-08, 21:40
Junior Member
 
Join Date: Dec 2007
Posts: 12
Rep Power: 0
cooljackD is on a distinguished road
Re: Passing variables from HTML to Flash CS3

wot if yu have two values to pass to flash cs3

like :




how do retrieve it in the flash document CLASS
with the LoaderInfo.parameters

as you did in your example

Thanks

Best wishes for 2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 03-01-08, 07:01
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Hi cooljackD,

with more HTML vars:

HTML code
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>html vars to flash CS3</title>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>

<body>
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','300','height','150','src','main','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','flashvars','id=444&ir=888','movie','main' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="300" height="150">
  <param name="movie" value="main.swf" />
  <param name="quality" value="high" />
  <param name="flashvars" value="id=444&amp;ir=888" />
  <embed src="main.swf" width="300" height="150" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" flashvars="id=444&amp;ir=888"></embed>
</object>
</noscript>
</body>
</html>
Actionscript
Code:
package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.display.LoaderInfo;
	
	public class Main extends MovieClip
	{
		private var id:String;
		private var my_array:Array=new Array();
		
		public function Main()
		{
			getHTMLvars();
		}
		
		private function getHTMLvars():void
		{
			var value:String;
			var obj:Object=LoaderInfo(root.loaderInfo).parameters;
			for (value in obj)
			{
				id=String(obj[value]);
				my_array.push(id);
				try_txt.text=my_array.toString();
			}
		}
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8 (permalink)  
Old 05-01-08, 01:24
Junior Member
 
Join Date: Dec 2007
Posts: 12
Rep Power: 0
cooljackD is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Thanks for the quick answer, it really put some light on lots of things
so as you explained the ~value~ is the object we track from the vars we get from the html page and the id is the object containing the values of the of its container but instead of reading it in a textfield throught trace is the any way to the see all the objects retrieved from flash, i used to do list varialble and if the array was set to a global variable you could actually see all the array collection retrieved and now with as3 you can see anything is the a way to do that, its for futur things of i'm still trying to get use to the new ide flash cs3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #9 (permalink)  
Old 01-02-08, 01:26
Junior Member
 
Join Date: Dec 2007
Posts: 12
Rep Power: 0
cooljackD is on a distinguished road
Re: Passing variables from HTML to Flash CS3

hi flep
how would you retrieve the flashvars if you intend to use in an external swf

since with flash cs3 you need a preloader and the your app how to you get the flash vars in the app after it is loaded by the preloader ???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #10 (permalink)  
Old 01-02-08, 03:32
Junior Member
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0
rexus is on a distinguished road
Re: Passing variables from HTML to Flash CS3

Hi Flep, thank you for this tutorial.
anyway, using my_array.toString(); will give a result of 444,888 on the text field.

what if I have 2 text fields and I want to fill it with my_array[0] and my_array[1]?
how to do that?
I've been trying like:

try_txt1.text = my_array[0];
try_txt2.text = my_array[1];

where try_txt1 and try_txt2 are a dynamic textfield I created on the screen.

how do I do that?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3 passing array button to animate gwulfwud advanced Actionscript 3.0 1 02-09-08 12:33
Displaying PHP Variables in Flash chosendesigns PHP | mySQL | Flash CS3 1 14-05-08 13:49
passing DisplayObject references trough XML dedavai advanced Actionscript 3.0 1 21-01-08 10:11
passing DisplayObject references trough XML dedavai advanced Actionscript 3.0 0 21-01-08 09:43
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 0 01-11-07 06:47


All times are GMT. The time now is 20:09.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406