Archive for the 'ActionScript' Category

Overriding methods

Tuesday, January 29th, 2008

This all seems very simple, and once you start designing things in any form of OO way you learn that you need to “extend” a core object and change some of the functions it implements. Generally, the “base” object (initial object) is called a “base class” or “superclass”. The object that extends it is called a “subclass”.

Subclassing is simply a general term for extending an object.

You will almost certainly run into error messages when attempting these extensions. Basically you will have to make sure the base class methods you are extending fit the following rules:

  1. NOT MARKED AS FINAL: The instance method is not declared with the final keyword in the base class. When used with an instance method, the final keyword indicates the programmer’s intent to prevent subclasses from overriding the method.
  2. NOT PRIVATE: The instance method is not declared with the private access control specifier in the base class. If a method is marked as private in the base class, there is no need to use the override keyword when defining an identically named method in the subclass, because the base class method will not be visible to the subclass.

I would add my own to this list:

  1. NOT STATIC: Static methods are both NOT INHERITED and NOT OVERRIDABLE

Adobe has put a great document together to outline this and many other things relating to inheritance.

http://livedocs.adobe.com/labs/flex3/html/help.html?content=04_OO_Programming_11.html

Binding to Getter/Setter methods

Monday, January 28th, 2008

This is a tough one to write about. I can’t say I agree with the outcome, as it’s overly complex. But it’s what there is:

 

The problem:

I had to have a “label” on an object that was read-only but bindable. Basically, this means that I had to write a getter method for label. But as I found out, the flex compiler does not enable binding on read-only properties. This means if you create a getter method without a setter method binding will not fire. At all. Ever.

 

Even if you define your class as [Bindable]. So that I’m clear on this:

 

READ-ONLY PROPERTIES ARE NOT BINDABLE!

 

Okay, now that I’ve said that clearly. Let me tell you how I solved this. Recall I called it ugly.

 

All I had to do was create a setter method for the property which was private. That way external interfaces won’t be misled in believing they can “set” a value on the property (thus maintaining the read-onlyness).

 

Nice, huh?

 

image

ActionScript 3 Tips and Tricks

Thursday, January 10th, 2008

http://www.kirupa.com/forum/showthread.php?t=223798

 

I don’t know how I missed it, but it has a great list of AS3 functions and tips. Give it a scour

URLLoader stream error handling

Thursday, January 10th, 2008

Actually, this has to do with asynchronous error handling, the URLLoader is just a common example of it.

 

Basically, since the call is async a try..catch..finally block won’t do you a beans bit of good. It takes a while for the loader to determine the url is bad and then it dispatches the IO_ERROR event.

 

What this means to your app is that if you don’t listen and handle this event the application will throw ugly errors.

 

To handle this do something along the lines of:

 

 

var loader:URLLoader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
function catchIOError(event:IOErrorEvent){
trace(”Error caught: “+event.type);
}
loader.load(new URLRequest(”Invalid XML URL”));
trace(”Continuing with script…”);

rollOver and rollOut — ignore those child mouse events!

Thursday, January 10th, 2008

Finally, someone has heard the frustration of all of us. We use mouseOver and mouseOut on an HBox and have to either handle all child mouseOver and Outs ourselves, or do the old mouseEnabled = false nonsense — which isn’t usually what we want anyway.

 

spriteButton.addEventListener(MouseEvent.ROLL_OVER, over);
spriteButton.addEventListener(MouseEvent.ROLL_OUT, out);

 

As found on:

http://www.kirupa.com/forum/showthread.php?p=1948052#post1948052

 

Both sets of events determine when a mouse enters or leaves the graphics area of an interactive object. The rollOver and mouseOver events fire when the mouse comes in contact with an interactive object, while rollOut and mouseOut occur when the mouse leaves the interactive object.
Where they differ is with their interaction with interactive object children. The roll events (rollOver and rollOut) simplify the process and prevent interference with child events. Essentially, this is the same as using mouseOver and mouseOut with mouseEnabled set to false. mouseOver and mouseOut with mouseEnabled provide a parent sprite with events from its children. rollOver and rollOut keeps the events on the parent object.

Detecting when the mouse leaves your movie

Thursday, January 10th, 2008

I have tried tons of “mouseOut” and lose-focus types of solutions to deal with my AIR applications losing focus. Well, as of ActionScript 3 I find there is a new event (on the stage itself) which is published when the mouse leaves the movie.

 

stage.addEventListener(Event.MOUSE_LEAVE, myMouseOutFunction);

Finally!

Dispatching an event — custom style

Thursday, January 10th, 2008

Let’s say you just wrote a great AS3 class that extends something non-UI. Now when you’re done you want to dispatch an event but you don’t inherit “dispatchEvent” since you’re outside of UI classes (which inherit the event dispatcher).

 

This one has plagued me. In fact there are so many classes in my projects which shouldn’t be extending anything but I ended up extending UIComponent just to get the event dispatcher. Well, I’m here to learn you up on not having to do this hack.

 

ActionScript Code:
package {
   
    import flash.display.Sprite;
    import flash.events.Event;
   
    public class MyDispatcher extends Sprite {
       
        public function MyDispatcher() {
            var dispatcher:CustomDispatcher = new CustomDispatcher();
            dispatcher.addEventListener(“customEvent”, handleEvent);
            dispatcher.dispatchEvent(new Event(“customEvent”));
        }
       
        private function handleEvent(event:Event):void {
            trace(event.type); // “customEvent”
        }
    }
}

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

class CustomDispatcher implements IEventDispatcher {

    private var eventDispatcher:EventDispatcher;
       
    public function CustomDispatcher() {
        eventDispatcher = new EventDispatcher(this);
    }
   
    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
        eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }
    public function dispatchEvent(event:Event):Boolean {
        return eventDispatcher.dispatchEvent(event);
    }
           
    public function hasEventListener(type:String):Boolean {
        return eventDispatcher.hasEventListener(type);
    }
           
    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
        eventDispatcher.removeEventListener(type, listener, useCapture);
    }
           
    public function willTrigger(type:String):Boolean {
        return eventDispatcher.willTrigger(type);
    }
}

from http://www.kirupa.com/forum/showthread.php?p=1899603#post1899603

AIR: List of all opened windows

Wednesday, January 2nd, 2008

I was hunting around today for all opened windows in my application. Seemed obvious enough and something Adobe would have included in the environment — and they did. But it was not something simple to find a reference to, so I thought I’d throw this post out there in the ether. Maybe someone else will find it useful.

 

var windows:Array = NativeApplication.nativeApplication.openedWindows;

 

image

 

Beautiful. And this opens up some other interesting possibilities:

 

Represents this native AIR application.

The NativeApplication class provides application information, application-wide functions, and dispatches application-level events.

NativeApplication is a singleton object, created automatically at application startup. Get the NativeApplication instance of an application with the static property NativeApplication.nativeApplication.

(from the api)

 

There are some interesting items in there:

 

Of course I was already using several of these for my tray-icon and menus and such, but I didn’t know the window list was in here too.

Inspect-ability

Monday, December 31st, 2007

Most of us, by now, know all about making things [Bindable] in our objects. There’s another FlexBulder trick in town that I see far too few folks using:

 

Inspectable

 

imageThis is the feature that gives the user the wonderful drop-menu of available options when they ask for code-assistance (control-space):

 

This feature is very easily accomplished and if you get used to it you will make all of your coding a bit tighter and quicker — if you ask me.

 

I’ve dug around and found a few examples of this tag but I’m sure there are more, so please reply with your examples as well!

 

For a Boolean

[Inspectable(attribute=true,false)]
public var myVar:Boolean    = false;

 

 

For a String

[Inspectable(enumeration="red,green")]
public var myApple:Boolean    = “red”;

 

Now, I’d just love it if someone would explain the true differences between “attribute” and “enumeration”. As well as the fact that there are other properties you can include in these tags

  • defaultValue
  • verbose
  • category

 

And more. It’s an odd tag and it’s usage can be confusing. But using it as above at the minimum I have enabled code-assist throughout my programs.

 

For more information:

http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001658.html

 

And some explanations

image