Welcome, Guest. Please login or register.
Did you miss your activation email?
05/22/12, 05:33
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  General
| |-+  Flash and AS 3 (Moderators: papachan, kofi addaquay)
| | |-+  passing a variable from AS 2.0 swf embedded into AS 3.0 flash file
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3 Print
Author Topic: passing a variable from AS 2.0 swf embedded into AS 3.0 flash file  (Read 5931 times)
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« on: 10/04/07, 10:18 »

Hello i have an swf using AS 2.0 and my main flash file is CS3 AS 3.0

(for jorge: my as 2.0 is the amfphp pane displaying images)

I would like to make it so that when the user clicks a button in the AS 2.0 file (which is loaded into main flash), it passes a variable ("12345" for example).

Is it possible to do this? if so, what code/solution would i need to do that? I'm in a lot of trouble if i can't do it!
Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #1 on: 10/04/07, 10:53 »

There are many ways you can do this. I suggest looking into 'flashvars' which is set within the browser. you might also have to consider using ExternalInterface as a way of passing the variable value to javascript and having some kind of logic in the javascript call a function in ur as3 swf (passing in the variable value). hope that helps. again, there a quite a number of ways to do this.

Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #2 on: 10/05/07, 06:25 »

There are many ways you can do this.

you should see the relief on my face when i read this..

which way do you think would be the most simple? I am not too advanced in flash and i literally just need to pass one variable over when the button is clicked

Thanks for your reply
Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #3 on: 10/05/07, 09:51 »

Smiley...looking for an easy way are we?? lol...why dont you use this oppurtunity to learn how to use the ExternalInterface. i think its a very important as it allows communications between your swf and your browser. look into this.
on button click, you would use the ExternalInterface to send your variable to a javascript function and have that function call an actionscript function in ur as3 swf which displays your data.
look into
calling javascript functions from actionscript AND
actionscript functions from javascript.

hope that helps for now

Kofi
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #4 on: 10/08/07, 05:03 »

So reading up on both flashvars and ExternalInterface it would seem ExternalInterface is the more complex way?
It seems this cannot be done entirely within flash...? You have to use java in the html file as a catalyst  ?
Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #5 on: 10/08/07, 09:00 »

No Smiley, like i said..there are quite a number of ways. you can also use what is called, the LOCALCONNECTION which allows two separate flash movies FROM the same browser to communicate with each other. Look into this..if all else fails ..ie you dont understand it. i will provide you with same code.

Smiley
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #6 on: 10/08/07, 09:40 »

thanks again for your reply Smiley
i found some stuff on google about the localconneciton and it seems the one out of the 3 i think i can understand roughly whats happening (i know nothing of java etc)

with this code, how would i target the specific part (my button) of the AS2.0 swf, instead of all of the swf?
I need to have my AS3.0 file load the AS2.0 then the AS2.0 passes a variable to my AS3.0 file.
it sounds like i am going crazy

Code:
// ActionScript 2 file, AS2animation.fla
// one movie clip animation named animation_mc on the timeline

// local connection instance to receive events
var AVM_lc:LocalConnection = new LocalConnection();

// stopAnimation event handler
AVM_lc.stopAnimation = function(){
    animation_mc.stop();
}

// listen for events for "AVM2toAVM1"
AVM_lc.connect("AVM2toAVM1");

-------

// ActionScript 3 file, AS3Loader.fla

// local connection instance to communicate to AVM1 movie
var AVM_lc:LocalConnection = new LocalConnection();

// loader loads AVM1 movie
var loader:Loader = new Loader();
loader.load(new URLRequest("AS2animation.swf"));
addChild(loader);

// when AVM1 movie is clicked, call stopPlayback
loader.addEventListener(MouseEvent.CLICK, stopPlayback);

function stopPlayback(event:MouseEvent):void {
    // send stopAnimation event to "AVM2toAVM1" connection
    AVM_lc.send("AVM2toAVM1", "stopAnimation");
}
Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #7 on: 10/08/07, 10:34 »

look close to your post. you are close. first of all you want to load you as2 swf into ur as3 swf. you can do that by using the loader class...BUT you would have to use the 'contentLoaderInfo' property of the loader class to listen for the complete event. like this
say you have
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);

this is handled once your swf completely loads. when it loads..in the handleComplete function use the localconnection to connect to the as2 swf  THEN have that function send back your variable. get what i mean?
« Last Edit: 10/09/07, 09:52 by kofi addaquay » Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #8 on: 10/08/07, 11:32 »

sorry, i am trying to build you a sample but my Flash program keeps crashing..i even made a post about it.
i am not quite sure if you want ur communication to be automatic. you might have a button in the as2 swf, which onclick, does the magic.
i am going to try hard to help you blind folded since i can test this out. to successfully communicate between the 2 swfs,  you must configure the receiving movie to listen for communcations. ie ..the as3 swf.
ie
import flash.net.LocalConnection;

var myReceiver:LocalConnection = new LocalConnection();
myReceiver.sendVariable = function(sentVariable:String)
{
   myTextBox.text = sentVariable;  <-- this displays what is sent from as2
}
myReciever.connect("_connectionChannel");

so before your swf even loaders ur as3 swf is already waiting or listening for messages sent over _connectionChannel.

within ur as2 swf, you would use a button handler like this

var varToSend:String = "Yaay";
var movieConnect:LocalConnection = new LocalConnection();
myButton.onRelease = function()
{
    movieConnect.send("connectionChannel", "sendVariable", varToSend);
}

thats about it. hope you understand. Smiley

Kofi
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #9 on: 10/08/07, 23:21 »

Hi kofi,

thanks again so much for your help here.
I am also trying to build a sample file here using your code and will post it as soon as i have something

(sorry for the late reply i am gmt +7)

J
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #10 on: 10/08/07, 23:50 »

Hi kofi,

I've created a test file.
I have got the as2.0 file to load into as3.0

but i am getting errors when trying to impliment your code:

1119: Access of possibly undefined property sendVariable through a reference with static type flash.net:LocalConnection.
1120: Access of undefined property myReciever.
« Last Edit: 10/09/07, 00:09 by jarmanje » Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #11 on: 10/09/07, 00:11 »

something is wrong with the forum. it keeps cutting off the second half to my last post...

here is the test file:
http://www.yoce.com/test.zip

the main.fla is a AS3.0 file

Do you see what is causing the error?

Logged
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #12 on: 10/09/07, 09:51 »

Ok...i got it to work. You need to be careful dude, there were typos in your code for the myReceiver Variable.

another major thing that i had to do was to implement the LocalConnection class as a Dynamic class so that you would implement the myReceiver.sendVariable function. That function is dynamic so the second error you were seeing was due to the LocalConnection being STATIC.

CHANGES
1
creat a new actionscript class called DynamicLocalConnection and save it in the same folder as your as2 and as3 swf. The class is implemented this way (copy and paste it)

package
{
   import flash.net.LocalConnection;
   
   dynamic public class DynamicLocalConnection extends LocalConnection
   {
      //no need to put anything thing here.
      //this is to implement dynamic properties
   }
}

Second change
in you AS2 file, there is YET another type. you have this
movieConnect.send("connectionChannel", "sendVariable", varToSend);
instead of
movieConnect.send("_connectionChannel", "sendVariable", varToSend);

the underscore is missing.

the code in your AS2 fla should look like this

var varToSend:String = valueText.text;
var movieConnect:LocalConnection = new LocalConnection();
myButton.onRelease = function()
{
    movieConnect.send("_connectionChannel", "sendVariable", varToSend);
}

FINALLY
you main swf. AS3 swf. The code should look like this

import flash.net.LocalConnection;
import DynamicLocalConnection;

var myReceiver:DynamicLocalConnection = new DynamicLocalConnection();

var loader:Loader = new Loader();
loader.load(new URLRequest("as2file.swf"));
addChild(loader);

myReceiver.connect("_connectionChannel");

myReceiver.sendVariable = function(passString:String)
{
   myTextBox.text = passString;  //<-- this displays what is sent from as2
}


There you go. i have already tested it. its working fine. let me know if you have any more problems


Kofi
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #13 on: 10/09/07, 10:25 »

Kofi you're really a life saver i cant thank you enough!
I'll try this now hopefully it'll work

thanks again so much for your help!
Logged
jarmanje
Senior Programmer
****
Posts: 334


View Profile
« Reply #14 on: 10/09/07, 10:30 »

It works perfectly,

you're a genius!!!!!!!
Logged
Pages: [1] 2 3 Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
anything