Skip to content


Focus Events

So I found this little demo within the Adobe docs, so I decided to add it to my site for future reference. ( I wish I saved the link to the original files, when I find it I will post it)

package {
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.FocusEvent;
    import flash.events.IEventDispatcher;

    public class FocusEvents extends Sprite {
        private var gutter:uint = 50;
        private var childCount:uint = 5;

        public function FocusEvents() {
            var child:Sprite;
            for(var i:uint; i < childCount; i++) {
                child = new CustomSprite();
                configureListeners(child);
                addChild(child);
            }
            refreshLayout();
        }

        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
            dispatcher.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
            dispatcher.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, mouseFocusChangeHandler);
        }

        private function refreshLayout():void {
            var ln:uint = numChildren;
            var child:DisplayObject = getChildAt(0);
            var lastChild:DisplayObject = child;
            for(var i:uint = 1; i < ln; i++) {
                child = getChildAt(i);
                child.x = lastChild.x + lastChild.width + gutter;
                lastChild = child;
            }
        }

        private function focusInHandler(event:FocusEvent):void {
            var target:CustomSprite = CustomSprite(event.target);
        }

        private function focusOutHandler(event:FocusEvent):void {
            var target:CustomSprite = CustomSprite(event.target);
        }

        private function mouseFocusChangeHandler(event:FocusEvent):void {
            var target:CustomSprite = CustomSprite(event.target);
        }
    }
}

import flash.display.Sprite;
import flash.events.MouseEvent;

class CustomSprite extends Sprite {
    private var size:uint = 100;
    private var bgColor:uint = 0x336699;

    public function CustomSprite() {
        buttonMode = true;
        useHandCursor = true;
        addEventListener(MouseEvent.CLICK, clickHandler);
        draw(size, size);
    }

    private function draw(w:uint, h:uint):void {
        graphics.beginFill(bgColor);
        graphics.drawRect(0, 0, w, h);
        graphics.endFill();
    }

    private function clickHandler(event:MouseEvent):void {
        var target:Sprite = Sprite(event.target);
        stage.focus = target;
    }
}

Posted in Actionscript 3, Flash Components.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.