« April 2004 | Main | July 2004 »
June 30, 2004
[Flash Quirks] Double Clicking
Want your applications to support double-click events? Need to add a double-click event to a UI component? Unfortunately, neither Flash nor UI components directly support double-click events. However, we do in fact have a couple of different ways of implementing this type of functionality into our applications.
Capturing Single and Double Clicks
Catching a double-click isn't really all the difficult. All we have to do is use an interval to measure how many times a user clicks within a given length of time and if they click twice within that interval we can treat it like a double-click:
// On Timeline
// interval length between clicks
var doubleClickSpeed = 250;
// the actual interval
var clickInterval;
// clears interval
function endClickTimer(){
clearInterval(clickInterval);
clickInterval = null;
}
// called when the button / MovieClip is pressed
function select(){
// if interval exist then this is a double click
if(clickInterval != null){
trace("Double Clicked");
endClickTimer();
// if interval doesn't exist this is a single click
}else{
trace("Single Clicked");
clickInterval = setInterval(this,"endClickTimer",doubleClickSpeed);
}
}
MyButton.onPress = function(){
select();
}
In the above code the first click triggers the interval. Clicking again before the interval expires will trigger the double-clicked otherwise the interval expires and nothing more happens.
Capturing Single or Double Clicks
With the first Single and Double Click approach you'll always get a single click and double click when double clicking (which may or may not always be desirable). You can however work around this issue by slightly skewing the implementation:
// On Timeline
// interval length between clicks
var doubleClickSpeed = 250;
// the actual interval
var clickInterval;
// clears Interval
function endClickTimer(){
clearInterval(clickInterval);
clickInterval = null;
}
// handles single clicks
function click(){
trace("Single Click");
endClickTimer();
}
// called when the button / MovieClip is released
function select(){
// If interval exist then this is a double click
if(clickInterval != null){
trace("Double Clicked");
endClickTimer();
// set interval
}else{
clickInterval = setInterval(this,"click",doubleClickSpeed);
}
}
MyButton.onRelease = function(){
select();
}
In the above example the single click is handled by the interval once it expires. This way you will only ever get a single or double-click but never both. Also, note that the event is now triggered onRelease. This prevents the single-click from firing off before the mouse is actually released.
Catching Double Clicks in V2 Components
For components you could apply the same type of logic with the v2 (or v1) components by placing the double-click routine into an event callback:
// On Timeline
var clickInterval;
var lastSelected;
function endClickTimer(){
clearInterval(clickInterval);
clickInterval = null;
}
// called by the event
function change(){
var selected = List_lb.selectedIndex;
if(clickInterval != null && selected == lastSelected){
trace("Double Clicked");
endClickTimer();
}else{
if(clickInterval != null){
endClickTimer();
}
clickInterval = setInterval(this,"endClickTimer",250);
}
lastSelected = selected;
}
List_lb.dataProvider = new Array(1,2,3,4,5);
// Event Callback
List_lb.addEventListener("change",this);
One thing to look out for on List (or a subclass of List) is that the "change" event is called anytime you select a List item. This could cause the double-click event to fire off prematurely if the user where to select 2 different items within the interval. To avoid this we compare the currently selected index with the last selected index (also works for data grids).
On a side note I normally would use an EventProxy to handle the routing of events but for simplicity omitted its use.
For more information on using EventProxy objects check out Mike Chamber's Article.
Happy Coding!
-erik
Posted by erikbianchi at 01:17 AM | Comments (374)
June 23, 2004
[Flash Quirks] Nested Components
Can't get Components to work properly when nested inside of your own
component? Have to result to using attachMovie but hate having to
programmatically layout your GUIs?
The problem is that you're probably trying to reference your Child components in the constructor (or in a method that is called by the constructor) of your parent component. Child component's ActionScript is not initialized with the parent component because child hierarchies are not apart of your classes inheritance chain. To properly initialize and target child components make sure your Parent component inherits from MovieClip and place the initialization code for child components into an onLoad Event.
Example:
class ParentCom extends MovieClip{
Declares Child MovieClips/Components
private var ChildCom;
// Constructor
function ParentCom(){
// Parent Component Initialization Code
}
// onLoad Event Inherited from MovieClip
function onLoad(){
// Child Component Initialization Code
}
}
Because the onLoad event is invoked after your MovieClip has loaded, and child components placed in the first frame are apart of that load. Child Components are properly initialized!
-erik
Posted by erikbianchi at 09:14 PM | Comments (321)
June 22, 2004
[Flash Quirks] Introduction
Flash development has a lot of unique and sometimes quirky aspects to it that aren't always straightforward or have well documented (or easy to find) solutions.
Over the next couple of weeks my Flash Quirks series of articles will attempt to explain some of these common perplexities and will hopefully be able to save you some time and hair pulling during the development proccess.
Stay Tuned . . .
-erik
Posted by erikbianchi at 09:13 PM | Comments (384)
June 19, 2004
[OT] We're Going back to Cali
After nearly 2 years of living here in Atlanta, my wife and I have decided to pack our bags and head back to California.
Having grown up in California our entire lives we came out to Georgia for a change of pace and to try something different. It was a wonderful life experience for the both of us but we miss our friends, our family and the Californian life-style just too much to ignore.
We are and always will be Californians at heart. We grew up eating In & Out burgers, snowboarding in fresh powder without jackets on, taking weekend road trips to Vegas and hanging out at the beach in shorts and a t-shirt year around.
There is a lot we are going to miss about Georgia: the warm southern nights of spring, the tress in October, the amazing housing market, the cost of living, the southern hospitality and some of the most wonderful people we have ever met but home is where the heart is and our hearts never really left California.
-erik
Posted by erikbianchi at 04:05 AM | Comments (277)
June 17, 2004
MX Developers Journal: Shameless Plug
A little late but I just got my hands on a copy of the MX Developers Journal that I wrote an article for way back in the January edition. The article was a quick break down of AS2 for AS1 programmers.
You can still get the digital version here: http://sys-con.com/magazine/?issueid=270
Shortly after it's release, I realized that I totally forgot to mention that AS2 can compile down to Flash Player 6 (Doh!). Of course any specific Flash Player 7 feature would not be available.
-erik
Posted by erikbianchi at 04:11 AM | Comments (267)