Javascript Call As3 Function
I know that there are lots of articles and forum post about this question and lots of them are not working and remained unanswered. Some tutorials claimed that their code works, bu
Solution 1:
Please try following:
Main.as
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.text.TextField;
/**
* <p>ExternalInterface example</p>
* <p><b>author:</b> Lukasz 'Severiaan' Grela</p>
* @author Lukasz 'Severiaan' Grela
*/publicclassMainextendsSprite
{
protectedvarm_oOutput:TextField;
//---------------------------------------//Group: CONSTRUCTOR publicfunctionMain():void
{
if (stage) init();
elseaddEventListener(Event.ADDED_TO_STAGE, init);
}
protectedfunctionei_invokeFunction(p_sMethod:String, ...args):void
{
//info("ei_invokeFunction(p_sMethod:String=" + p_sMethod + ", ...args=[" + args + "]):void");switch (p_sMethod)
{
case"func1":
func1.apply(this, args);
break;
case"func2":
func2.apply(this, args);
break;
}
}
protectedfunctionfunc1(num1:Number, num2:Number, num3:Number):void
{
m_oOutput.text = "func1(num1=" + num1 + ", num2=" + num2 + ", num3=" + num3 + ");";
}
protectedfunctionfunc2(str1:String, num2:Number, str3:String):void
{
m_oOutput.text = "func2(str1=" + str1 + ", num2=" + num2 + ", str3=" + str3 + ");";
}
protectedfunctionrun():void
{
// entry point
m_oOutput = newTextField();
m_oOutput.x = m_oOutput.y = 5;
m_oOutput.width = 480;
m_oOutput.height = 320;
m_oOutput.multiline = true;
m_oOutput.wordWrap = true;
m_oOutput.border = true;
addChild(m_oOutput);
//prepare the ExternalInterface hook-upif (ExternalInterface.available)
{
try {
ExternalInterface.addCallback("invokeFunction", ei_invokeFunction);
m_oOutput.text = "ExternalInterface is available."
}
catch (err:*)
{
m_oOutput.text = "ExternalInterface is not available. \nError:" + err;
}
}
else
{
m_oOutput.text = "ExternalInterface is not available.";
}
}
privatefunctioninit(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, onStageResized, false, 0, true);
run();
}
}
}
index.htm
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"/><title>external_interface_test</title><metaname="description"content="" /><scriptsrc="js/swfobject.js"></script><script>var flashvars = {
};
var params = {
menu: "false",
scale: "noScale",
allowFullscreen: "true",
allowScriptAccess: "always",
bgcolor: "",
wmode: "direct"// can cause issues with FP settings & webcam
};
var attributes = {
id:"core"
};
swfobject.embedSWF(
"externalinterfacetest.swf",
"altContent", "490", "330", "10.0.0",
"expressInstall.swf",
flashvars, params, attributes);
</script><script>var $DEBUG = true;
//daft implementation only for testing - this should be replaced (the body of that function) with more accurate/reliable versionfunctiongetFlashMovie(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
/**
* Reference to the "core" SWF
*/functiongetCore()
{
try
{
var movie = getFlashMovie("core");
}
catch(e)
{
if($DEBUG)
alert("getCore" + "\n" + e);
}
return movie;
}
//The "invokeFunction" is the registered callback method within SWF in query/**
* wrapper for calling flash function
*/functionfunction1(num1, num2, num3)
{
try
{
getCore().invokeFunction("func1", num1, num2, num3);
}
catch(e)
{
if($DEBUG)
alert("func1" + "\n" + e);
}
}
/**
* wrapper for calling flash function
*/functionfunction2(str1, num2, str3)
{
try
{
getCore().invokeFunction("func2", str1, num2, str3);
}
catch(e)
{
if($DEBUG)
alert("func2" + "\n" + e);
}
}
</script><style>html, body { height:100%; overflow:hidden; }
body { margin:0; }
</style></head><body><divid="altContent"><h1>external_interface_test</h1><p><ahref="http://www.adobe.com/go/getflashplayer">Get Adobe Flash player</a></p></div><divid="eiMenu"><ahref='#'onclick='javascript:function1(1, 2, 3);return false;'>Function 1</a>|
<ahref='#'onclick="javascript:function2('a', 2, 'c');return false;">Function 2</a></div></body></html>
and this are screengrabs:
You can try to run this and let me know your output, I've tested on FF27
Post a Comment for "Javascript Call As3 Function"