Flex 4: pass MXML application components reference to external classes

Using Flash Builder (Flex 4 SDK) I want to controll my application in an external class, just to make code more organized.

I want to access a button (or any other component)  and change its enabled status to disable, for example.

My application MXML looks like

[code="xml"]

<?xml version="1.0"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">

<s:layout>
<s:BasicLayout/>
</s:layout>

<fx:Script>
<![CDATA[
import myClass;
protected function myButton_clickHandler(event:MouseEvent):void
{
myClass.myFunction(this);
}
]]>
</fx:Script>

<s:Button id="myButton" label="Click Me"
click="myButton_clickHandler(event)"/>
</s:Application>

[code]

and my external class looks like:

[code="as3"]

public class myClass
{
public static function myFunction(application:Object):void{

// disable the button
application.myButton.enabled = false;
}
}

[code]

this way you can access all the components of your mxml application.

Mixing getChildByName and gotoAndStop

Sometimes I want to reference objects by thier instance names, and want to access some frames inside this objects (Movieclips), her’s how you mix the two;


// button animator
function animate_this(brother:SimpleButton) {
// add event listners to this button
brother.addEventListener(MouseEvent.MOUSE_OVER, animate_mc_over);
brother.addEventListener(MouseEvent.MOUSE_OUT, animate_mc_out);
brother.addEventListener(MouseEvent.MOUSE_DOWN, animate_mc_down);
brother.addEventListener(MouseEvent.MOUSE_UP, animate_mc_out);
var brother_name:String=brother.name;
var brother_name_regx:RegExp=/btn_/g;
var target_name:String=brother_name.replace(brother_name_regx,"mc_");

// animate the coresponding clip
function animate_mc_out(event:MouseEvent):void {
MovieClip(brother.parent.getChildByName(target_name)).gotoAndStop(1);
}
function animate_mc_over(event:MouseEvent):void {
MovieClip(brother.parent.getChildByName(target_name)).gotoAndStop(2);
}
function animate_mc_down(event:MouseEvent):void {
MovieClip(brother.parent.getChildByName(target_name)).gotoAndStop(3);
}
}

Dynamic text inside buttons and masks

When working in flash and programing as3, you have to think of the whole project from start to finish befor you write the first line of code or draw the first pixel in your timeline!

I’m the sort of guys who start a project by spliting it to parts/steps, but I found this methode not working good, lets examin this scenario toghter;
- in the beging of the project, the client didn’t tell if the site will be multilangual, so I created buttons on the timeline and animated the frames using masks.
- when the project is finished and working, the client come again and ask me to make the project multilangual, that means the texts inside buttons should be dynamic!
i taught it’s easy, but I was wrong, I discouvered that dynamic text inside buttons to be able to work, ther should be ;
* textfields should always be in the first keyframe, also the button containg this textfield or parent buttons should be in the first frame
* ther should be no timeline masks over the button, if you want masking create them dynamicly

that’s a big probleme since i created many buttons on the timeline, so i have to redo the whol stuff!!

AS3 is a language that takes to to the next level in flash authoring/programing; before as3, we really don’t care about creating buttons or creating movieclips and turning them to buttons, but now, you should think a lot befor going to the next step.

the weird function position !

I come across a weird problem when I wanted to delete some event listners:


public class test extends MovieClip
 {
 var already_loaded:Boolean = false;
 public function test ( )
 {
 reload ();
 btn.addEventListener (MouseEvent.MOUSE_DOWN, reload);

function reload ()
 {
 if (already_loaded == true)
 {

trace ("a hasEventListener "+mc.hasEventListener(MouseEvent.MOUSE_DOWN));
 mc.removeEventListener (MouseEvent.MOUSE_DOWN, tracer);
 trace ("b hasEventListener "+mc.hasEventListener(MouseEvent.MOUSE_DOWN));
 }

mc.addEventListener (MouseEvent.MOUSE_DOWN, tracer);
 function tracer (event:MouseEvent):void
 {
 trace ("ignit.");
 }

}
 already_loaded = true;
 }
 }

this code doesn’t work, but when i change the position of the function tracer(), it works


<pre>public class test extends MovieClip
 {
 var already_loaded:Boolean = false;
 public function test ( )
 {
 reload ();
 btn.addEventListener (MouseEvent.MOUSE_DOWN, reload);

function reload ()
 {
 if (already_loaded == true)
 {

trace ("a hasEventListener "+mc.hasEventListener(MouseEvent.MOUSE_DOWN));
 mc.removeEventListener (MouseEvent.MOUSE_DOWN, tracer);
 trace ("b hasEventListener "+mc.hasEventListener(MouseEvent.MOUSE_DOWN));
 }

mc.addEventListener (MouseEvent.MOUSE_DOWN, tracer);
 }
 function tracer (event:MouseEvent):void
 {
 trace ("ignit.");
 }

already_loaded = true;
 }
 }</pre>

int vs Number vs uint

In computing, signedness is a property of variables representing numbers in computer programs. A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent positive numbers.

by senocular

The general concensus is, use int when you’re not dealing with division or multiplication by decimal values. Generally, they’re best used for values used with +/- such as increment variables in loops.

Number is used for everything else but color which (esp when dealing with Alpha) you should use uint - in fact thats about the only time you should use uint.

by vijayram
As you are aware, Number data-type can accepts decimal values and hence floating-point operator(multiplication and division) is quicker when data-typed to Number.
So to avoid this performance overhead of implicit conversion, its better to data-type your variable to “int” during addition and subraction and whilst division and multiplication is involved use “Number” data-type.

Integrating flash swf into HTML: best practise

Ther’s a lot of methodes to embed your swf flas document inside an html page, the best practise is to use SWFOBJECT javascript based solution as it is compatible with older navigators, also it allows you to control the way your swf interacts with html pages and is well documented.

for sending dynamic data from html to swf, give a look to :

Accessing FlashVars and HTML Parameters

How do you use JavaScript Objects to define your flashvars, params and object’s attributes?

Tips for things A-ne-pas-faire in flash

you should avoid doing the folowing habits/behaviors when dealaing with Flash:

  1. Never create animations directly on root of the timeline, better create a movie clip for your animations, then insid this created MC, insert your animation. This way, you can easily re-position your animation on the timeline (rather than modifying the position of every keyframe on your animations).
  2. Organize your timeline/library;  don’t create un-necessary MC’s, give items appropriate names.

Instance Names for Programmatically Created Display Objects

As it happens, like manually created instances, display objects created programmatically can also be assigned an instance name via the name variable. For example, the following code creates a TextField instance, gives it the instance name “price,” adds it to a container, and then retrieves a reference to it by name.


var t:TextField = new TextField( );
t.text = "$99.99";
t.name = "price"
var detailsPage:Sprite = new Sprite( );
detailsPage.addChild(t);
trace(detailsPage.getChildByName("price"));
// Displays: [object TextField]

Continue reading