-
[RESOLVED] Moving focus away from a TextField
Hi all,
i have made a flash movie which is working perfectly.
I used the tutorial by GotoAndLearn which you can find here...
http://www.gotoandlearn.com/play?id=85
which shows how to make a preloader as a separate swf file and he states that you won't need to worry about the swf file that is loaded by the preloader.
He was correct for everything in my movie except for the
Code:
stage.focus = null;
code which i use to move the focus away from a textfield once a person has entered text and hit the Enter key.
Basically, i just don't want the typing cursor to hang around after the user hits enter.
Can anyone tell me why the
Code:
stage.focus = null;
and
Code:
element.setFocus();
pieces of code no longer work when my movie is loaded as an external swf in a preloader? They both worked perfectly before. Tried loads of workarounds but nothing seems to help.
Any help would be appreciated.
Cheers
LL
-
rabid_Delineator
its probably a scope thing , meaning when you define stage in your child swf , it isnt being localized to the new , global stage , when loaded. So you can do a couple of different things. One would be creating a global stage singleton , or public static class, that you can import into your child , and make calls to set your focus , this will maintian stage being the root of your movie no matter how it is parented. Or you can try dispathcing and event, so in your event handler for the return key,
dont directly call element.setFocus or set the stage.focus to null. comment those lines out. instead say
Code:
dispatchEvent( new Event( "manageFocus" , true , false ) );
then in your loading , parent class , add a listener :
Code:
addEventListener( "manageFocus" , manageFocus );
declare your function
Code:
private function manageFocus( evt : Event ) : void {
//set your focus to whatever you want here
//stage.focus = null;
};
-
I don't know what element is, or HOW those lines are failing (are you getting error messages, unexpected behavior, what?), but the most common cause of errors when changing context from document class to loaded class is that you are expecting the stage property to be set from the beginning instead of waiting for the ADDED_TO_STAGE event.
The stage property is null until the displayObject is on the stage. You can use the ADDED_TO_STAGE event to tell you when it is safe to refer to the stage.
Before (bad code)
Code:
public function MyClass(){
stage.focus = null;
//more stuff.
}
After (better code)
Code:
public function MyClass(){
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.focus = null;
//more stuff.
}
-
rabid_Delineator
hes saying , that those lines of code are failing , upon the return key being pressed by the user. stage would be available , as the child would have to be added to the display list before the user would be able to type anything and hit enter.
-
Thanks guys,
you're right Rabbit, i was getting a null object error, probably because the stage was no longer related to the swf which was loaded but instead related to the preloader swf.
I used your suggestion and now it works well.
Another problem popped up though. I implemented some code i downloaded for a swish looking ToolTip since i typed that first forum post here, and now the stage reference is null in the ToolTip and the ToolTips don't work. But i should be able to pass a reference to the stage from the preloader swf using a Global Var class i have.
Thanks for all of your help. I couldn't for the life of me figure out why the stage.focus = null code had stopped working and thought it must have been the focus part, but i got that very wrong
Cheers
Last edited by lordlupin; 11-06-2009 at 03:49 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|