A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Focus events

  1. #1
    Member
    Join Date
    Apr 2007
    Location
    Manhattan, New York, USA
    Posts
    58

    Focus events

    I'm trying to put the text back into a text field on FOCUS_OUT one character at a time. On FOCUS_IN, I want to remove the text one character at a time.

    Here's the basic code:


    major.text = "ActionScript 3.0";

    major.addEventListener(FocusEvent.FOCUS_IN, inFocus);
    major.addEventListener(FocusEvent.FOCUS_OUT, outFocus);


    function inFocus(event:FocusEvent):void
    {
    major.text="";// want to clear the text one character at a time
    }


    function outFocus(event:FocusEvent):void
    {
    major.text="ActionScript 3.0";// want to put the text back in one character at a time
    }


    I attached a fla. that does this in ActionScript 2.0, but I'm trying to do this in ActionScript 3.0 ... Thanks for any

    replies...
    Attached Files Attached Files

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Create a timer to do the individual letter manipulation.

    Code:
    var t:Timer = new Timer(500); //half-second delay.
    var fullMessage:String = "ActionScript 3.0";
    major.text = fullMessage;
    
    major.addEventListener(FocusEvent.FOCUS_IN, inFocus);
    major.addEventListener(FocusEvent.FOCUS_OUT, outFocus);
    
    
    function inFocus(event:FocusEvent):void{
      t.addEventListener(TimerEvent.TIMER, removeALetter);
      t.start();
    }
    
    function removeALetter(e:TimerEvent):void{
      if (major.text.length == 0){
        t.removeEventListener(TimerEvent.TIMER, removeALetter);
        t.stop();
      }else{
        major.text = major.text.substring(0, major.text.length-1);
      }
    }
    
    function outFocus(event:FocusEvent):void{
      t.addEventListener(TimerEvent.TIMER, addALetter);
      t.start();
    }
    
    function addALetter(event:TimerEvent):void{
      if (major.text == fullMessage){
        t.removeEventListener(TimerEvent.TIMER, addALetter);
        t.stop();
      }else{
        major.text += fullMessage.charAt(major.text.length);
      }
    }

  3. #3
    Member
    Join Date
    Apr 2007
    Location
    Manhattan, New York, USA
    Posts
    58
    Thanks a million for the reply. I admire the clean code. But, there's a slight problem:

    the problem now is that if someone enters text into this input text field and when the input text field focus_out, the text they entered stays there (as it suppose to) but the original text also comes back in when it shouldn't. So I know we need an if statement or and else if statement in the addALetter function believe, but I'm not sure how to write that...

    I tried this to no avail:


    function addALetter(event:TimerEvent):void
    {
    if (major.text == fullMessage)
    {
    t.removeEventListener(TimerEvent.TIMER, addALetter);
    t.stop();
    }
    else if (major.text != fullMessage && major.text !="")
    {
    t.removeEventListener(TimerEvent.TIMER, addALetter);
    t.stop();
    }else {
    major.text += fullMessage.charAt(major.text.length);
    }
    }


    Basically what I need is when the user inputs text to the text field, the addALetter function shouldn't run. If you look at the fla attached in the initial post, you'll see that once text is entered into the text field, when the text field has no more focus, the original text is not re-entered, because the user has input some text...

    Thanks any further assistance... and I wish i can buy you a beer man... go Yankees!!!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The cleanest way to prevent the unwanted behavior is just to check in outFocus whether major.text is empty. If it is not empty, just don't do anything.

    By the way, please use [ code ] or [ php ] tags to format code.

    Code:
    function outFocus(event:FocusEvent):void{
      if (major.text == ""){
        t.addEventListener(TimerEvent.TIMER, addALetter);
        t.start();
      }
    }
    I can't look at the fla, because I don't have access to flash at the moment. If you post a swf somewhere, I could look at that.

  5. #5
    Member
    Join Date
    Apr 2007
    Location
    Manhattan, New York, USA
    Posts
    58
    POWERFUL stuff there man!!! I understand the code, which makes me very glee. Thanks ; I'm always a student. Thank you!!!


    Here was the .swf for this in actionscript 2.0 :

    http://gamesportsmag.com/as_2_version.swf

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The swf there has a bug, which we have avoided. Of course, we have a different bug. But I like ours better.

    The as2 bug is that you cannot enter a value which is a prefix in the default value. Try entering "N" for your Name or "Tel" for your telephone. It will not respect it. This is probably because they are testing whether the content is found in the default value at position 0, rather than just testing whether the content is empty.

    Our bug is that you can start typing while it is removing letters. If you do so, it will continue to delete letters, even those you typed.

    I don't think that you can type while it is adding letters because that only happens on focusOut.

    But we should probably put in some flags so we know whether it's in the process of adding or deleting letters so that we can remove event listeners appropriately. I think if you focus in and out before it has finished the process, you'll end up in a stalemate where the same letter is being added and removed.

    Code:
    var t:Timer = new Timer(500); //half-second delay.
    var fullMessage:String = "ActionScript 3.0";
    var adding:Boolean = false;
    var removing:Boolean = false;
    
    major.text = fullMessage;
    
    major.addEventListener(FocusEvent.FOCUS_IN, inFocus);
    major.addEventListener(FocusEvent.FOCUS_OUT, outFocus);
    
    
    function inFocus(event:FocusEvent):void{
      removing = true;
      if (adding){
         t.removeEventListener(TimerEvent.TIMER, addALetter); 
         adding = false;
      }
      t.addEventListener(TimerEvent.TIMER, removeALetter);
      t.start();
    }
    
    function removeALetter(e:TimerEvent):void{
      if (major.text.length == 0){
        t.removeEventListener(TimerEvent.TIMER, removeALetter);
        t.stop();
        removing = false;
      }else{
        major.text = major.text.substring(0, major.text.length-1);
      }
    }
    
    function outFocus(event:FocusEvent):void{
      if (major.text == ""){
        adding = true;
        if (removing){
          t.removeEventListener(TimerEvent.TIMER, removeALetter);
          removing = false;
        }
        t.addEventListener(TimerEvent.TIMER, addALetter);
        t.start();
      }
    }
    
    function addALetter(event:TimerEvent):void{
      if (major.text == fullMessage){
        t.removeEventListener(TimerEvent.TIMER, addALetter);
        t.stop();
        adding = false;
      }else{
        major.text += fullMessage.charAt(major.text.length);
      }
    }

  7. #7
    Member
    Join Date
    Apr 2007
    Location
    Manhattan, New York, USA
    Posts
    58
    Wow! Very impressive, clean code. I appreciate this a lot. You got me rocking 5TonsOfFlax !!!

    THANK YOU!!!

    One more thing I forgot to mention, but I may can do it myself. I'll try, because that's how you learn, trial and error, lose a lot of sleep etc.

    But, if I want four different input fields (name,company,tel no,address), I would have to of course create the vars and text as I did initially. But, can I use event.target in the functions so I wouldn't have to create all the functions over for each input text field? Thus, would it be possible to use the same functions for different input text fields as in the example I referred you to? For example:

    [code]

    function removeALetter(e:TimerEvent):void{
    if (event.target.length == 0){
    t.removeEventListener(TimerEvent.TIMER, removeALetter);
    t.stop();
    removing = false;
    }else{
    event.target = event.target.substring(0, event.target.length-1);
    }
    }

    [code/]


    Or, that would be too complicated? If so, I'll just create separate functions for each text field. But, in any event, I'll read my ActionScript books all night and try to make these great functions modular as possible. I wrote some complicated code before when I couldn't get no help anywhere, and I was definitely excited...but back to tackling code....gotta learn ...

    Thanks again; I'm off to coding.
    Last edited by xFlashStudentx; 10-26-2009 at 03:41 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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center