Skip navigation
Currently Being Moderated

Textfield and eventlisteners

Aug 2, 2012 4:57 AM

Hi guys, i fee im plauging this forum sometimes, this code lets a user input text and click a button which outputs it to a textfieldand also adds a number to another textfield. Another button removes this text and also subtracts the same number. It all works fine when there is text in the input field but when it is empty and you click the button that adds the text it still adds to the number.

 

[AS]

 

//-------------------------------------------------------------------- ---

var textAdd01 : Number;

          textAdd01 = 3;

 

 

//-------------------------------------------------------------------- ---

LBtextMenu.LBtxtSubmit.addEventListener(MouseEvent.MOUSE_UP, LBtypedChar);

function LBtypedChar(evt:MouseEvent):void

{

LBTxt.LBOutput.text = LBtextMenu.LBInput.text;

LBtextHotspot.gotoAndStop(2);

trace (LBTxt.LBOutput.text.length)

 

 

}

///----------------------Add Price---------------------------------------

 

 

LBtextMenu.LBtxtSubmit.addEventListener(MouseEvent.MOUSE_UP, LBtxtAddPrice);

function LBtxtAddPrice(evt:MouseEvent):void

{

 

priceOutput.text = String(textAdd01 + Number(priceOutput.text));

 

 

if (LBTxt.LBOutput.text.length >0){

 

          LBtextMenu.LBtxtSubmit.removeEventListener(MouseEvent.MOUSE_UP, LBtxtAddPrice);

          LBtextMenu.LBtxtRemove.addEventListener(MouseEvent.MOUSE_UP, LBtxtRemovePrice);

}

 

 

}

///------------------------------------------------------------------- ---

LBtextMenu.LBtxtRemove.addEventListener(MouseEvent.MOUSE_UP,LBtxtRemov eTxt);

 

 

function LBtxtRemoveTxt(e:MouseEvent)

{

 

  LBTxt.LBOutput.text = "";

          LBtextHotspot.gotoAndStop(1);

          LBtextMenu.LBtxtSubmit.addEventListener(MouseEvent.MOUSE_UP, LBtxtAddPrice);

          trace (LBTxt.LBOutput.text.length)

}

///----------------------Remove Price---------------------------------------

 

 

LBtextMenu.LBtxtRemove.addEventListener(MouseEvent.MOUSE_UP, LBtxtRemovePrice);

function LBtxtRemovePrice(evt:MouseEvent):void

{

priceOutput.text = String(Number(priceOutput.text) - textAdd01);

 

 

if (LBTxt.LBOutput.text.length ==0){

 

          LBtextMenu.LBtxtRemove.removeEventListener(MouseEvent.MOUSE_UP, LBtxtRemovePrice);

 

}

}

//-------------------------------------------------------------------- -

[/AS]

 

Thanks for any help in advance.

 
Replies
  • Currently Being Moderated
    Aug 2, 2012 5:07 AM   in reply to TwistedPixel23

    It is difficult to tell what's what, but for the code you show I can see where you do check the length value of some text.  That is what you should be doing to control whether or not actions are taken. 

     

    For instance...  if the following function is involved with your problem, then you should test the length of the text to decide if you want to process it or not....

     

    function LBtypedChar(evt:MouseEvent):void

    {

      if(LBtextMenu.LBInput.length > 0){

          LBTxt.LBOutput.text = LBtextMenu.LBInput.text;

          LBtextHotspot.gotoAndStop(2);

          trace (LBTxt.LBOutput.length);

       }

    }

     

    Note that in that code the way the length is checked is a tad shorter.  The textfield itself has a length property that reflects the same value as the length property of the String inside it.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 6:26 AM   in reply to TwistedPixel23

    Did you not follow what I suggest you do to control when code executes or not?  I didn't mention anything regarding traces.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 6:52 AM   in reply to TwistedPixel23

    An event listener will still work as long as it is active, it is what the event handlers function does that means anything.

     

    You need to show what you did.  I was just providing an example of what you should do.  You probably need to find the correct place(s) to inject that kind of conditional processing.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 7:41 AM   in reply to TwistedPixel23

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points