0

I'm loading a swf form into my swf movie (to save on file size). I have a 'faux-form' of static MCs in the main swf, and when a user clicks one, it loads the 'real form' which is its own swf file. What I want to do is properly set the focus of the real form's input field, based on the MC of the faux form the user clicks.

For example: User watches animation and sees form on last frame (faux form). User clicks on the 'city' input field to enter their city. Externally loaded swf file with 'real form' is immediately loaded into the main swf on top of the faux form, so the user has active input fields to interact with. Text input field 'city' should now be in focus.

Thanks in advance for any help!

jmarx34
  • 228
  • 2
  • 7
  • 25

1 Answers1

1

If you have a reference to the text input you want to set focus to you can use the following:

var cityInputField:TextField = mc.getChildByName("cityInputField") as TextField;
if(cityInputField)
   stage.focus = cityInputField;
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • Unfortunately this didn't do the trick. I can assume 'mc' (in the mc.getChildByName) is referencing the MC object I am assigning the external swf to? i am loading the swf (successfully) as follows: function swfLoadedHandler(event:Event):void { loaded_swf = event.target.content; loaded_swf.x = 200; loaded_swf.y = 42; addChild(loaded_swf); } – jmarx34 May 07 '12 at 17:47
  • Did you step through the code in the debugger and see if it executed the focus line? – Barış Uşaklı May 07 '12 at 17:49
  • yea, i added a trace statement inside the 'if', but it did not get executed. so it clearly isn't seeing the text input field in the external swf, i just can't seem to figure out how to access it. – jmarx34 May 07 '12 at 17:51
  • mc is the movieclip you load form the external swf, so you might have to cast the content to a movie clip and then make sure to get the correct child. – Barış Uşaklı May 07 '12 at 17:54
  • sorry, i'm still a little confused. could you share code as to how you envision this? (currently my input fields in the external mc are just loaded onto the stage of the external swf, and they are the only MCs in the file, with instance names: city_field1 and zip_field1) – jmarx34 May 07 '12 at 18:05
  • Ok so if you are loading your swf with a loader class, and the swf has a MC named city_field1 you can retrive that MC with : var cityMC:MovieClip = loader.getChildByName("city_field1") as MovieClip; After that if the textfield is a child of the movie clip then you need to get that child from the cityMC with the code in my answer, using the correct name of course. You should be able to see the members of these in the debugger to figure out their names. – Barış Uşaklı May 07 '12 at 18:10
  • Baris, I really appreciate your help...still struggling though. I guess we cannot format code in the comments section, but hopefully you can still see this easily enough and see where I'm making my mistake: import flash.display.MovieClip; import flash.net.URLRequest; import flash.display.Loader; import flash.events.Event; import flash.text.TextField; var loaded_swf: MovieClip; var form_url: URLRequest = new URLRequest('filename.swf'); var ldr: Loader = new Loader(); var cityInputField:TextField; var cityMC:MovieClip; city_field.addEventListener(MouseEvent.CLICK, loadSwf); – jmarx34 May 07 '12 at 18:19
  • function swfLoadedHandler(event:Event):void { loaded_swf = event.target.content; loaded_swf.x = 200; loaded_swf.y = 42; cityMC = loaded_swf.getChildByName("city_field1") as MovieClip; cityInputField = cityMC.getChildByName('city_field1') as TextField; trace(cityMC); if(cityInputField) { trace('hello'); stage.focus = cityInputField; } addChild(loaded_swf); } function loadSwf(MouseEvent:Event):void { ldr.load(form_url); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.swfLoadedHandler); } – jmarx34 May 07 '12 at 18:19
  • cityMC = loaded_swf.getChildByName("city_field1") as MovieClip; cityInputField = cityMC.getChildByName('city_field1') as TextField; is the name of the movie clip and text field the same? – Barış Uşaklı May 07 '12 at 18:25
  • so i had just used drag and drop to add the input fields onto the stage. in doing this, how do i name the text input vs converting it to a mc and giving the mc an instance name (which is what i had done)? i guess this is probably the issue. do you recommend me creating the input fields dynamically through AS instead? – jmarx34 May 07 '12 at 18:39
  • You should be able to name them in flash by giving them an instance name, that name becomes the name you use in getChildByName, I don't work with flash professional so I create my inputs dynamically in AS3 or through mxml if I'm using flex. – Barış Uşaklı May 07 '12 at 18:56
  • i've rebuilt the form using AS to create the form fields, and still having the same issues more or less. what i'm wondering is the MC that we are referencing in the "mc.getChildByName..." based off of my previous code, should this be 'loaded_swf' (which is the MC created as the holder of the external swf in the main swf file), or something else? – jmarx34 May 07 '12 at 19:24
  • loaded_swf is usualy the Loader class that was used to load that external swf, you can add the loader class to the stage, and then call getChildByName on the loader class to get the movie clips inside. – Barış Uşaklı May 07 '12 at 19:33
  • right, but if this were true, then from your first response var cityInputField:TextField = loaded_swf.getChildByName('city_field1') as TextField; stage.focus = cityInputField; would have worked fine – jmarx34 May 07 '12 at 20:29
  • 1
    So it turns out that this is a perfect example of KISS (and I promise I'm the 2nd 'S' here). Instead of overcomplicating things, I went back and reverted code to: stage.focus = loaded_swf.city_field1; and that actually worked! (I did change change the external swf file from manually dragged text inputs to AS created ones, not sure if that made a difference...) – jmarx34 May 07 '12 at 20:36