Simple tip #3 – Utility SWC (library project)

Do you frequently use simple actionscript functions in more than one project. Perhaps you’ve got a function to do tracking, parse strings to dates, or return a random colour etc.

I’m sure most folk will have struggled to think of what project they last used a generic function ABC in as they could do with using it for project XYZ and would rather spend 30 mins searching for said function rather than rewrite it.

Well as explained below is a way to stop this from happening and hopefully save you a heap of time.

  • Step 1

    – Create a library project.

  • So using flexbuilder, go to File->New->Flex Library Project.
  • This will create an empty library project.
  • Step 2

    – add in your Utilty class to the library project.

  • To do this add/create a Utility actionscript file that contains your static functions. Save, and this should then automaticlaly create a SWC file inside the library bin folder. (If it hasn’t and the file added has no errors then check the project properties to make sure the newly added file is being included – see pic)

  • Step 3

    – add the reference of the newly created SWC file to any project you intend to use it in.

  • So go to your project you wish to use the SWC with, then select the projects properties and inside ‘Flex build path’ select ‘Add SWC’.
  • Browse to the the previous library project and select the SWC from its bin folder.

You are now done, any time you update the code in the library project, those changes will be reflected in any project that references the SWC file automatically.

Previous Tip

Next Tip

[ad name=”ad-1″]

Flex & Version control

Ever had a project where your client has some kind of odd/unusual caching issue with their servers that you have absoulutly zero control over?

Well if you have (which I have had) then the following swc file may help you to diagnose the problem. Its a very simple swc file that you can place inside your code and pass it a version number. Next time you build your app and deploy it to your clients servers you can type ‘version’ then whatever number you gave inside your code will appear inside an Alert box.

This has helped me sooooo many times, as after deploying some code, if they (client) appear to be seeing something different to what I’m seeing then I can get them to type ‘version’ and if it’s the old version number I have to tell them to clear their cache & wait for their internal servers to refresh (again I/they have no control over this), then a few hours later they will have the correct version.

So it just helps when you roll out a minor update and you say its fixed, then they come back and say that they can’t see the changes, you can then show that its the internal hardware and that they just have to wait to get the new swf.

As a little extra feature on the version SWC I’ve given it the ability to listen to your own functions should you wish. It works by listening to the keypress event on the stage (so their is nothing visual to worry about). By default the version number is shown by typing ‘version’ when the app has focus. If you wished it to do something else on a certain word or set of key presses (a-z, 0-9) then you would do the following:

 
//There is a addKeyListenerFunction and a removeKeyListenerFunction
addKeyListenerFunction('whateverSetOfKeyPressesYouWantToListenTo', someFunction);
//remove listener by passing in the word that you where listening for
removeKeyListenerFunction('whateverSetOfKeyPressesYouWantToListenTo');

Click here for runnable demo, right click for source code. (just make sure app has focus for it to work)

If you’re interested in file size, using the above example with and without the version SWC changed the file size by just under 3K (3057 bytes).

The version SWC is case insensitive, so if you ask it to listen to HELLOWORLD, this is the same as helloworld.

Get the SWC file here.

[ad name=”ad-1″]

Why flex? Why actionscript? Why flash?

Having done close to 4 years of flex programming I’ve been asked frequently ‘why use flex?’.

My normal response is well flex is for programmers and flash is for designers (generally speaking). But having never created a proper flash project I’ve not had a proper insight into flash, but that’s changed a wee bit in the past few days. How? Well I’ve been doing an actionscript only project so that it could be imported into a flash project.

I’m really surpised about all the stuff I take and use in flex that I just couldn’t use in the project code that was just for flash.

Main Issues & work arounds (if possible)

 

Embedded assets (ByteArrayAsset & BitmapAsset etc issues)
  • Why when you wish to use something from the start of your app should you have to load the asset. Well I’m sure if you’ve done flash for a while you’re used to either pulling something onto the stage or using the loader method if using actionscript.
  • Well I’m used to using the [Embed] metadata but try using that and you’ll find that it requires the flexSDK. Why!!! If you’re trying to do everything without flex then you’ll actually find that the reason it requires the flexSDK is as follows –

    IFlexAsset is a marker interface with the following meaning: if a class declares that it implements IFlexAsset, then that class represents an asset — such as a bitmap, a font, or a sound — that has been embedded in a Flex application. This interface does not define any properties or methods that the class must actually implement.

    So the reason you’ll find that you can’t use the [Embed] tag in a pure actionscript project is NO REASON. So should you get an error after using the [Embed] tag then all you need to do is create an empty class of whatever it can’t find and place it in a folder mx/core/ 

Tweens
  • There is a considerable difference between tweens for flex and tweens for flash. The flash tween isn’t anywhere near as flexible as the flex version 🙂. If you wish to change a value from say 0 to 100 using an easing function then in flex you just tell the tween here is the start value ‘0’ and here is the end value ‘100’ and I want the duration to be say 700 milliseconds. Then every time this updates I can do other things and on the last call I can make sure that the end value is what I’m expecting (see previous post on fuzzyness after rotations for why this is a good thing).  But with the flash versioned Tween I can’t do this.  The property I change must be part of an object and it must be changed using either seconds! or the timeline.  The flash tween does have a update event that you could listen to but I like the flex version that has a function that gets called for each update and an end update function call for the last value update.

    So what can you do, well flex is open source, so just take a look at the flex tween class and copy/paste and remove all flexEvent references and change any mx_internal to private.  If you still wish to use the flexEvents then just create your own event and fire them. Job done 🙂

Code hinting
  • Dammit why??? Surely they can put code hinting into CS4, why not? The lack of code hinting really must be to turn programmers away from CS4 and to use flexbuilder or simialr. No workaround for this other than if you want code hinting use flexbuilder.  You can edit your AS files that are in your flash project using flexbuilder, but why can’t you do this in CS4?
SWC’s
  • I belive its possible to create a SWC in flexbuilder and then load it into flash CS but I’ve not managed it yet. (when I do I’ll blog that)
Conclusion

If you can develop in flex, the do. Your life will be sooo much easier.

If you need code for a flash project then use flexbuilder to make it, try to keep as much of it separate from your flash project as possible. e.g. In my case my actionscript project accepted a Sprite from whatever the designer was doing in CS4 and that’s all it cared about so I could develop it 100% in flexbuilder using an actionscript project. (Then just pass code over to flash project to insert into project – use of a SWC would make this process better, working on it)

If you know the flex libraries and flash doesn’t have something but flex does then chances are you can just remove any mx.* references or fudge it (as in the case of the or tween issues)

If your designing something, then sure stick to flash.

[ad]