- 积分
- 136401
- 注册时间
- 2014-12-27
- 最后登录
- 2026-1-24
- 在线时间
- 605 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
|---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
    
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
 
|
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"),
- 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的源代码。
- #include <stdlib.h>
- #include "AS3/AS3.h"
-
- void decrementNumber() __attribute__((used,
- annotate("as3sig:public function decrementNumber(n:Number):uint"),
- 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);
- }
-
- int main()
- {
- // The SWC still needs a main() function.
- // See the code comments in samples/05_SWC for more details.
- AS3_GoAsync();
- }
复制代码 第一步使用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://bbs.9ria.com/thread-164074-1-1.html原文链接:http://blogs.adobe.com/flascc/2012/11/06/using-multiple-flascc-swcs-in-a-flash-project/
|
|