views:

54

answers:

1

I have a function that calculates how many characters remaining the user can type, but I don't know why it only starts counting from the 2nd characters. Means at the end I will able to type an extra character from the maximum amount I set.

wInput.maxChars=30

wInput.addEventListener(KeyboardEvent.KEY_DOWN, calculate);
private function calculate(event:Event=null):void {
                NameRC=wInput.maxChars-wInput.length;
                remainingA.text=NameRC;

        }

wInput function:

private function input():void {
            //name input
            wInputF.font=arial.fontName;
            wInputF.color=0x818181;
            wInputF.size=15;
            wInputF.align=TextFormatAlign.JUSTIFY;
            wInputF.leftMargin=5;
            wInputF.rightMargin=10;


            wInput.defaultTextFormat=wInputF;
            wInput.border=true;
            wInput.borderColor=0xE6E6E6;
            wInput.selectable=true;
            wInput.type=TextFieldType.INPUT;
            wInput.maxChars=30;
            wInput.width=wBox.width-wType.textWidth-70;
            wInput.height=wInput.textHeight+5;
            wInput.x=wBox.x+wType.width+10;
            wInput.y=wType.y-5;
            wBox.addChild(wInput);

            //calculate remaining characters
            remaining();
            remainingC.x=wInput.x+wInput.width+20;
            remainingC.y=wInput.y+12;
            wInput.addEventListener(KeyboardEvent.KEY_DOWN, calculate);
        }
A: 

I'm not quite sure what nameRC is, and if wInput is the TextField, where do you set its .text property? Anyway, going on the only place that you set a .text property, which is:

remainingA.text=NameRC;

I'd guess that it could be doing it always 1 digit behind because you do the max - length calculation before you add anything to the TextField. So like, when you first do that calculation, there's nothing in the TextField, even though you just caught a Keyboard event. So it'll return 0; then after that you add something to the field. So the next time that Keyboard event is fired, it's one behind. My only guess is that you just need to swap those lines around.

I could be way off, though ;)

debu