Adobe Achemy入门指南(二)
在第一篇入门文章介绍了Achemy的基本知识,本文将介绍了了一个新的知识点,即如何从c代码中调用外部的actionscript3代码。
这在实际中有许多地方可以应用到。
思路很简单:就是常用的回调的概念,我们在as3中调用c语言代码的时候,将自身实例对象作为参数传递给所调用的c函数,
然后在c代码中就可以在需要的时候回调as3代码中所定义的回调函数。
具体实例如下,功能非常简单,就是让c代码调用as3中的一个函数,打印"hello,world".
首先是外部的as3代码:
代码
package {
import flash.display.Sprite;
import cmodule.test.CLibInit;
public class AlchemyWrapper extends Sprite
{
public function AlchemyWrapper()
{
var loader:CLibInit = new CLibInit;
var lib:Object = loader.init();
trace(lib.invoke(this));
}
public function testName():String
{
return "hello,world";
}
}
}
import flash.display.Sprite;
import cmodule.test.CLibInit;
public class AlchemyWrapper extends Sprite
{
public function AlchemyWrapper()
{
var loader:CLibInit = new CLibInit;
var lib:Object = loader.init();
trace(lib.invoke(this));
}
public function testName():String
{
return "hello,world";
}
}
}
然后是里面的alchemy部分的c代码:
代码
#include <stdlib.h>
#include <stdio.h>
//Header file for AS3 interop APIs
//this is linked in by the compiler (when using flaccon)
#include "AS3.h"
AS3_Val alchemyWrapper = NULL;
//Method exposed to ActionScript
static AS3_Val invoke(void* self, AS3_Val args)
{
alchemyWrapper = AS3_Undefined();
AS3_ArrayValue( args, "AS3ValType", &alchemyWrapper);
AS3_Val str = AS3_CallS("testName", alchemyWrapper, AS3_Null());
return str;
}
//entry point for code
int main()
{
//define the methods exposed to ActionScript
//typed as an ActionScript Function instance
AS3_Val invokeMethod = AS3_Function( NULL, invoke );
// construct an object that holds references to the functions
AS3_Val result = AS3_Object( "invoke: AS3ValType", invokeMethod );
// Release
AS3_Release( invokeMethod );
// notify that we initialized -- THIS DOES NOT RETURN!
AS3_LibInit( result );
// should never get here!
return 0;
}
#include <stdio.h>
//Header file for AS3 interop APIs
//this is linked in by the compiler (when using flaccon)
#include "AS3.h"
AS3_Val alchemyWrapper = NULL;
//Method exposed to ActionScript
static AS3_Val invoke(void* self, AS3_Val args)
{
alchemyWrapper = AS3_Undefined();
AS3_ArrayValue( args, "AS3ValType", &alchemyWrapper);
AS3_Val str = AS3_CallS("testName", alchemyWrapper, AS3_Null());
return str;
}
//entry point for code
int main()
{
//define the methods exposed to ActionScript
//typed as an ActionScript Function instance
AS3_Val invokeMethod = AS3_Function( NULL, invoke );
// construct an object that holds references to the functions
AS3_Val result = AS3_Object( "invoke: AS3ValType", invokeMethod );
// Release
AS3_Release( invokeMethod );
// notify that we initialized -- THIS DOES NOT RETURN!
AS3_LibInit( result );
// should never get here!
return 0;
}
具体的编译运行步骤,请参阅第一篇文章中的说明文字
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2010-09-21 21:18 Phinecos(洞庭散人) 阅读(977) 评论(1) 编辑 收藏 举报