Javascript call AS3 function

前端 未结 1 1656
耶瑟儿~
耶瑟儿~ 2021-01-26 12:03

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,

1条回答
  •  生来不讨喜
    2021-01-26 12:56

    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;
    
        /**
         * 

    ExternalInterface example

    *

    author: Lukasz 'Severiaan' Grela

    * @author Lukasz 'Severiaan' Grela */ public class Main extends Sprite { protected var m_oOutput:TextField; //--------------------------------------- //Group: CONSTRUCTOR public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } protected function ei_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; } } protected function func1(num1:Number, num2:Number, num3:Number):void { m_oOutput.text = "func1(num1=" + num1 + ", num2=" + num2 + ", num3=" + num3 + ");"; } protected function func2(str1:String, num2:Number, str3:String):void { m_oOutput.text = "func2(str1=" + str1 + ", num2=" + num2 + ", str3=" + str3 + ");"; } protected function run():void { // entry point m_oOutput = new TextField(); 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-up if (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."; } } private function init(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

    
    
    
        
        external_interface_test
        
    
        
        
        
        
    
    
        

    external_interface_test

    Get Adobe Flash player

    and this are screengrabs:

    Initial state Function 1 clicked Function 2 clicked

    You can try to run this and let me know your output, I've tested on FF27

    0 讨论(0)
提交回复
热议问题