interaction with keyboard/textfields help please
Normally for a string to object is converted as follows.
var obj:object=getChildByName("string");
And we can give properties to it like obj.x=100;
But in the case of a series of stings
[objet Stage].[object MainTimeline].[object TextField]
it wil not works.Actually i need to give properties to a target path which is a string what i do?? Here is the开发者_如何学编程 code to get path to a movieclip:
addEventListener(MouseEvent.CLICK, targetMC);
function targetMC(MouseEvent:Event):void
{
var curinstance = MouseEvent.target.valueOf();
var targ:Object = curinstance.parent;
var path = curinstance;
do
{
if (targ == "[object Stage]")
{
path = targ + "." + path;
}
else
{
path = targ + "." + path;
}
targ = targ.parent;
} while (targ);
trace(path);
}
i would like to give properties to path.
A number of things are awkward about your code:
Don't compare the string value of objects to find out about class type. Use the
is
keyword:if (obj.parent is Stage) doSomething();
Don't use class names as parameter names: MouseEvent is a type!
function targetMC ( ev:MouseEvent ) // ...more code
It is useful to name handler methods according to the event upon which they are invoked, for example:
function onMouseClick (ev:MouseEvent)
or
function mouseClickHandler (ev:MouseEvent)
If you can avoid it, don't cast to
Object
to access members, but try to use subclass types - it allows the compiler to more effectively check your code for errors. Since all objects in the display list are instances ofDisplayObject
, you could use this:var obj:DisplayObject = ev.target as DisplayObject;
If you want to output a path to your object, use instance names instead of types - you might have more than one TextField!
private function getObjectPath (obj:DisplayObject) : String { var path:String = obj.name; if (obj.parent != null && !(obj.parent is Stage)) { path = getObjectPath (obj.parent) + "." + path; } return path; }
Now for your answer: Use the KeyboardEvent.
textField.addEventListener (KeyboardEvent.KEY_UP, onKeyUp);
and
private function onKeyUp (ev:KeyboardEvent) : void {
var tf:TextField = ev.target as TextField;
var text:String = tf.text;
tf.text = text + String.fromCharCode(charCode);
}
Note that this will only work as long as the TextField has focus, that is the user has to click it first.
If you need to pass a key charCode to a TextField, the latter should listen to a KeyboardEvent and retrieve the info from the event's charCode property
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/
Your perspective of AS3 is "different"... For instance getChildByName doesn't convert a String to an Object, it basically does what the method name states , it retrieves a parent's child using its name as a reference.
It looks like you're adapting whatever language you're coming from to AS3. I doubt this will work...
精彩评论