Flash automatically names objects on stage "instance#"
I have 2 TLF text boxes already placed on my main stage. In the property inspector window I give these the instance names: "txt1" and "txt2".
I am trying to have a single mouseup event, and figure out which text box it occurred on.
My document class has the following code:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class SingleEvent extends Sprite{
public function SingleEvent() {
// constructor code
root.addEventListener(KeyboardEvent.KEY_UP, textChanged,false,0,true);
}
private function textChanged(e:KeyboardEvent){
trace(e.target.name);
trace(" " + e.target);
switch(e.target){
case txt1:
trace("txt1 is active");
break;
case txt2:
trace("txt2 is active");
break;
default:
break;
}
}
}
}
Example output is开发者_如何学运维:
instance15
[object Sprite]
instance21
[object Sprite]
Since the objects are already on the stage, I am not sure how to get flash to recognize them as "txt1" and "txt2" instead of "instance#". I tried setting the .name property, but it had no effect.
In the publish settings, I have "Automatically declare stage instances" checked.
Also, is it possible to have a single change event for multiple slider components? The following never fires:
root.addEventListener(SliderEvent.CHANGE, sliderChanged,false,0,true);
Thanks for any tips
It seems that the Flash Player hasn't initialized the TLFTextFields by the time the constructor is called. Try this:
package {
import flash.display.MovieClip;
import flash.utils.describeType;
import flash.events.Event;
public class TestTLF extends MovieClip {
private function onEnterFrame (ev:Event) : void {
removeEventListener (Event.ENTER_FRAME, onEnterFrame);
for (var i:int = 0; i < numChildren; i++) {
var obj:Object = getChildAt(i);
trace (obj.name + ":"+describeType(obj));
}
}
public function TestTLF() {
addEventListener (Event.ENTER_FRAME, onEnterFrame);
}
}
}
You will find that in addition to the mysteriously created additional objects "__id#" and "instance#", both of your TFLTextFields are recognized.
Also, your Slider components obviously don't "bubble" their SliderEvents, which means they never reach the root object. You can, however assign a handler function as many times as you want, so you could just add listeners to each of your sliders, all referring to the same sliderChanged
function.
精彩评论