September 10th, 2010 by Kyle
Tags: , , , ,
Posted in: ActionScript, Flex


You can’t add event listeners to an object that is returned from a method and expect to receive the events that are dispatched within the same method that returns the object to which you are adding event listeners.

This on is perhaps not so obviously stated, but can be made more clear via some code (hopefully):

  1.  
  2.  
  3. myObject:MyObject = new Object(fooParam);
  4. myObject.addEventListener("myEventType", myCallBackFunction);
  5.  
  6. //where MyObject Class looks like this:
  7.  
  8. public class MyObject extends EventDispatcher
  9.         {
  10.                 // constructor
  11.                 public function MyObject (fooParam:String, target:IEventDispatcher=null)
  12.                 {
  13.                         super(target);
  14.                         // do some stuff and then dispatch event
  15.                         dispatchEvent(new MyEvent(MyEvent.MY_EVENT_TYPE));
  16.                 }
  17.  

This just won’t work.

Better to do something like:

  1.  
  2.  
  3. myObject:MyObject = new Object();
  4. myObject.addEventListener("myEventType", myCallBackFunction);
  5. myObject.doStuff(fooParam);
  6.  
  7. //where MyObject Class looks like this:
  8.  
  9. public class MyObject extends EventDispatcher
  10.         {
  11.                 // constructor
  12.                 public function MyObject (target:IEventDispatcher=null)
  13.                 {
  14.                         super(target);
  15.                  }
  16.  
  17.                public function doStuff(fooParam:String):void {
  18.                         // do some stuff and then dispatch event
  19.                         dispatchEvent(new MyEvent(MyEvent.MY_EVENT_TYPE));
  20.                }
  21.  

That should work and not cause you to chase your tail trying to figure out why things are not getting dispatched or not being responded to by an eventListener
:)




2 Comments »

September 8th, 2010 by Kyle
Tags: , , , ,
Posted in: ActionScript, Flex


As a result of being back in the “land of the coding” (see post: http://blog.flexmonkeypatches.com/2010/09/08/back-in-the-land-of-the-coding/), I should begin easily to accumulate content for blog posts. Case in point last week, when I spun my wheels for what was longer than I should have, over dumb assumptions or misconceptions or misrememberings regarding some aspect of Flex/Flash/AS3. I thought I would start a new series of posts meant to capture such dumb assumptions as a reminder to myself ad hopefully as a cache of useful tidbits that others can come across when they are struggling with things that just aren’t working as they thought they should. Here are my first 2 entires in this the inaugural “Note to Self”:

AS3 Event Bubbling is only for objects on the display list!!

There may be reference to this in the docs and if I run across it, I will amend this post to include it. Now it would be nice if you could bubble events up the inheritance hierarchy or from one instance up to the class instance that contains the instance dispatching the event (and so on and so forth), but you can’t. There may be ways of doing this in a more clean manner, but really the underlying mechanism is really through a series of eventListeners that you must attach up through the hierarchy of object through which you want the events to pass. Don’t forget to implement the clone method of any custom events you are dispatching, otherwise you will run into trouble. Just implement clone and you can easily redispatch the event from within the listening function that receives the event.




2 Comments »

September 11th, 2008 by Kyle
Tags: , , , ,
Posted in: ActionScript, Flex


I have a few samples I am working on to demonstrate how to do some “drag selection” and I thought some of the base code I use in these samples would be good to post on its own.

So here it is. Pretty basic, but it will serve as the basis for a few samples that I should have out next week (hopefully).

This movie requires Flash Player 8

Download a zipfile containing the source to this sample.

Browse the source of this example.

Or continue into the blog entry to see the source:
Read the rest of this post»




2 Comments »

August 14th, 2008 by Kyle
Tags: , ,
Posted in: Uncategorized


“ECMAScript 4 standard, the standard on which AS3 is based, and the draft proposal for future versions of javascript has been rejected in favour of ECMAScript 3.1, which proposes small incremental changes to javascript for the foreseeable future.” (source: http://www.gskinner.com/blog/archives/2008/08/javascript_stal.html)

I’ve been following these blog posts and comments all day:
http://whydoeseverythingsuck.com/2008/08/ru-roh-adobe-screwed-by-ecmascript.html
http://www.gskinner.com/blog/archives/2008/08/javascript_stal.html
http://www.rockonflash.com/blog/?p=127

This kinda sucks.
I agree with most comments.

All I have to say is if Microsoft is IBM and Apple is Microsoft and Adobe is Apple, who is IBM again?

-Kyle




No Comments »

December 5th, 2007 by Kyle
Tags: , , , , , , , , ,
Posted in: ActionScript, Flex


XML and ampersands do not mix well.
Flex uses mxml which is XML.
This can lead to issues when writing binding expressions in mxml tags for component properties when you are trying to write logical expressions using ‘and’.

Here is a simple sample that demonstrates how to escape the ampersands in the expression and how to create a logical expression that does not even use ‘and’, but achieves the same result.

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  4.  
  5.         <mx:Script>
  6.                 <![CDATA[
  7.                         [Bindable]
  8.                         public var bOne:Boolean=false;
  9.                         [Bindable]
  10.                         public var bTwo:Boolean=false;
  11.                 ]]>
  12.         </mx:Script>
  13.  
  14.         <mx:Button label="(click to toggle) bOne: {bOne}" click="bOne=!bOne"/>
  15.         <mx:Button label="(click to toggle) bTwo: {bTwo}" click="bTwo=!bTwo"/>
  16.  
  17.         <mx:Label text="If bOne and bTwo are true, the buttons below will be Enabled"/>
  18.         <!–escape the ampersands for the logical and operator –>
  19.         <mx:Button label="enabled?" enabled="{(bOne &amp;&amp; bTwo)}"/>
  20.         <!–or use this compounded logical statement which will evaluate to the same thing –>
  21.         <mx:Button label="enabled?" enabled="{(bOne ? bTwo : false)}"/>
  22.  
  23. </mx:Application>
  24.  

Browse the source of this example.
Download a zipfile containing the source to this sample.

This movie requires Flash Player 8




5 Comments »

November 19th, 2007 by Kyle
Tags: , , , , , ,
Posted in: ActionScript, Flash Player


I had a customer who was having problems with linebreaks in her Flex code.
I had used linebreaks before with no problem until I realized that she was setting text with linebreaks in mxml and not setting properties dynamically as I had done in all instances in teh past when dealing with linebreaks.
I search the Adobe public bugbase and found this bug:

http://bugs.adobe.com/jira/browse/SDK-12649

I thought I’d write a simple app to demonstrate the problem and workarounds.

Here is the source for the app:

  1.  
  2.  
  3. <mx:application xmlns:mx="http://www.adobe.com/2006/mxml">
  4.         <mx:label id="ta1" height="75" width="250" text="This does not \nwrap">
  5.         <mx:button label="click to set via actionscript" click="ta2.text=’Setting via actionscript and\nthis does wrap’">
  6.         <mx:label id="ta2" height="75" width="250">
  7.         <mx:label id="ta3" height="75" width="250" text="Alternatively with this workaround,{‘\n‘}this wraps too">
  8. </mx:label>
  9. </mx:label></mx:button></mx:label></mx:application>

Browse the source of this example.
Download a zipfile containing the source to this sample.

This movie requires Flash Player 8




2 Comments »

Here is another good datagrid itemrender that I refer to when starting on datagrid renderer issues for customers.
It demonstrates how to create an itemEditor that is based on VBox and has child components (in this case a checkbox) and demonstrates how to implement the IDropInListItemRenderer and IFocusManagerComponent interfaces.
The sample also demonstrates how to add a listener to the underlying dataprovider arraycollection to detect change events and show what data has changed when edits are committed to the collection.

Here is the code for the application:

  1.  
  2.  
  3. <mx:application xmlns:mx="http://www.adobe.com/2006/mxml" creationcomplete="init()">
  4.     <mx:script>
  5.         <!–[CDATA[
  6.                 import mx.events.CollectionEvent;
  7.                 import mx.events.DataGridEventReason;
  8.             import mx.collections.ArrayCollection;
  9.                 import mx.events.DataGridEvent;
  10.                 import mx.events.ListEvent;
  11.  
  12.             [Bindable]–>            private var myAC:ArrayCollection = new ArrayCollection([
  13.                 {id:89, Contact: ‘Bob Jones’, FollowUp: true },
  14.                 {id:5, Contact: ‘Jane Smith’, FollowUp: true },
  15.                 {id:7, Contact: ‘Doug Johnson’, FollowUp: false },
  16.                 {id:15, Contact: ‘John Jackson’, FollowUp: true },
  17.                 {id:21, Contact: ‘Christina Coenraets’, FollowUp: true },
  18.                 {id:4, Contact: ‘Joanne Wall’, FollowUp: false },
  19.                 {id:461, Contact: ‘Maurice Smith’, FollowUp: false },
  20.                 {id:35, Contact: ‘Lorraine Barnes’, FollowUp: true },
  21.                 {id:61, Contact: ‘The Dude’, FollowUp: true },
  22.                 {id:56, Contact: ‘Abe Rockaway’, FollowUp: true }
  23.  
  24.             ]);
  25.  
  26.             private function init():void{
  27.                 myAC.addEventListener(CollectionEvent.COLLECTION_CHANGE, onChange);
  28.             }
  29.  
  30.             private function onChange(event:CollectionEvent):void{
  31.                 for (var i:int=0; i < event.items.length;i++){
  32.                         var obj:Object=event.items[i].source;
  33.                          for (var p:String in obj) {
  34.                                         if(p!="mx_internal_uid"){
  35.                                                 cellInfo.text += "\n";
  36.                                                 if(event.items[i].property==p){cellInfo.text +="*"}
  37.                                                                  cellInfo.text += p+": " + obj[p];
  38.                                         }
  39.                                                 }
  40.                 }
  41.             }
  42.  
  43.         ]]>
  44.     </mx:script>
  45.     <mx:datagrid id="myGrid">
  46.         dataProvider="{myAC}" editable="true" >
  47.         <mx:columns>
  48.             <mx:datagridcolumn datafield="Contact" width="150">
  49.                         </mx:datagridcolumn><mx:datagridcolumn datafield="id" width="150" editable="false">
  50.             </mx:datagridcolumn><mx:datagridcolumn datafield="FollowUp">
  51.                 width="150"
  52.                 headerText="Follow Up?"
  53.                 itemRenderer="DGCheckBoxEditor" rendererIsEditor="true"
  54.                 editorDataField="cbSelected"/>
  55.         </mx:datagridcolumn>
  56.    
  57.  
  58.     <mx:textarea id="cellInfo" width="300" height="150">
  59.  
  60. </mx:textarea>
  61. </mx:columns></mx:datagrid></mx:application>

Read the rest of this post»




11 Comments »

October 17th, 2007 by Kyle
Tags: , , , , , ,
Posted in: ActionScript, Flex


I recently had an issue from a customer regarding figuring out the correct syntax to get at a few nodes in an XML object that had namespaces using e4x. Now I don’t know about you, but if I don’t use e4x often, I forget things and get rusty. If your e4x-foo is weak, here are a few references that I found useful:

http://www.darronschall.com/weblog/archives/000223.cfm
(In fact Darren has a few e4x entries aside from this one.)

http://www.partlyhuman.com/blog/roger/as3-e4x-rundown

Hope this helps,

-Kyle




No Comments »

October 8th, 2007 by Kyle
Tags: , , , , , ,
Posted in: ActionScript, Flex


I had a customer that was trying to use masking to make menus appear with rounded corners. I had gotten this to work, but there was something I could not figure out that caused a flicker as menus were selected and appeared. I put this on the back shelf for a while and recently revisited it. To my surprise, the issue I had previously seen had gone away. Either something in the Flex framework had changed or a more recent version of the player had a beneficial effect. Whatever the cause, I decided it was good enough and that I should post this code.

In this sample, I extended the default MenuBar, so I could get a handle on new menus as they are shown to apply the mask. I also implemented a new style “corner-radius” and a custom menuItem renderer.

Browse the source of this example.
Download a zipfile containing the source to this sample.

This movie requires Flash Player 8

Read the rest of this post»




10 Comments »

What should the LineChart do in this situation?
How can you draw a line with only 1 data point much less extrapolate the data??

Turns out that the Flex chart does not handle that very well. The solution?? Write your own lineSegmentRenderer.

Here is an example of a lineSegementRenderer that will just draw a circle for this case.

MyLineRenderer.as:

  1.  
  2. package
  3. {
  4.         import mx.charts.renderers.LineRenderer;
  5.         import mx.charts.series.items.LineSeriesItem;
  6.         import mx.charts.series.renderData.LineSeriesRenderData;
  7.         import mx.charts.series.items.LineSeriesSegment;
  8.         import mx.charts.renderers.CircleItemRenderer;
  9.         import flash.display.Graphics;
  10.         import flash.geom.Rectangle;
  11.         import mx.charts.ChartItem;
  12.         import mx.charts.chartClasses.GraphicsUtilities;
  13.         import mx.core.IDataRenderer;
  14.         import mx.graphics.IFill;
  15.         import mx.graphics.IStroke;
  16.  
  17.         public class MyLineRenderer extends LineRenderer
  18.         {
  19.                 private static var rcFill:Rectangle = new Rectangle();
  20.  
  21.                 public function MyLineRenderer()
  22.                 {
  23.                         super();
  24.                 }
  25.  
  26.                 override protected function updateDisplayList(unscaledWidth:Number,
  27.                                                                                                           unscaledHeight:Number):void
  28.                 {
  29.                         var l:int=(data as LineSeriesSegment).items.length;
  30.                         if(l==1){
  31.                                 var item:LineSeriesItem=((data as LineSeriesSegment).items[0] as LineSeriesItem)
  32.                                 var fill:IFill = GraphicsUtilities.fillFromStyle(getStyle("fill"));
  33.                                 var stroke:IStroke = getStyle("stroke");
  34.  
  35.                                 var w:Number = stroke ? stroke.weight / 2 : 0;
  36.  
  37.                                 rcFill.right = 2;
  38.                                 rcFill.bottom = 2;
  39.  
  40.                                 var g:Graphics = graphics;
  41.                                 g.clear();
  42.                                 if (stroke)
  43.                                         stroke.apply(g);
  44.                                 if (fill)
  45.                                         fill.begin(g, rcFill);
  46.                                 g.drawCircle(item.x, item.y, 3);
  47.                                 if (fill)
  48.                                         fill.end(g);
  49.                         }
  50.                         else{
  51.                                 super.updateDisplayList(unscaledWidth, unscaledHeight);
  52.                         }
  53.                 }
  54.         }
  55. }
  56.  

Read the rest of this post»




9 Comments »

« Previous Entries