2012-09-19 54 views
1

我希望用户在文本字段(INPUTFIELD)中填入1-350之间的数字。 通过XML我想在另一个文本字段(OUTPUTFIELD)中返回一个字符串。as3 if else,min max value

我得到了99%的工作,但在用户填入0或高于350的数字后,我想在OUTPUTFIELD中返回一条消息。

我没有得到这个工作,我得到的消息:

TypeError: Error #2007: Parameter text must be non-null. 
    at flash.text::TextField/set text() 
    at MethodInfo-2() 

我的代码如下:

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); 
function keyDownHandler(event : KeyboardEvent):void { 
    if (event.keyCode == Keyboard.ENTER) { 
     if (inputField.text > "0") { 
       outputField.text = xmlData.prefix_list.country[Number(inputField.text)]; 
       outputField.setTextFormat(myTextFormat); 
     } else { 
       if (inputField.text > "350") { 
        outputField.text = "DOES NOT EXIST"; 
        outputField.setTextFormat(myTextFormat); 
       } 
     } 
    } 
} 

我试了无数选项中的“如果”有条件的,例如只有几个像:

if (inputField.text > "0" || inputField.text < "350") 
if (inputField.text >= 0 && inputField.text =< 350) 
if (inputField.text >= "0" && inputField.text =< "350") 

同样适用于“其他”条件。

} else { 
    if (inputField.text > "350") { 

也许有人可以指出我正确的方向。

+0

在代码的**行**错误发生? – 2012-09-19 21:00:49

+0

@navnav直到现在才看到您的回复!现在已经修好了,但是仍然要感谢您的帮助。 – James070

回答

0
if (int(inputField.text) > 0 && int(inputField.text) < 350) { 
    outputField.text = xmlData.prefix_list.country[Number(inputField.text)]; 
    outputField.setTextFormat(myTextFormat); 
} else { 
    outputField.text = "DOES NOT EXIST"; 
    outputField.setTextFormat(myTextFormat); 
} 

如果使用整数,则必须将值转换为int。

在AS3中很容易转换类型。这里是教程如何将字符串转换为数字http://www.trainingtutorials101.com/2011/01/actionscript-3-convert-strings-to.html

+1

与铸造问题无关的一个小提示:您可以在创建文本字段时设置'outputField.defaultTextFormat = myTextFormat;'而不是多次调用'setTextFormat' –

+0

嗯,我只是复制了他的代码,但是感谢您注意; ) – mswiszcz

+0

哇!那是很快的家伙!非常感谢,它像一个魅力! RESPECT ....... – James070