So I needed global KeyboardEvent handling in AS3 project, but ran into problem – stage losses focus in couple of scenarios..
The fact that flash has no global object that never loses focus just SUCK!!!
I found in documentation that in Flex you can put your event on application object and solve this problem, but because I hate Flex – I can’t be bothered.
So possible solution I found all over the internet is force focus back to stage:
stage.focus = stage;
But this method creates another problem if you actually need focus on something not stage.(like text input fields).
My approach makes KeyboardEvent listeners travel with focus, from object to object that gets it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| package {
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
public class KeyBoardFocusMain extends Sprite {
private var focusObject:InteractiveObject;
public function Main():void {
setFocusObject(this.stage);
}
private function setFocusObject(newFocusObject:InteractiveObject):void {
if (focusObject){
focusObject.removeEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
focusObject.removeEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
focusObject.removeEventListener(FocusEvent.FOCUS_OUT, handleFocusChange);
}
focusObject = newFocusObject;
//
focusObject.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
focusObject.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
focusObject.addEventListener(FocusEvent.FOCUS_OUT, handleFocusChange);
}
private function handleFocusChange(event:Event = null):void {
if (focusObject != stage.focus){
if (stage.focus != null){
setFocusObject(stage.focus);
} else {
setFocusObject(stage);
}
}
}
private function handleKeyUp(event:KeyboardEvent):void {
trace("handleKeyUp:", event);
switch (event.keyCode){
//...
default:
break;
}
}
private function handleKeyDown(event:KeyboardEvent):void {
trace("handleKeyDown:", event);
switch (event.keyCode){
//...
default:
break;
}
}
}
} |
Hope it helps, have fun.
Hi,
in development – good debug environment is crucial. As I like FlashDevelop I am limited to trace output window. (And have to switch of Flash Builder if I want to do profiling or step-through debugging.)
Simple trace output is not the best way to trace stuff! So I used 2 logging tools for a while : SOSmax and Alcon.
Both has some advantages and disadvantages.. but none meets all my expectations!
Alcon has build in FPS counter, which is cool, and object inspector. But it lags and cuts trace messages… so it makes this tool not good for my needs.
SOSmax is very powerful, shows trace messages fast and nicely, has message filter and search. But don’t ship good interface to use it. And doesn’t have some Alcon features I like.
I will try to write User stories for perfect tracer I imagine !
- I want tracer to output different level messages in different colors.( and do it instantly)
- I want ability to sent messages in ‘row’ form. (Create socket, and send my custom html formated messages.)
- I want ability to sent messages in ‘global’ form. (Have a class to hide ‘row’ form, with static functions for messages and other stuff..)
- I want ability to sent messages in ‘local’ form. (Instantiate local logger object, to be able to tract the source of messages.)
- I want ability to clear, save to txt file, copy all or portion of the log text.
- I want check buttons to switch on/off visibility for certain level messages.
- I want FPS counter, I want it to be visible only if I ask for it.
- I want ability to start more then one SWF files, and log trace messages in different logger window tabs
- I want ability to watch/un-watch variables(objects, arrays) in separate window, info should be refreshed by time I specify.
- I want ability to fold/unfold multi-line messages. And I want options to control which level messages to fold or not(on arrival).
- I want menu function, to select my project source folder path, and all logger files to be copied there.
- I want search text for string, and buttons to jump to next,prev find.
- I want ability to add time, level info to message log.
- support as3, and as2.
and I want it to be open source project…
I have prepared Away 3d material showdown example. It took awhile.. its just lot of work finding new job and all..
I have tried to write it in such a way.. that I could use it as a draft to create materials in my work.

[ SOURCE : a3dMaterials.zip] OR [ A3dMaterial.as ]
Again.. I feel like I can make good tutorial/reference file out of this.. so… stay in touch.
So… I was wondering witch class of AWAY 3d to gut next… end decided to create a quick class diagram to see hole picture. I use Enterprise Architect for application modeling and AS3 code generation, so I used it here, and produced picture so big that I end up creating flash application just to view it..
I hope you will find this diagram useful.

Stand alone image : Away_3d_Class_diagram.png
( PS : maybe you know application that exports diagrams as swf? )
I have been thinking about 3d in flash for a long time.. but it always seemed that its still not a good time: flash is too weak. or 3d engines had not enough power. But I always knew.. that day will come for me to dive in 3d world.
I have spend that day examining 3d engine list and end up in choosing between two most powerful open source engines : PaperVision and AWAY 3d . I know that PaperVision is more popular, but it seemed to me that currently AWAY 3d is at least one step ahead. I am sure I will learn PaperVision then time will come.. but for naw – I present you my first work in AWAY 3d.
I like learning stuff orderly and without rush, so it was naturally for me to start with exploring 3d primitives and what can be done with them. I started by reading nice flashmagazine.com tutorials for AWAY 3D and then with AWAY 3D livedocs at my side I come up this this result:

[ SOURCE: A3dPrimitives.as ]
(I use FlashDevelop with Flex SDK for coding…)
You will find instantiation for all away 3d primitives, and my draft of Object3D class possibilities. Some stuff I skipped as it involves topics I am planing to learn in the future.. or simply something I fail to understand, so I put all that in ‘unknown’ section.
I will update this draft with hopes that it may be useful for someone.. or maybe turn it to tutorial of some sort.
… next step… textures!