我是 actionscript 的菜鸟,从来没有使用过它,但是我将一个 Unity 项目编译为 flash,并且为了进行浏览器<-> javascript 通信,我需要做一个 hackish 的东西,在 .as 代码上提供一个类。统一是在指责它不知道类型字符串:“找不到类型或不是编译时常量:字符串。”
这是我的代码,请注意我什至没有 Flash 编译器,我使用的是 notepad++,所以我处于雷区,不知道该语言并且无法调试它。
import flash.external.ExternalInterface;
import flash.system.Security;
import global.SCORMInsideActionScriptFunctions;
public class BrowserCommunicator
{
public static function callFromJavascript() : void
{
trace("Javascript successfully called ActionScript function.");
}
// callback to register:
public static function GetSessionTime() : string
{
return SCORMInsideActionScriptFunctions.GetSessionTime()
}
//Sets up an ExternalInterface callback and calls a Javascript function.
public static function TestCommunication() : void
{
if (ExternalInterface.available)
{
try
{
ExternalInterface.addCallback("callFromJavascript", callFromJavascript);
// mine:
ExternalInterface.addCallback("GetSessionTime", GetSessionTime);
}
catch (error:SecurityError)
{
trace("A SecurityError occurred: " + error.message);
}
catch (error:Error)
{
trace("An Error occurred: " + error.message);
}
ExternalInterface.call('calledFromActionScript');
}
else
{
trace("External interface not available");
}
}
public static function FlushData_ServerToCache() : void
{
if (ExternalInterface.available)
{
// calls the java function here:
ExternalInterface.call('flushData');
}
else
{
trace("External interface not available");
}
}
public static function SubmitData_CacheToServer() : void
{
if (ExternalInterface.available)
{
ExternalInterface.call('submitData');
}
else
{
trace("External interface not available");
}
}
public static function SetData_AppToCache( field : string, data : string) : void
{
if (ExternalInterface.available)
{
ExternalInterface.call('setData', field, data);
}
else
{
trace("External interface not available");
}
}
public static function GetData_CacheToApp( field : string ) : string
{
if (ExternalInterface.available)
{
return ExternalInterface.call('getData', field);
}
else
{
trace("External interface not available");
return "";
}
}
}
}