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

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


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2010/09/21/1832844.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值