forked from: TextField NGワード
♥0 |
Line 100 |
Modified 2012-02-18 08:50:47 |
MIT License
archived:2017-03-30 02:59:08
ActionScript3 source code
/**
* Copyright tepe ( http://wonderfl.net/user/tepe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/utDL
*/
// forked from AsaToBan's TextField NGワード 未完成
/*
* TextFiledの色々テストその2
* 1.NGワード
* 2.複雑じゃないパターン
*/
package
{
import flash.text.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.Sprite;
import flash.text.TextFieldAutoSize;
public class FlashTest extends Sprite
{
private var txtA:TextField;
private var txtB:TextField;
private var NGword1:NGword = new NGword();
public function FlashTest()
{
// write as3 code here..
init();
}
private function init():void
{
txtB = new TextField();
txtB.height = 200;
txtB.width = 456;
addChild(txtB);
var txt:TextField = new TextField();
//txtB.autoSize = TextFieldAutoSize.LEFT;
txt.width = 400;
txt.height = 200;
txt.y=250;
txt.x=130;
addChild(txt);
txt.appendText("\n・枠内に文字列を入力してEnterで登録\n・登録した文字列を表示する\n・入力済みの文字列を登録しようとした場合はNGとする");
txt.appendText("\n・resetと入力すると登録した文字列を消去する")
txtA = new TextField();
txtA.border = true;
txtA.borderColor = 0xCCCCCC;
txtA.width = 200;
txtA.height = 50;
txtA.x = stage.stageWidth/2 - txtA.width/2;
txtA.y = stage.stageHeight/2 - txtA.height/2;
txtA.type = TextFieldType.INPUT;
txtA.background = true;
txtA.name = "txtA";
//txtA.selectable = false;
addChild(txtA);
//defalt textformat
var tf:TextFormat = new TextFormat();
tf.align = TextFormatAlign.CENTER;
tf.size = 36;
txtA.defaultTextFormat = tf;
tf.size = 18
txtB.defaultTextFormat = tf;
addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
txtA.addEventListener(Event.CHANGE,onChange);
}
private function onChange(e:Event):void{
txtA.backgroundColor = 0xffffff;
}
private function keyDownHandler(e:KeyboardEvent):void
{
var result:Boolean = NGword1.check(txtA.text);
if(e.keyCode == Keyboard.ENTER && txtA.length>0)
{
if(txtA.text == "reset"){
NGword1.clear();
txtA.text="";
txtB.text="";
}
if(!result){
//txtA.text ="NG!";
txtA.backgroundColor = 0xffcccc;
}
else{
txtA.backgroundColor = 0xffffff;
txtB.appendText("\n"+txtA.text);
txtB.scrollV = txtB.maxScrollV;
NGword1.addWord(txtA.text);
txtA.text = "";
}
}
}
}
}
//対応文字列登録
class NGword
{
private var aNGAry:Array;
public function NGword():void{
aNGAry = new Array();
}
public function check(_str:String):Boolean
{
var bCheckNg:Boolean = true;
for(var i:int =0; i<aNGAry.length; i++)
{
if(aNGAry[i] == _str) bCheckNg = false;
}
return bCheckNg;
}
public function addWord(str:String):void{
aNGAry.push(str);
}
public function clear():void{
aNGAry = null;
aNGAry = new Array();
}
}