Search code examples
javascriptactionscript-3actionscriptexternalinterfaceaddcallback

How do I get this function in ActionScript to return an array to JavaScript using ExternalInterface


I am very new to both JavaScript and ActionScript. I am trying to get javascript to call a function in ActionScript and store the returned array. I have looked everywhere for help and i cant seem to get this to work. My actionscript is below:

import flash.display.Sprite;
import flash.text.Font;
import flash.text.FontType;
import flash.text.FontStyle;
import flash.external.*;

public class FontList extends Sprite
{

    public function FontList()
    {
        ExternalInterface.call('populateFontsList', getDeviceFonts());
        ExternalInterface.addCallback('getFonts', getDeviceFonts);
    }

There is a getDeviceFonts() method that works, and the .call function works too, calling the function within the javascript. However, when i try and call the getFonts method in javascript it dosent work. Relavent Javascript is as below:

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

var fontArray = getFlashMovie("FontList.swf").getFonts();

Am i missing something here?


Solution

  • If the Actionscript function getDeviceFonts() returns an array, your code should work as it is, and the fontArray in JavaScript would contain the same values.

    One thing to keep in mind though is that you can't call the ActionScript function until it the swf file is loaded and ready, so for example, you can't do var fontArray = getFlashMovie("FontList.swf").getFonts(); directly on page load, since the swf won't be loaded yet, and hence getFonts() won't be defined yet.

    But I guess ExternalInterface.call('populateFontsList', getDeviceFonts()); should work, if you have a JavaScript function populateFontsList that takes an array as an argument. That JavaScript function should be called as soon as the swf is loaded and the ActionScript is executed.