FlasCC allows you to create a SWC file from a C++ library. This is useful if you have an existing ActionScript project and want to add some logic that is already written in C++. This post demonstrates how you can operate on ActionScript objects that were passed as parameters into functions of a C++ library.
This post assumes you already know how to compile a SWC in FlasCC. If you do not know how see the samples/05_SWC sample application that ships with the FlasCC SDK.
The AS3++.h library exposes some functions to make it easier to operate on ActionScript objects within C++.
Getting a property
Getting the value of a property of an ActionScript object is pretty straightforward by using the getproperty() function.
In the sample code below the getDelta() C++ function demonstrates this.
Setting a property
Setting the value of a property is also straightforward by using the setproperty() function.
In the sample code below the setDelta() C++ function demonstrates this.
Calling a method
Calling a method is slightly more involved as you need to first use getproperty() on the name of the method to get a Function var that represents that method. Then pass that object into the call() or call_v() functions.
In the sample code below the getString() C++ function demonstrates this.
Sample Code
Here is a sample C++ library that demonstrates how to do each of these operations.
When compiling this library using g++ remember to set the -lAS3++ flag as described in the AS3++ documentation.
#include <stdio.h> #include "AS3/AS3.h" #include "AS3/AS3++.h" //// // // Get a property of an ActionScript object in C++ // //// void getDelta() __attribute__((used, annotate("as3import:flash.events.MouseEvent"), annotate("as3sig:public function getDelta(m:MouseEvent):int"), annotate("as3package:MyLibrary"))); void getDelta() { // marshall the AS3 parameter into a C++ variable AS3::local::var mouseEvent; AS3_GetVarxxFromVar(mouseEvent, m); // now that we have it in C++ let's get a property on that object AS3::local::var propertyName = AS3::local::internal::new_String("delta"); AS3::local::var outputValue = AS3::local::internal::getproperty(mouseEvent, propertyName); // convert the var to a scalar int output = AS3::local::internal::int_valueOf(outputValue); // return that scalar value AS3_Return(output); } //// // // Set a property on an ActionScript object in C++ // //// void setDelta() __attribute__((used, annotate("as3import:flash.events.MouseEvent"), annotate("as3sig:public function setDelta(m:MouseEvent):void"), annotate("as3package:MyLibrary"))); void setDelta() { // marshall the AS3 parameter into a C++ variable AS3::local::var mouseEvent; AS3_GetVarxxFromVar(mouseEvent, m); // now that we have it in C++ let's set a property on that object AS3::local::var propertyName = AS3::local::internal::new_String("delta"); AS3::local::var newValue = AS3::local::internal::new_int(5); AS3::local::internal::setproperty(mouseEvent, propertyName, newValue); } //// // // Call a method of an ActionScript object in C++ // //// void getString() __attribute__((used, annotate("as3import:flash.events.MouseEvent"), annotate("as3sig:public function getString(m:MouseEvent):String"), annotate("as3package:MyLibrary"))); void getString() { // marshall the AS3 parameter into a C++ variable AS3::local::var mouseEvent; AS3_GetVarxxFromVar(mouseEvent, m); // demonstrate how to call a function of that object from C++ AS3::local::var functionName = AS3::local::internal::new_String("toString"); // first get the property of the object called "toString" which is a Function var AS3::local::var function = AS3::local::internal::getproperty(mouseEvent, functionName); // then call that Function object (in this case it takes no parameters) AS3::local::var output = AS3::local::internal::call(function, AS3::local::internal::_null, 0, NULL); // Since AS3_Return (as shown in getDelta()) can only accept a scalar value we need // to marshall this C++ object into an ActionScript String and return it using AS3_ReturnVar AS3_DeclareVar(asOutputString, String); AS3_CopyVarxxToVar(asOutputString, output); AS3_ReturnAS3Var(asOutputString); } int main() { // The SWC still needs a main() function. // See the code comments in samples/05_SWC for more details. AS3_GoAsync(); } |
And here is a sample ActionScript application that demonstrates how to call the methods in the SWC generated from that C++ library:
package { import flash.display.Sprite; import flash.text.TextField; import flash.events.Event; import flash.events.MouseEvent; import MyLibrary.CModule; public class demo extends Sprite { public function demo() { addEventListener(Event.ADDED_TO_STAGE, initCode); } private function initCode(e:Event):void { var tf:TextField = new TextField(); addChild(tf); // start the FlasCC library MyLibrary.CModule.startAsync(this); // a sample ActionScript object var me:MouseEvent = new MouseEvent("click"); // // Get a property // tf.appendText("delta=" + MyLibrary.getDelta(me) + "\n"); // // Set a property // MyLibrary.setDelta(me); tf.appendText("delta=" + me.delta + "\n"); // // Call a method // tf.appendText("toString=" + MyLibrary.getString(me) + "\n"); } } } |

Hi, very interesting.
can we use some library like WINUSB in c++ to connect to custom usb device and transfer data to Flash?
FlasCC compiled content runs inside the Flash Player’s secure sandbox and only has access to the same Flash APIs that a normal Flash application has access to. This means you cannot access USB devices in the way you want as there is no API to do that.
Hi,help me.
when i compile it to a SWC .
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal10new_Str
ingEPKci’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal4callENS0
_3varES2_iPS2_Pv’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal11setprop
ertyENS0_3varES2_S2_’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal11int_val
ueOfENS0_3varE’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal5_nullE’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local3varD1Ev’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal7new_intE
i’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local3varC1ERKS1_’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local8internal11getprop
ertyENS0_3varES2_’
/tmp/ccsaBxr6.o: error: undefined reference to ‘__ZN3AS35local3varaSES1_’
/////////////////////////////////////////////////////
T05: check
@echo “——– Sample 5 ——–”
@echo && echo “Now compile a SWC and demo SWF”
“$(FLASCC)/usr/bin/g++” $(BASE_CFLAGS) -O4 getp.cpp -emit-swc=MyLibrary -o getp.swc
include
../Makefile.common
clean:
rm -f *.swf *.swc *.bc *.exe
@jack – Is this the compile command you are using?
$(FLASCC)/usr/bin/g++” $(BASE_CFLAGS) -O4 getp.cpp -emit-swc=MyLibrary -o getp.swc
If so what code is inside getp.cpp?
thank you.
getp.cpp is the Sample Code in(Operating on ActionScript objects in C++),
operating system is windows 7.
the compile command is $(FLASCC)/usr/bin/g++” $(BASE_CFLAGS) -O4 getp.cpp -emit-swc=MyLibrary -o getp.swc
@jack – It looks like you may be missing the -lAS3++ compiler flag. See this page for an example: http://www.adobe.com/devnet-docs/flascc/docs/capidocs/as3plusplus.html
thank you !!!!!!!!!!!!!!
it work.
the compile command is $(FLASCC)/usr/bin/g++” $(BASE_CFLAGS) -O4 getp.cpp -emit-swc=MyLibrary -o getp.swc -lAS3++
Really helpful! I think this should be somewhere in the doc.
help me please!
i need a Sample Code about flash++.h and swc.
my Sample Code bitmapdata.cpp:
/////////////////////////////////////////////////////
#include
#include
using namespace AS3::ui;
void gettpoint() __attribute__((used,
annotate(“as3sig:public function gettpoint(x:Number,y:Number):uint”),
annotate(“as3package:MyLibrary”)));
void gettpoint()
{
double xx;
double yy;
inline_as3( “%0 = x;%1 = y\n” : “=r”(xx) : “r”(yy) );
flash::geom::Point p=flash::geom::Point::_new();
AS3_Return(xx);
}
int main()
{
AS3_GoAsync();
return 0;
}
//////////////////////////////////////////////////////////
the compile command is “$(FLASCC)/usr/bin/g++” $(BASE_CFLAGS) -O4 bitmapdata.cpp -emit-swc=MyLibrary -o bitmapdata.swc -lFlash++ -lAS3++
////////////////////////////////////////////////////////////
It took me ten minutes to compile,but it can’t work.why?
@jack – Can you please post your question on the FlasCC Forums (http://forums.adobe.com/community/game_developers/flascc). It will be much easier to discuss your issue there.