flash on 2009-12-12
/**
* Copyright jopiruizen ( http://wonderfl.net/user/jopiruizen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gwoZ
*/
package{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class FlashTest extends Sprite{
var w=470,h=470,chars,fhit=true,temp,targets;
var timeout=10,wait=3000,tCtr=0;
var area:Sprite,input:Sprite,fld:TextField;
public function FlashTest() { loaderInfo.addEventListener("complete",init);}
function draw(t,c,a,o) {
t.graphics.beginFill(c,a);
if(o.s=="c"){t.graphics.drawEllipse(0-(o.w/2),0 -(o.w/2),o.w,o.w);}
else if(o.s=="r"){t.graphics.drawRect(0,0,o.w,o.h);}
t.graphics.endFill();
}
function init(e:*){
chars = "";targets=new Array();
mouseChildren = false;
draw(this,0x000000,1,{w:w,h:h,s:"r"});
area=new Sprite(); draw(area,0,0,{w:w,h:h,s:"r"});
addChild(area);
input=new Sprite();
draw(input,0xFF9900,1,{w:w-40,h:50,s:"r"});
input.x=(w-(w-40))/2;
input.y=h-50-10;
addChild(input);
fld=new TextField();input.addChild(fld);fld.border=true;fld.borderColor=0xFFFFFF;fld.width=w-40;fld.height=50;
fld.selectable=false;fld.maxChars=12;
var tf:TextFormat = new TextFormat("Comic Sans MS",30,0x000000,true);
tf.align = "center";
fld.defaultTextFormat = tf;
fld.text="Type in your key";
addEventListener("keyDown",keyh);
addEventListener("enterFrame",enfra);
addEventListener("click",clck);
stage.focus=this;
create();
}
function clck(e:*){stage.focus=this;};
function keyh(e:*){
if( fhit == true ){fhit=false; fld.text="";}
var ccode = e.charCode;
if( ccode == 8) {//backspace
temp=chars.split("");
temp.splice(temp.length-1,1);chars="";
for(var i=0;i<temp.length;i++)chars+=temp[i];
} else if(ccode==13||ccode==32){ //enter
chars="";
} else if((ccode>=65&&ccode<=90)||(ccode>=97&&ccode<=122)) {
chars+=String.fromCharCode(ccode);
}
fld.text=chars;
}
function mod(dvn:*,dvsr:*){return((dvn%dvsr)== 0);}
function ri(i) {return Math.floor( Math.random() * i);}
function crfld(w,h,s,c){
var f=new TextField();
f.width=w;f.height=h;
var tf=new TextFormat("Comic Sans MS",s,c,true);
tf.align="center";
return f;
}
function create(){
var o={};
o.main=new Sprite();
draw(o.main,0xFFFFFF,1,{w:100,h:15,s:"r"});
o.main.name ="target_" + ri(1000000) + "_" + targets.length;
o.main.x=ri(470-100);
o.main.y=-15;
targets.push(o);
area.addChild(o.main);
wait=(ri(6) + 1)*200;
trace("wait: "+ wait);
return o;
}
function act(){
for( var i=0;i<targets.length;i++){
var o=targets[i];
o.main.y++;
}
}
function enfra(e:*){
stage.focus=this;
if( mod(tCtr,wait) ){
create();
}
act();
tCtr+=timeout;
if(tCtr>50000)tCtr=0;
}
}
}