JavaScript与ActionScript3交互

// January 24th, 2010 // AS3知识积累, javascript and CSS

TestData.as

package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.external.ExternalInterface;

public class TestDate extends Sprite
{

private var i:int=0;
private var timer:Timer=new Timer(1000,60);

internal var jsReady:Boolean;
internal var aa:String =”hi”;
private var output:TextField=new TextField();
public function TestDate()
{
this.addChild(this.output);
this.timer.addEventListener(TimerEvent.TIMER,timerHandler);
this.timer.start();
this.output.appendText(‘开始时间’+”\n”);
this.output.autoSize=’12px’;

}

private function timerHandler(event:TimerEvent):void{

jsReady = new Boolean(ExternalInterface.call(“JSReady”,aa));
this.output.appendText(“runing:”+(i++)+”\n”);
if(jsReady){
ExternalInterface.addCallback(“sayHello”,sayHello);
ExternalInterface.call(“setSwfReady”,true);
if(ExternalInterface.available)ExternalInterface.call(“callAS”,”");
this.timer.stop();
}
}

public function sayHello(txt:String):void{
this.output.appendText(“JS: “+txt+”\n”);
}
}
}

javascript

<script language="JavaScript" type="text/javascript">
var jsReady=false;
var swfReady=false;
function atReady(){
jsReady=true;
}

function JSReady(){
return jsReady;
}
function setSwfReady(ready){
swfReady=ready;
}

function callAS(){
if(swfReady){
TestDate.sayHello(“Hello,World”);
}
}

function show(str){
document.getElementById(“show”).innerHTML=”test:”+str;
}

</script>

Leave a Reply

You must be logged in to post a comment.