Code Snippets
360|Flex was… Awesome
Mar 15th
So I’m finally back at work from a week at 360|Flex, San Jose, and like the title suggests, it was great!
Tell us more!
Here’s my little blurb about the conference, in case you weren’t there:
This was my first 360 conference, and I had the honor of speaking there as well. I got to talk a bit about the Pixelwave framework, and give an overview of what a Flash developer may need to know to get started developing native iPhone apps. I also got to show a few demos of the engine in action, and finally demoed our upcoming iPhone game, a re-work of the popular Dino Run Flash game.
So that was nice, but the rest of the conference is what’s probably going to keep me coming in the years to come.
The speakers were good, the parties were fun, the location was cool, but what I probably enjoyed most were the people. I got to meet some really cool developers from all over the world, and make some really great connections. I think the smaller, more intimate atmosphere of this conference is what really sets it apart, allowing people to really spend time together, get to know each other, and build real friendships instead of just exchanging business cards. if you can make it to one of these in the future you should, and come say hi!
Flash Extension: Swap multiple symbols at once
Mar 15th
Every once in a while we come across something in Flash authoring that doesn’t work quite right… For example not being able to select a few symbols and swapping them with a different one from the library.
We can’t blame Adobe for this though since they can’t possibly think of everything, and they gave us the means to extend Flash’s capabilities using JSFL. If you’ve never heard of it, JSFL (Java Script Flash) is a javascript API that can be used to make the Flash authoring do whatever you want programatically.
So we had the need to swap something like 30 symbols on the stage with a different one from the library, and once you select more than one symbol the little ‘Swap’ button in the properties panel gets grayed out. We could select each one and swap it individually, but that would take waay too long… So I wrote a small script to do this, and thought I might as well post it here for everyone to use.

It’s super easy to install, just double click the MassSwap.mxp file and it’ll be added to Flash’s commands list (via the extension manager).
Once you’ve installed it you can use it like so:
Creating unique variables within your preprocessor macros
Dec 1st
Sometimes in your macros you need to use a variable, but you can’t scope it to the macro. If you use this macro more than one in the same scope you get a duplicate variable warning.
For example, let’s say this is your macro:
#define LOOP_10_TIMES \ int i = 0; \ for( i = 0; i < 10; ++i)
And this is your implementation:
LOOP_10_TIMES{
x += i;
}
This is fine, but what happens when you want to use this macro twice? Like so:
LOOP_10_TIMES num += i; LOOP_10_TIMES mun += i;
You’re sure to get a nasty error (or at least a warning).
This is because your code now declares the variable ‘i’ twice in the same scope.
Calling C functions in your C++ code
Oct 20th
Have you ever wanted to call a c function located in one file within a different c++ (.mm / .cpp) file?
If you did, you probably know that it can be a royal pain if you don’t know the trick.
So if like me you’ve been looking for the solution, it’s very easy! Just use the extern “C” keyword (not to be confused with plain extern). Here’s an example:
extern "C" #include "my_c_code.h";
This also works with the Objective-C #import keyword:
extern "C" #import "MyObjCClass.h";
or alternatively for multiple includes
extern "C" {
#include "my_c_code.h"
#include "my_other_c_code.h"
}
And that’s the way you include someone else’s C code into your C++ files. Easy!
But…
If you’re writing brand new C code and you want it to be automatically compatible with other C++ code you can build it in a way that will make it #include’able into any source file.
Using the __cplusplus preprocessor variable you can check if the current build target is a c++ file and if so, surround your code with extern “C”
So for example let’s say this is our current C header file:
//my_c_header.h: int addOne(int n); int globalVar; //..etc
To make this code C++ friendly, we’d do this:
//my_cpp_friendly_c_header.h:
#ifdef __cplusplus
extern "C" {
#endif
int addOne(int n);
int globalVar;
//..etc
#ifdef __cplusplus
}
#endif
Now in your C++ file you can just write
// my_cpp_code.cpp #include "my_cpp_friendly_c_header.h"
So if you know that your C code will be included into C++ source, it’s always a good idea to wrap it in extern “C” { … } to avoid headaches later on…
More info over at: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html


