[Flash/Flex] FlasCC:如何在AS项目中调用多个SWC

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的源代码:

  1. #include <stdlib.h>
  2. #include "AS3/AS3.h"

  3. void incrementNumber() __attribute__((used,
  4.         annotate("as3sig:public function incrementNumber(n:Number):uint"),
  5.         annotate("as3package:MyLibrary")));

  6. void incrementNumber()
  7. {
  8.     int numberToIncrement = 0;
  9.     inline_as3("%0 = n;\n" : "=r"(numberToIncrement));
  10.     numberToIncrement++;

  11.     AS3_Return(numberToIncrement);
  12. }

  13. int main()
  14. {
  15.     // The SWC still needs a main() function.  
  16.     // See the code comments in samples/05_SWC for more details.
  17.     AS3_GoAsync();
  18. }
复制代码
第二个与第一个非常相似,多了decrementNumber()函数,这是MyLibrary2.c的源代码。

  1. #include <stdlib.h>
  2. #include "AS3/AS3.h"

  3. void decrementNumber() __attribute__((used,
  4.         annotate("as3sig:public function decrementNumber(n:Number):uint"),
  5.         annotate("as3package:MyLibrary2")));

  6. void decrementNumber()
  7. {
  8.     int numberToDecrement = 0;
  9.     // bring the AS3 value into C
  10.     inline_as3("%0 = n;\n" : "=r"(numberToDecrement));
  11.     numberToDecrement--;

  12.     AS3_Return(numberToDecrement);
  13. }

  14. int main()
  15. {
  16.     // The SWC still needs a main() function.  
  17.     // See the code comments in samples/05_SWC for more details.
  18.     AS3_GoAsync();
  19. }
复制代码


第一步使用gcc将C文件编译进SWC。

  1. ~/flascc/sdk/usr/bin/gcc MyLibrary.c -emit-swc=MyLibrary -o MyLibrary.swc
  2. ~/flascc/sdk/usr/bin/gcc MyLibrary2.c -emit-swc=MyLibrary2 -o MyLibrary2.swc
复制代码


现在创建一个简单的AS应用带调用这些SWC。
这个简单的应用只需分别调用SWC中的函数。以下是demo.as的源代码:
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.text.TextField;
  5.     import flash.events.Event;
  6.     import MyLibrary.CModule;
  7.     import MyLibrary2.CModule;

  8.     public class demo extends Sprite {
  9.         public function demo() {
  10.             addEventListener(Event.ADDED_TO_STAGE, initCode);
  11.         }

  12.         public function initCode(e:Event):void {
  13.             // create a TextField to show the output
  14.             var tf:TextField = new TextField();
  15.             addChild(tf);

  16.             // Start the FlasCC libraries
  17.             MyLibrary.CModule.startAsync(this);
  18.             MyLibrary2.CModule.startAsync(this);

  19.             var x:int = 4;
  20.             var xpp:int = MyLibrary.incrementNumber(x);
  21.             var xmm:int = MyLibrary2.decrementNumber(x);

  22.             // show the output
  23.             var s:String;
  24.             s  = "x++ = " + xpp + "\n";
  25.             s += "x-- = " + xmm + "\n";
  26.             tf.appendText(s);
  27.             trace(s);
  28.         }
  29.     }
  30. }
复制代码
注意必须先导入CModule,并在每个SWC中调用startAsync()。
现在我们把AS文件打包为SWF。
  1. /path/to/flex/bin/mxmlc -static-link-runtime-shared-libraries -library-path=MyLibrary.swc,MyLibrary2.swc demo.as -o demo.swf
复制代码


如果你打开这个SWF,会看到以下输出:


  1. x++ = 5
  2. x-- = 3
复制代码

你还可以在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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值