PDA

View Full Version : [AS3] Simple focus example



bootiteq
October 21st, 2008, 08:46 PM
A simple AS3 textField focus example which adds a custom selection glow around the textfeild when it is selected....

Paste into frame 1



var glowOn = new GlowFilter(0x0099ff,1,4,4,2,1,false);
var glowOff = new GlowFilter(0x0099ff,0,4,4,2,1,false);

function init() {
for (var i = 0; i<4; i++) {
var mc = createTextField();
mc.name = "tf_"+i;
addChild(mc);
mc.x = 30;
mc.y = 30*(i+1);
mc.addEventListener(Event.CHANGE,traceValue);
mc.addEventListener(FocusEvent.FOCUS_IN,showSelect ed);
mc.addEventListener(FocusEvent.FOCUS_OUT,hideSelec ted);
}
}

function createTextField() {
var tf = new TextField();
var format = new TextFormat();
tf.defaultTextFormat = format;
tf.width =200;
tf.height =20;
tf.background = true;
tf.border = true;
tf.type = TextFieldType.INPUT;
return tf;
}

function traceValue(e:Event) {
trace(e.target.name + ": " + e.target.text);
}

function showSelected(e:FocusEvent) {
e.target.filters = [glowOn];
}
function hideSelected(e:FocusEvent) {
e.target.filters = [glowOff];
}
init();

sekasi
October 21st, 2008, 10:22 PM
Try to keep class based programming when releasing examples since it sets a better example for people hunting for source. Frame programming is very 1995.

Also, make sure your functions are a bit more strict. If it has a return argument, the function should specify it; ie: private function createTextField():TextField {}.

In addition, be more strict in your coding.

e.target.filters would be better off as TextField(e.target).filters.

And keep your listeners as weak references.

bootiteq
October 21st, 2008, 11:23 PM
And keep your listeners as weak references.

Whoops I thought that was the default value - thanks.


e.target.filters would be better off as TextField(e.target).filters

Can you explain, or direct me to a place where I can find out why?

Cheers

prg9
October 21st, 2008, 11:29 PM
Frame programming is very 1995.

Hard to believe concerning Flash since it originated in 1996.
http://en.wikipedia.org/wiki/Adobe_Flash#History

bootiteq
October 22nd, 2008, 12:15 AM
^HA HA that is gold!!!!!

prototype
October 22nd, 2008, 09:57 AM
prg9 = ownage!

bootiteq
October 22nd, 2008, 11:42 PM
version 2 - as class - let me know of any way to improve this :elderly:

preview
http://www.whitecoatdesign.com/kirupa/showFocus/

source
http://www.whitecoatdesign.com/kirupa/showFocus/showFocus.zip




package {

// CREATES A GLOW AROUND A SELECTED ITEM
import flash.text.TextField;
import flash.filters.GlowFilter;
import flash.events.Event;
import flash.events.FocusEvent;
public class showFocus extends TextField {

var glowOn:GlowFilter;
var glowOff:GlowFilter;
var tf:Object;
public function showFocus(tFeild:Object):void {
glowOff = new GlowFilter(0x0099ff,0,4,4,2,1,false);
glowOn = new GlowFilter(0x0099ff,1,4,4,2,1,false);
tf = tFeild;
init();
}

private function init():void {
tf.addEventListener(FocusEvent.FOCUS_IN,showSelect ed,false,0,true);
tf.addEventListener(FocusEvent.FOCUS_OUT,hideSelec ted,false,0,true);
}

private function showSelected(e:FocusEvent):void {
tf.filters = [glowOn];
}

private function hideSelected(e:FocusEvent):void {
tf.filters = [glowOff];
}

}
}

sekasi
October 23rd, 2008, 01:11 AM
Hard to believe concerning Flash since it originated in 1996.
http://en.wikipedia.org/wiki/Adobe_Flash#History

way to take something literally captain obvious

prg9
October 23rd, 2008, 09:31 AM
way to take something literally captain obvious

:pir: Ummm... Your the one with the pirate captain as an avatar, captain. :thumb:

My point was (since it was not obvious to ya) was that not all code needs to be class based. Classes are wonderful however not every piece of code snippet needs to be within a class, especially in this forum where people are sharing snippets. Does the GTAL "Shared User Content" forum require only classes, it too says "snippets and classes"? So why go off on bootiteq and make the comment about 1995 and that it should be a class to begin with?

Anyway, now help out your fello Australian improve the class you instructed him to make.

sekasi
October 23rd, 2008, 07:14 PM
I'd say to strongly encourage every AS3 programmer to work strictly in an object based environment (not necessarily strictly OOP, but working with modular classes) is nothing else but a good thing. If I offend someone along the way, so be it. : )

The faster the flash community moves away from timeline and procedural thinking and global variables and typecasting root and all that.. the better.

And don't take the piss on my Avatar, Guybrush has never done anything wrong. :(

bootiteq
October 23rd, 2008, 08:53 PM
I am not in least bit offended - I welcome any help and especially new ideas. If the suggestion makes my workflow easier or work cooler/faster i'll use it.

So is the class OK?

Not %100 about whether I should have choosen textfield to extend for example as the class works on both textfields and MovieClips - perhaps Sprites as well.

Cheers

Lily2007
October 27th, 2008, 06:22 AM
programming when releasing examples since it sets a better example for people hunting for source. Frame programming is very 1995.

Also, make sure your functions are a bit more strict. If it has a return argument, the function should specify it; ie: private function createTextField():TextField {}.

dail
October 27th, 2008, 03:44 PM
You could make the class extend DisplayObject, rather than textField, as GlowFilters can be applied to any DisplayObject. You should probably type your tFeild var as DisplayObject. As the class is now, I could pass in any old generic object. You can also declare your vars access modifiers. At the moment they are internal, maybe keep them private, exposing on the ones you need to have exposed.

You could also let a user pass in the on/off colours via your constructor arguments. Perhaps set up some default values in there, so they don't need to, but could if need be. It would make the class a little more flexible.

I don't necessarily agree with the premise that all samples need to be classes. There are lots of people who use Flash who code on the timeline, it works and procedural code can perform better.

bootiteq
October 28th, 2008, 05:28 AM
I cant extend DisplayObject so I've kept it as TextField.

I want to keep it SIMPLE. I personally would keep the same color/glow values for a whole project and think it would be easier to just open this file and change directly. I have added thee ability to change to a different color just for an example.



package {

// CREATES A GLOW AROUND A SELECTED ITEM

import flash.display.DisplayObject;
import flash.text.TextField;
import flash.filters.GlowFilter;
import flash.events.Event;
import flash.events.FocusEvent;

public class showFocus extends TextField {

private var glowOn:GlowFilter;
private var glowOff:GlowFilter;
private var focusItem:DisplayObject;
private var glowColor:uint; // has a default but can be overwritten with a user defined color

public function showFocus(fi:DisplayObject, glowColor = 0x0099ff):void {
glowOn = new GlowFilter(glowColor,1,4,4,2,1,false);
glowOff = new GlowFilter(glowColor,0,4,4,2,1,false);
focusItem = fi;
init();
}

private function init():void {
focusItem.addEventListener(FocusEvent.FOCUS_IN,sho wSelected,false,0,true);
focusItem.addEventListener(FocusEvent.FOCUS_OUT,hi deSelected,false,0,true);
}

private function showSelected(e:FocusEvent):void {
focusItem.filters = [glowOn];
}

private function hideSelected(e:FocusEvent):void {
focusItem.filters = [glowOff];
}

}
}



eg: frame script excerpt assigning to movieclips and textfields.


new showFocus(tfa); // default blue glow
new showFocus(tfb); // default blue glow
new showFocus(tfc,0xff0000); // user defined red glow

Tical
February 17th, 2009, 03:20 PM
Hard to believe concerning Flash since it originated in 1996.
http://en.wikipedia.org/wiki/Adobe_Flash#History


Good I'm glad someone else also realized that this guy was a numbnut!...if you want it in a Class...then put it in one...this is showing how it works...its fine w/out being in a class!...Google took me straight to the answer I wanted..which was found here!...Thanks!