转换一种思路,在flash前端触发任务脚本,故研究一下lua,由于as3 不能直接调用lua,需要c来做桥梁,所以要先把alchemy配置上.
配置alchemy:http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Getting_Started#Windows
ps:修改在C:\cygwin\etc 下 profile 文件
在 PATH="/usr/local/bin:/usr/bin:${PATH}" 后一行加上
source /cygdrive/c/alchemy/alchemy-setup
就不用每次启动alc-on 的时候在命令行输入 .
试了很多次,想把官方的liblua52.a 编译进swc 都没有成功
gcc LuaAlchemy.c -llua52 -O3 -Wall -swc -o LuaAlchemy.swc --- 未成功
采用了笨的办法,把源文件重新编译成.o文件,一起编译进swc
gcc -c lapi.c
...
gcc -c lzio.c
全部把c文件编译成.o 文件 lua.c,luac.c 除外
LuaAlchemy.c 为自己写的c源代码,其中引用到了lua库代码
命令行通过gcc LuaAlchemy.o lapi.o lauxlib.o lbaselib.o ... lzio.o -O3 -Wall -swc -o LuaAlchemy.swc
最终把lua 编译进LuaAlchemy.swc
LuaAlchemy.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//lua头文件
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
//测试lua解析
static AS3_Val test(void* self, AS3_Val args)
{
char* val = NULL;
AS3_ArrayValue( args, "StrType", &val );
lua_State * l = luaL_newstate() ; //创建lua运行环境luaL_dostring
int ret = 0 ;
//ret = luaL_loadfile(l,"c:\\func.lua") ; //加载lua脚本文件
ret = luaL_dostring(l,val) ;//解析lua字符串
ret = lua_pcall(l,0,0,0) ;
int totalPixs = 0;
lua_getglobal(l,"width"); //获取lua中定义的变量
lua_getglobal(l,"height");
int w = 0;
int h = 0;
int total = 0 ;
w = atoi(lua_tostring(l,-1));
h = atoi(lua_tostring(l,-2));
total = w * h;
lua_pop(l,1) ; //恢复lua的栈
lua_close(l) ; //释放lua运行环境
return AS3_Int(total);
}
//加法
static AS3_Val sum(void* self, AS3_Val args)
{
int num1;
int num2;
AS3_ArrayValue( args, "IntType,IntType", &num1 ,&num2);
int total = 0;
total = num1 + num2;
return AS3_Int(total);
}
//调用as3 中方法
static AS3_Val asFunc(void* self, AS3_Val args)
{
AS3_Val baNS = AS3_String("alchemy");
AS3_Val baClass = AS3_NSGetS(baNS, "TaskScript");
AS3_Val emptyParams = AS3_Array("");
AS3_Val ba = AS3_New(baClass, emptyParams);
AS3_Val testScript = AS3_GetS(ba, "testScript");
AS3_Val scrParams = AS3_Array("IntType", 10001);
AS3_Val str = AS3_Call(testScript, ba, scrParams);
return str;
}
flash builder 调用:
TaskScript.as :
package alchemy
{
public class TaskScript
{
public function TaskScript()
{
}
public function testScript(tid :int):int
{
return tid;
}
}
}
package
{
import alchemy.TaskScript;
import cmodule.LuaAlchemy.CLibInit;
import flash.display.Sprite;
[SWF(width="800",height="600",backgroundColor="#cccccc",frameRate="60")]
public class AlchemyTest extends Sprite
{
public function AlchemyTest()
{
var test:CLibInit = new CLibInit();
TaskScript;
var lib:Object = test["init"]();
var str :String ="width=30;height=20;function sum(a,b) return a+b;end;function mystrcat(a,b)return a..b;end;function mysum(a,b)return csum(a,b);end;";
trace(lib.test(str));
trace(lib.sum(100000,2));
trace(lib.asFunc(1));
}
}
}
输出:
600
100002
10001
调试成功