天天看點

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,如需轉載請自行聯系原作者

繼續閱讀