Passing variables Javascript->Flash

24 views
Skip to first unread message

Robson

unread,
Sep 19, 2008, 9:29:24 PM9/19/08
to OpenSocial - OpenSocial Application Development
Hi Guys,

After reading lots of pages about Flash and Javascript integration, I
found 2 ways to do that using FlashVars and SetVariable.

1) Under opensocial, I cant use SetVariable. I've tried to load flash
file using SWFOBJECT and gadgets.io.getProxyUrl, but it didnt work.
Does someone make it work ? Is that a security problem ?

Error is:
Error calling method on NPObject!

2) Using FlashVars I could setup the communication, but there's a
weird thing happening. If I need to hide the flash object setting the
display to none and then make it visible again, the .swf reloads. It
just happens under FF and Chrome.

Any ideas ?


Robson

unread,
Sep 19, 2008, 10:04:37 PM9/19/08
to OpenSocial - OpenSocial Application Development
Hi all!

Again, found a solution.

After looking at BuddyPoke code I saw that flash's plugin needed to
run is v.9.

Newest versions of Flash allow integration with javascript using
External Interface.

So, that's a clean solution to integrate Javascript and Flash. Another
tip is SWFOBJECT that encapsulates lot of crossbrowser function to run
flash using javascript.

Reference:
http://blog.deconcept.com/2005/08/16/external-interface/

Cheers,

--Robson

Tushar Vaghela

unread,
Sep 20, 2008, 12:28:40 AM9/20/08
to opensoc...@googlegroups.com
Hi,

Here is the way to Pass value to Flash File.

When you are Embedding  the Flash Object in Page, Add name of the variable and the value in parameter Movie and Embed Tag.

Suppose your SWF file is myGame.swf, and you want to pass the UseName in it.

The code for this would be something like this.

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
width="765" 
height="500" 
id="danceMELAApp_Sep_10_Test
align="middle"
>
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="myGame.swf?UseName='myUserName'" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="myGame.swf?UseName='myUserName'
quality="high" 
bgcolor="#ffffff" 
width="765" 
height="500" 
name="myGame
align="middle" 
allowScriptAccess="sameDomain" 
type="application/x-shockwave-flash" 
</object>

You can Access _root.UseName in ActionScript which will return value myUserName


And if you want to pass value on Runtime, Flash External Interface is handy to do this.

Here's how you can send value from Flash to JavaScript Function,

Suppose you want to pass Score to submitScore function in JavaScript.

Code in Flash
=================
import flash.external.*;

flash.external.ExternalInterface.call("submitScore", _root.nScore);


In JavaScript
=================
function submitScore(score)
alert("Submit Score Called with ("+score+")");
}


And Finally if you want to send the Value from JavaScript to Flash in Runtime, use this.

Suppose you want to Pass your Username to Flash from JavaScript

In JavaScript
=================
function thisMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

thisMovie("myGame").sendToFlash(Username);

Code in Flash
=================
import flash.external.*;

function sendToFlash(Username) {
trace("sendToFlash called with (+arguments+)");
}


Application in which I have used this is http://sandbox.orkut.com/Application.aspx?appId=998727593002 

Some tips on how to use Flash with openSocial is at http://tusharvaghela.wordpress.com/2008/06/26/using-flash-with-open-social/

Thank you,
Tushar

Robson Dantas

unread,
Sep 20, 2008, 1:45:03 AM9/20/08
to opensoc...@googlegroups.com
Hi Tushar!

Thanks for the answer. But i'm still getting problems.

Do I need to use ExternalInterface.addCallback("sendToFlash",null,sendToFlash);  at flash ?

May u send a .fla example ?

Tks,

--Robson

2008/9/20 Tushar Vaghela <tushar...@gmail.com>

Robson Dantas

unread,
Sep 20, 2008, 5:31:19 PM9/20/08
to opensoc...@googlegroups.com
Hi people!

After get one night asleep, reading everything, I could setup the app and access Flash from Javascript using Opensocial. It works at the same domain and crossdomain too.

There're some tricks, hope it helps.

Somes errors that I've faced, all related to security issues. On the same domain, everything was working fine.
Uncaught Error: Error in Actionscript. Use a try/catch block to find error.
function getTextFromJavascript is not available
Error calling method on NPObject


Tools:
Using Flash Professional, version 8.0
Opensocial 0.7 apis

Test.fla

import flash.external.*;
import flash.system.Security;

// trick number 1
System.security.allowDomain('*');

// declaration trick number 2
var methodName:String = "sendTextToFlash";
var instance:Object = null;
var method:Function = getTextFromJavaScript;

//must return true and true
var isAvailable:Boolean = ExternalInterface.available;
var ret:Boolean = ExternalInterface.addCallback(methodName, instance, method);

function getTextFromJavaScript(valor):Void {
     teste.text = "From JavaScript: "+valor;
}

test.html (tricks: allowScriptAccess=always and function thisMovie)

<html>
<head>
<script>

function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
}else{
if(document[movieName].length != undefined){
return document[movieName][1];
}
return document[movieName];
}
}


function change()
{
    thisMovie("abc123").sendTextToFlash("Xddd");
}


</script>
</head>
<body>

SWF OUTSIDE DOMAIN
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="abc123" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="http://www.path.com.br/orkut/flash/teste.swf" />

<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />
<embed src="http://www.path.com.br/orkut/flash/teste.swf" name="abc123" quality="high" bgcolor="#ffffff" width="550" height="400" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<a href="javascript:change()">change</a>

</body>
</html>

Should it helps.

--Robson

2008/9/20 Robson Dantas <biu.d...@gmail.com>
Reply all
Reply to author
Forward
0 new messages