01 Sep 2010

Using getDefinitionByName() with External Library SWCs

2 Comments Flex/Flash, programing

In a recent project I was forced to dynamically generating UI Elements in flex from Strings. This is typically done using getDefinitionByName() as shown bellow. Although this works, in my app I never really know what the string is, nor do I know what the Class is named or where it is located. This becomes an issue since you must make reference to the Class somewhere in your code before it is available to the Flex framework.

This example shows the typical use of getDefinitionByName() with out any external SWC. Notice the imports and class references on the top of the Script block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< ?xml version="1.0" encoding="utf-8"?>
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:controls="com.controls.*">
 
	<mx:script>
		< ![CDATA[
			import flash.utils.getDefinitionByName;
 
                       //You must import the classes and instantiate them
			import com.controls.TestControlA;TestControlA;
			import com.controls.TestControlB;TestControlB;
 
			private function loadTestClass(name:String):void
			{
			var ClassReference:Class = getDefinitionByName(name) as Class;
           		var s:DisplayObject = (new ClassReference() as DisplayObject)
           		testContainer.addChild(s);
			}
		]]>
	</mx:script>
 
	<mx:vbox id="testContainer" />
 
	<mx:hbox id="buttonBar">
<!--You have to place the entire package location in the string-->
		<mx:button label="Test A" click="loadTestClass('com.controls.TestControlA')" />
		<mx:button label="Test B" click="loadTestClass('com.controls.TestControlB')" />
	</mx:hbox>
</mx:application>

This won’t work since I need to make a reference to Classes that I won’t know the names of till run-time. My first step was to move a control into an external SWC, however I still am forced to make reference to the class so it becomes available, or I will get an error. This is a no go.

Read more

Popularity: 7% [?]

18 Aug 2010

Handling External SWF Events in Flex using SWFLoader

6 Comments Examples, Flex/Flash, programing, Uncategorized

It has been a while since i’ve written about Flex development so I figured i would post up some useful code for anyone interested. Recently I have been dealing with a lot of 3rd party code which relies on a huge amount of external SWF assets which load into Flex using SWFLoader. The issue i ran into is that the developer getting access to <mx:Application /> from external SWF using the MovieClip(root) approach. This approach is a slight change to the old _root of the gloomy AS2 days. This created an extremely tightly coupled app, with mysterious methods scattered around the Flex project. There was no way of knowing who was calling these methods, if they where deprecated, or if it was actually being used by some external SWF. Did I mention that there are 60 SWFs all with code in the timeline. It is like finding a needle in a haystack. Unfortunately for me, I wasn’t aware how tightly coupled these external swfs where and quickly proceeded to break the application whenever I attempted to refactor any code.

Obviously, calling <mx:Application /> to fake the old _root method of communicating with nested SWFs is not exactly the best way to go about this. Instead I favor using and Event driven approach.

This the code to create a simple external SWF with 5 textfields. Each textfield is clickable and will dispatch a custom event which broadcast the value of the textfield.
Read more

Popularity: 17% [?]

20 Aug 2008

Change Twitter Status in Flex

No Comments Examples, Flex/Flash, Social Media

I’ve been spending a lot of time using twitter, so thought i may as well create a quick little app for changing my status. I’ll be implementing this in a couple experiments that i am going to be working on in the near future. For now, here is a quick little interface for you to tweet me or anyone else.

Unfortunately my company will not allow be release this source yet… :(

Popularity: 1% [?]

20 Aug 2008

Using Bit.ly for short URL’s in Flex

2 Comments Examples, Flex/Flash

When character counts are critical as in building a twitter or SMS app you could use services such at tinyurl.com and bit.ly to help save text real-estate. I prefer bit.ly since it’s domain has less characters then tinyurl’s

The code is really quite simple. All that you need to do is communicate with bit.ly ‘s API and it returns the new shortened URL via its API. Check out http://bit.ly/docs for full API.

original call
http://bit.ly/api?url=URL (Example: http://bit.ly/api?url=http://twitabit.com)

bit.ly response:

http://bit.ly/2oCfqc

Seems easy enough, but the problem comes when doing this with in Flex. You will quickly see once you test on your server Flex will alert you that there is a Sandbox violation. This simply means that bit.ly does not have a crossdomain.xml file that grants you permission to receive data from them.
Read more

Popularity: 2% [?]

Get Adobe Flash playerPlugin by wpburn.com wordpress themes