FlasCC允许你将C/C++类库编译进SWC并在Flash中调用。这篇文章演示了如何在AS项目中调用多个SWC文件。
为了演示,我将从2个非常简单的C/C++类开始,它们自有一个函数,分别是递增数字和递减数字。解释这些C语言不在本文讨论范围之内,如果你对C语言不是很熟悉,你可以阅读代码注释,还可以查阅FlashCC SDK中inline_as3和 AS3_Return函数的范例,特别注意这个例子 /samples/02_Interop and /samples/05_SWC。同时这些例子在FlashCC SDK docs/samples.html中有非常详细的注释。
这是第一个MyLibrary.c的源代码:
- #include <stdlib.h>
- #include "AS3/AS3.h"
-
- void incrementNumber() __attribute__((used,
- ? ?? ???annotate("as3sig:public function incrementNumber(n:Number):uint"),serif; font-size:12px; line-height:1.8em"> ? ?? ???annotate("as3package:MyLibrary")));
- void incrementNumber()
- {
- ? ? int numberToIncrement = 0;
- ? ? inline_as3("%0 = n;n" : "=r"(numberToIncrement));
- ? ? numberToIncrement++;
- ? ? AS3_Return(numberToIncrement);
- }
- int main()
- ? ? // The SWC still needs a main() function.??
- ? ? // See the code comments in samples/05_SWC for more details.
- ? ? AS3_GoAsync();
- }
复制代码
第二个与第一个非常相似,多了decrementNumber()函数,这是MyLibrary2.c的源代码。
- void decrementNumber() __attribute__((used,serif; font-size:12px; line-height:1.8em"> ? ?? ???annotate("as3sig:public function decrementNumber(n:Number):uint"),serif; font-size:12px; line-height:1.8em"> ? ?? ???annotate("as3package:MyLibrary2")));
- void decrementNumber()
- ? ? int numberToDecrement = 0;
- ? ? // bring the AS3 value into C
- ? ? inline_as3("%0 = n;n" : "=r"(numberToDecrement));
- ? ? numberToDecrement--;
- ? ? AS3_Return(numberToDecrement);
- 复制代码
第一步使用gcc将C文件编译进SWC。
- ~/flascc/sdk/usr/bin/gcc MyLibrary.c -emit-swc=MyLibrary -o MyLibrary.swc
- ~/flascc/sdk/usr/bin/gcc MyLibrary2.c -emit-swc=MyLibrary2 -o MyLibrary2.swc
复制代码
现在创建一个简单的AS应用带调用这些SWC。 这个简单的应用只需分别调用SWC中的函数。以下是 demo.as的源代码:
- package
- ? ? import flash.display.Sprite;
- ? ? import flash.text.TextField;
- ? ? import flash.events.Event;
- ? ? import MyLibrary.CModule;
- ? ? import MyLibrary2.CModule;
- ? ? public class demo extends Sprite {
- ? ?? ???public function demo() {
- ? ?? ?? ?? ?addEventListener(Event.ADDED_TO_STAGE,initCode);
- ? ?? ???}
- ? ?? ???public function initCode(e:Event):void {
- ? ?? ?? ?? ?// create a TextField to show the output
- ? ?? ?? ?? ?var tf:TextField = new TextField();
- ? ?? ?? ?? ?addChild(tf);
- ? ?? ?? ?? ?// Start the FlasCC libraries
- ? ?? ?? ?? ?MyLibrary.CModule.startAsync(this);
- ? ?? ?? ?? ?MyLibrary2.CModule.startAsync(this);
- ? ?? ?? ?? ?var x:int = 4;
- ? ?? ?? ?? ?var xpp:int = MyLibrary.incrementNumber(x);
- ? ?? ?? ?? ?var xmm:int = MyLibrary2.decrementNumber(x);
- ? ?? ?? ?? ?// show the output
- ? ?? ?? ?? ?var s:String;
- ? ?? ?? ?? ?s??= "x++ = " + xpp + "n";
- ? ?? ?? ?? ?s += "x-- = " + xmm + "n";
- ? ?? ?? ?? ?tf.appendText(s);
- ? ?? ?? ?? ?trace(s);
- ? ? }
- 复制代码
注意必须先导入 CModule,并在每个SWC中调用startAsync()。 现在我们把AS文件打包为SWF。
- /path/to/flex/bin/mxmlc -static-link-runtime-shared-libraries -library-path=MyLibrary.swc,MyLibrary2.swc demo.as -o demo.swf
复制代码
如果你打开这个SWF,会看到以下输出: 你还可以在 FlashBuildr中创建AS项目,然后把SWC加到你的库路径(Project > Properties > ActionScript Build Path > Add SWC)。 原文链接:http://blogs.adobe.com/flascc/2012/11/06/using-multiple-flascc-swcs-in-a-flash-project/ |
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|