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"
        }
    }
}
复制代码

然后是里面的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;
}
复制代码

 

    具体的编译运行步骤,请参阅第一篇文章中的说明文字 

posted on   Phinecos(洞庭散人)  阅读(980)  评论(1编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2008-09-21 ZOJ1005 Jugs
2007-09-21 java文件传输

导航

统计

点击右上角即可分享
微信分享提示