Browse the notebook

Category: TextField

Fill in the Blank


var story:String = "Fill in the _____.";


var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat("Georgia", 20);
txt.width = stage.stageWidth;
txt.multiline = true;
txt.wordWrap = true;
txt.text = story;
addChild(txt);

var alph:Array = "abcdefghijklmnopqrstuvwxyz".split("");
var keys:Object = {};
for (var i:int = 0; i<alph.length; i++){
	keys[65 + i] = alph[i];
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
function onKeyPressed(evt:KeyboardEvent):void{
	 
	if (evt.keyCode == Keyboard.ENTER){
		story = "Fill in the _____.";
		txt.text = story;
	}
	
	for (var i:int = 0; i<story.length; i++){
		if (story.charAt(i) == "_"){
			var head:String = story.substr(0, i);
			var tail:String = story.substr(i + 1);
			var letter:String = keys[evt.keyCode];
			if (!letter) return;
			story = head + letter + tail;
			
			txt.text = story;
			
			break;
		}
	}
}

I needed to do a fill in the blank for a personal project that I’m working on and this is what I came up with. Have a look at the swf here:

(you need to click first so you can type with the keyboard):
Fill in the blank

Read snippet: Fill in the Blank