守望者--AIR技术交流
标题:
FlasCC:如何在AS项目中调用多个SWC
[打印本页]
作者:
破晓
时间:
2014-12-29 16:28
标题:
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的源代码:
#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,会看到以下输出:
x++ = 5
x-- = 3
复制代码
你还可以在
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/
欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)