0

我尝试从 INPUT 类型的文本字段获取输入并将其数值保存在几个变量上,但是当我输入例如 1 或任何数字时,我在跟踪调试中得到 Nan,在我输入另一个数字后,我得到第一个我放了另一个,我得到了前两个,依此类推。我做错了什么?这是我的代码中的一些片段。

        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);
    }
4

1 回答 1

1

调度时TextEvent.TEXT_INPUT,用户输入尚未附加到text. 它很有用——您可以调用event.preventDefault()并且输入将被取消,text将保持不变。

使用Event.CHANGE事件。

于 2010-06-25T10:18:43.357 回答