加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:13:01 所属栏目:百科 来源:网络整理
导读:FlasCC允许你将C/C++类库编译进SWC并在Flash中调用。这篇文章演示了如何在AS项目中调用多个SWC文件。 为了演示,我将从2个非常简单的C/C++类开始,它们自有一个函数,分别是递增数字和递减数字。解释这些C语言不在本文讨论范围之内,如果你对C语言不是很熟悉
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"),serif; font-size:12px; line-height:1.8em"> ? ?? ???annotate("as3package:MyLibrary")));
  5. void incrementNumber()
  6. {
  7. ? ? int numberToIncrement = 0;
  8. ? ? inline_as3("%0 = n;n" : "=r"(numberToIncrement));
  9. ? ? numberToIncrement++;
  10. ? ? AS3_Return(numberToIncrement);
  11. }
  12. int main()
  13. ? ? // The SWC still needs a main() function.??
  14. ? ? // See the code comments in samples/05_SWC for more details.
  15. ? ? AS3_GoAsync();
  16. }
复制代码
第二个与第一个非常相似,多了decrementNumber()函数,这是MyLibrary2.c的源代码。

  1. 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")));
  2. void decrementNumber()
  3. ? ? int numberToDecrement = 0;
  4. ? ? // bring the AS3 value into C
  5. ? ? inline_as3("%0 = n;n" : "=r"(numberToDecrement));
  6. ? ? numberToDecrement--;
  7. ? ? AS3_Return(numberToDecrement);
  8. 复制代码


第一步使用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. ? ? import flash.display.Sprite;
  3. ? ? import flash.text.TextField;
  4. ? ? import flash.events.Event;
  5. ? ? import MyLibrary.CModule;
  6. ? ? import MyLibrary2.CModule;
  7. ? ? public class demo extends Sprite {
  8. ? ?? ???public function demo() {
  9. ? ?? ?? ?? ?addEventListener(Event.ADDED_TO_STAGE,initCode);
  10. ? ?? ???}
  11. ? ?? ???public function initCode(e:Event):void {
  12. ? ?? ?? ?? ?// create a TextField to show the output
  13. ? ?? ?? ?? ?var tf:TextField = new TextField();
  14. ? ?? ?? ?? ?addChild(tf);
  15. ? ?? ?? ?? ?// Start the FlasCC libraries
  16. ? ?? ?? ?? ?MyLibrary.CModule.startAsync(this);
  17. ? ?? ?? ?? ?MyLibrary2.CModule.startAsync(this);
  18. ? ?? ?? ?? ?var x:int = 4;
  19. ? ?? ?? ?? ?var xpp:int = MyLibrary.incrementNumber(x);
  20. ? ?? ?? ?? ?var xmm:int = MyLibrary2.decrementNumber(x);
  21. ? ?? ?? ?? ?// show the output
  22. ? ?? ?? ?? ?var s:String;
  23. ? ?? ?? ?? ?s??= "x++ = " + xpp + "n";
  24. ? ?? ?? ?? ?s += "x-- = " + xmm + "n";
  25. ? ?? ?? ?? ?tf.appendText(s);
  26. ? ?? ?? ?? ?trace(s);
  27. ? ? }
  28. 复制代码
注意必须先导入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/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读