as3文本字段输入一定数量的字符

时间:2013-10-30 19:11:45

标签: arrays actionscript-3 character

有一个数组和文本字段类型输入

myarray=["h","e","l","l","o"];

我只需要一个“h”,“e”,“o”和两次“l”按键输入到文本字段

这有可能吗?

3 个答案:

答案 0 :(得分:0)

这是一个不关心顺序的解决方案:

public class Main extends Sprite 
{
    var myarray:Array = ["h","e","l","l","o"];

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        var tf:TextField = new TextField();
        tf.type = TextFieldType.INPUT;
        tf.border = true;
        tf.addEventListener(TextEvent.TEXT_INPUT, onInput);
        addChild(tf);

    }

    private function onInput(e:TextEvent):void 
    {
        var index:int = myarray.indexOf(e.text);
        if (index == -1) {
            e.preventDefault();
        } else {
            myarray.splice(index, 1);
        }
    }

}

如果您希望输入与数组中的顺序完全匹配,那么您需要检查数组中的第一个字符是否与输入匹配,然后将其从数组中删除:

    private function onInput(e:TextEvent):void 
    {
        if (!myarray.length || myarray[0] != e.text) {
            e.preventDefault();
        } else {
            myarray.splice(0, 1);
        }
    }

答案 1 :(得分:0)

虽然你的问题相当神秘,但我会尽量回答它。

TextField具有内置restrict属性,限制了允许的输入。

这意味着您可以执行textField.restrict = "helo";之类的操作,并且只允许用户输入字母h,e,l和o。

这并未解决您希望用户输入“hello”一词这一事实。为此,您必须在进入时捕获用户输入并防止任何不需要的字符:

import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.TextEvent;

var restrictionArray:Array = "hello".split("");
var currentLetterIndex:int = 0;
var restrictedTextField:TextField = new TextField();
addChild(restrictedTextField);
restrictedTextField.restrict = "helo";

restrictedTextField.type = TextFieldType.INPUT
restrictedTextField.width = 100;
restrictedTextField.height = 30;
restrictedTextField.addEventListener(TextEvent.TEXT_INPUT, onKey);
function onKey(e:TextEvent):void {
    if(currentLetterIndex < restrictionArray.length && e.text == restrictionArray[currentLetterIndex])
    {
        currentLetterIndex++;
    }
    else
    {
        e.preventDefault();
    }
}

上面的代码通过restrictionArraycurrentLetterIndex跟踪下一个字母应该是什么。任何不匹配的字母都不会使用TextField方法输入preventDefault

答案 2 :(得分:0)

我试过并发现了。谢谢你的另一个答案

import flash.text.TextField;

var tf:TextField = new TextField  ;
tf.border = true;
tf.type = "input";
addChild(tf);
tf.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown);
var dizi:Array = "hello".split("");
var yazilan:Array=new Array();
function reportKeyDown(event:KeyboardEvent):void
   {
       tf.restrict = dizi.toString();
       var harfsira:int;
       harfsira = dizi.indexOf(String.fromCharCode(event.charCode));
       trace(harfsira);
       trace("character: " + String.fromCharCode(event.charCode) +         " (key code: " + event.keyCode + " character code: "         + event.charCode + ")");
           if (harfsira >-1)
               {
                  yazilan.push(String.fromCharCode(event.charCode));
                  dizi.splice((harfsira),1);
                  trace(yazilan.toString()+" "+"input character");
                  trace(dizi.toString()+" "+"restrict characters");
              }
              else
              {
                  trace("end of restrict character");
              }
    }