views:

17

answers:

1

I try to get input from a textfield of type INPUT and save its numerical value on a couple of variables, but when i enter for example 1 or any digit i get Nan in the trace debug, after i put another digit i get the first after i put another one i get the first two and so on so forth. What i am doing wrong? Here some snippets from my code.

        xSpeedField.addEventListener(TextEvent.TEXT_INPUT, inputXCapture);

        private function initField(field:TextField, label:String, x:uint, y:uint):void {
        var format:TextFormat = new TextFormat();
        format.color = 0x00FF00;
        var textLabel:TextField = new TextField();
        textLabel.text = label+":";
        textLabel.defaultTextFormat = format;
        panel.addChild(textLabel);          
        field.type = TextFieldType.INPUT;
        field.restrict = "0-9.\\-";
        field.text = "0";
        field.border = true;
        field.background = true;
        field.backgroundColor = 0x333333;
        field.width = FIELD_WIDTH;
        field.height = FIELD_HEIGHT;
        panel.addChild(field);
        field.x = x;
        field.y = y;

        textLabel.x = field.x - 20;
        textLabel.y = field.y;
    }

    private function onEnterFrameHandler(event:Event):void {                                                            
        if ( ball.x + xspeed > stage.stageWidth) {
            xspeed *= -1;            
        }
        else if ( ball.x + xspeed < 0 ) {
            xspeed *= -1;                
        }

        if ( ball.y + yspeed > stage.stageHeight - FIELD_HEIGHT - BALL_RADIUS) {
            yspeed *= -1;            
        }           
        else if ( ball.y + yspeed < 0 ) {           
            yspeed *= -1;
        }           
        ball.x += xspeed;
        ball.y += yspeed;
    }               

    private function inputXCapture(event:TextEvent):void {
        xspeed = Number(event.currentTarget.text);
        trace(event.currentTarget.text);
    }
+1  A: 

When TextEvent.TEXT_INPUT is dispatched, user input is not yet appended to text. It is useful - you can call event.preventDefault() and input will be canceled, text will remain the same.

Use Event.CHANGE event.

Maxim Kachurovskiy