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″]

Simple tip #1 – custom events

Today while coding I was creating some classes that required them to dispatch custom events. I know that this is a fairly common thing to do but sometimes it is these little things that can trip you up or take a while to find out how to do them.

So I thought that each time I come across something that is ‘simple’ (only simple after you know it!) that I’ll try to create a quick blog entry and take note of it.  Each time I create a new ‘tip’ post I’ll link it to the previous/next tip so that it will be quick and easy to browse through a load of tips.
 

Tip #1

So for my first tip, this is how to implement your own custom events.

First if you are firing the events from a custom MXML file then you need to create a metadata tag. I make this the first node inside the MXML file.  For example:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
		width="500"
		height="300"
		>
 
<mx:Metadata>
<!-- 
First two event are plain string events, they do not pass any specific data with them -->
<!-- 
The last event is a custom class that extends Event and as such you need to give
it its package name + the class name as the type -->
	[Event('next')]
	[Event('previous')]
	[Event(name='jump', type='com.kennethsutherland.events.JumpEvent')]
</mx:Metadata>
...

If your custom class is an AS3 file then you would put something like the following are the imports

[Event(name="previous", type="flash.events.Event")]
[Event(name='jump', type='com.kennethsutherland.events.JumpEvent')]

Then inside the MXML file (script block) or anywhere in the AS3 file to fire the event I’d do the following:
 

//custom event, the extra value is handled by the JumpEvent class
dispatchEvent(new JumpEvent("jump", specificValueForTheJumpEventClass));
//standard event
dispatchEvent(new Event("next"));

If you do the above and lets just say your MXML/AS file is called ‘GreatComponent’ then in order to use the new custom event, its as simple as the below bit of code. 

<local:GreatComponent
    next="doSomething()"
    jump="doSomethingElse(event)"
/>

That’s it, now you can fire of any custom event that you wish and make sure that it gets listened to.

Next Tip

 

[ad name=”ad-1″]