Skip to main content
Participating Frequently
April 20, 2011
Answered

Input text field...

  • April 20, 2011
  • 1 reply
  • 1516 views

Dear All, I am looking for some help on the below mention text input filed function in AS3: How to assign a particular text (a word or a numeric character) to a text input field so that it perform certain action only when it has that specific text input in it? For example if I write “1 or One” inside text input field and press Enter key it should display picture 1 and if I write “2 or Two” and press Enter key it should display picture 2.

Regards

This topic has been closed for replies.
Correct answer Ned Murphy

There is one software named Maxedia. We can play .swf files in it for live text change, etc. But it has its own limitations. As for now we can not use mouse event in it. I am providing you a working sample file. It is a very simple project. When we connect it with Maxedia web server it provide us one text input field option. The input text is than display in Maxedia output window. The code is divided in two parts - Outer and Inner script. Outer script is for the main Scene and inner script is for text movie clips.

OUTER SCRIPT CODE:


import flash.external.ExternalInterface;
ExternalInterface.addCallback("GetVars",GetVars);
ExternalInterface.addCallback("SetVariable1",SetVariable1);


function GetVars():Array
{
                return [
                     {VariableName:"SetVariable1",DefaultValue:"Variable1"}
             
                ]
}

function SetVariable1(variable:String):void
{
               for(var i:int = 0; i < numChildren; i++)
    {
                 var mc:MovieClip = getChildAt(i) as MovieClip;
                 if(mc != null && mc.name == 'TitleAnimation')
                 {
      mc.variable1 = variable;
      mc.SetVariable1(variable);    
     }
    }

}

INNER SCRIPT CODE:

function SetVariable1(variable:String):void
{
if(variable != null)
{
  for(var i:int = 0; i < numChildren; i++)
    {
                 var mc:MovieClip = getChildAt(i) as MovieClip;
                 if(mc != null && mc.name == 'Text1')
                 {
       var object:TextField = mc.getChildByName('InputTextField1') as TextField;
       if(object != null)
       {
      
       object.text = variable;
       }

     }
    }
}

}

SetVariable1(this.variable1);

Now what I need to do is, instead of getting Text as a output I want it to perform some action. For e.g. if I type numeric character "1" in the input text field and press the Enter key it should display Picture 1 and if I type 2 it should display Picture.

Please let me know if any further information is required at my end. This is a very important project for me and I truly appriciate the way your helped and helping me. Many thanks.


The code you just showed does nothing to explain what you are trying to do with the code you started off with.  The errors/problems are likely due to a couple of reasons.  Part of the proplem was likely due to not assigning a value to the picVisible variable at the beginning.  The other part of the problem was likely due to setting the picVisible value to the object you wanted to fade in before you did the fade out, which was fading out the object that was not yet visible.

I have rewritten the code so that it functions properly for what I am hoping your intentions are.  I made it so that if Pic1 is showing, entering a 1 will do nothing, same for Pic2.

import flash.events.Event;
import flash.events.KeyboardEvent;
import fl.transitions.*;
import fl.transitions.easing.*;
import fl.motion.*;
import fl.transitions.TweenEvent;

InputText.addEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);

var Pic1:Picture1 = new Picture1();
var Pic2:Picture2 = new Picture2();

var picToShow:* = Pic1;

Pic1.x = Pic2.x = 800;
Pic1.y = Pic2.y = 500;

Pic1.alpha = 1;
Pic2.alpha = 0;
addChild(Pic1);
addChild(Pic2);

function inputFieldHandler(event:KeyboardEvent) {
    if (event.keyCode == Keyboard.ENTER) {
       var iText:String = InputText.text;
       var picToHide:* = picToShow;
       var okayToShow:Boolean = false;
   
       if ((iText == "1" || iText.toLowerCase() == "one") && picToHide != Pic1) {
            picToShow = Pic1;
            okayToShow = true;
       } else if ((iText == "2" || iText.toLowerCase() == "two") && picToHide != Pic2 ) {
            picToShow = Pic2;
            okayToShow = true;
       }

       if(okayToShow){
           InputText.removeEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);
           var PicOut:Tween = new Tween(picToHide,"alpha",Strong.easeOut,picToHide.alpha,0,1,true);

           PicOut.addEventListener(TweenEvent.MOTION_FINISH, PICON);
       }
    }

}

function PICON(event:TweenEvent):void {
    var PicIn:Tween = new Tween(picToShow,"alpha",Strong.easeOut,picToShow.alpha,1,1,true);
    PicIn.addEventListener(TweenEvent.MOTION_FINISH, resetInputField);
}

 
function resetInputField(event:TweenEvent):void {
    InputText.addEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);
}

1 reply

Ned Murphy
Legend
April 20, 2011

Here is some basic code, where tField is the textfield and picture1/2 are movieclips of the pictures...

tField.addEventListener(KeyboardEvent.KEY_UP, checkText);

function checkText(event:KeyboardEvent):void {
    if(event.keyCode == Keyboard.ENTER){
        var txt:String = tField.text;
        if(txt == "1" || txt == "One")
            picture1.visible = true;
        } else if((txt == "2" || txt == "Two"){
            picture2.visible = true;
        }
    }
}

HS_OsanAuthor
Participating Frequently
April 20, 2011

Dear Mr. Murphy,

This is the error “TypeError: Error # 1009: Can not access a property or method of a null object reference is not possible” I am getting from the below mention code:

InputText.addEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);

var Pic1:Picture1 = new Picture1();

var Pic2:Picture2 = new Picture2();

Pic1.x, Pic2.x, Pic1.y, Pic2.y = 0;

Pic1.alpha=1;
Pic2.alpha=0;
addChild(Pic1);
addChild(Pic2);

var PicOut:Tween;
var PicIn:Tween;

var picVisible:Object = Pic1;

function inputFieldHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.ENTER){
     var iText:String = InputText.text;
          if ((iText == "1" || iText == "one" && picVisible != Pic1) || (iText == "2" || iText == "two" && picVisible != Pic2))
         InputText.removeEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);
         PicOut = new Tween(picVisible,"alpha",Strong.easeOut,picVisible.alpha,0,1,true);
         PicOut.addEventListener(TweenEvent.MOTION_FINISH, PICON);
         trace("Enter key pressed");
      }
}

function PICON(event:TweenEvent):void{
picVisible = this["PIC"];
picVisible.x = 0;
picVisible.y = 0;
PicIn = new Tween(picVisible,"alpha",Strong.easeOut,picVisible.alpha,1,1,true);
PicIn.addEventListener(TweenEvent.MOTION_FINISH, resetInputField);
}

function resetInputField(event:TweenEvent):void {
InputText.addEventListener(KeyboardEvent.KEY_DOWN, inputFieldHandler);
}

Thanks and kind regards

Ned Murphy
Legend
April 20, 2011

From what I see of your code there are probably other errors waiting to be listed, but for now...

The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....
 
- is not in the display list
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
 
If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.