import flash.text.TextField;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
//书上的例子,紧紧是写下来练习练习而已
var num:uint=30;
var score:uint=0;
var miss:uint=0;
var SW:uint=stage.stageWidth;
var SH:uint=stage.stageHeight;
var tf:TextField=new TextField();
tf.x=tf.y=25;
tf.autoSize="left";
tf.width=200;
stage.addChild(tf);
const YOU_WIN:String="you win";
const YOU_LOSE:String="you lose";
stage.addEventListener(MouseEvent.CLICK,kill);
stage.addEventListener(YOU_WIN,win);
stage.addEventListener(YOU_LOSE,lose);
for(var i:uint;i {
var target:Sprite=new Sprite();
target.graphics.beginFill(0xffffff*Math.random());
target.graphics.drawCircle(0,0,40);
target.graphics.endFill();
target.scaleX=target.scaleY=Math.random()*0.5+0.5;
stage.addChildAt(target,0);
target.x=Math.random()*(SW-target.width)+target.width/2;
target.y=Math.random()*(SH-target.height)+target.height/2;
}
function kill(e:MouseEvent):void
{
if(e.target!=e.currentTarget){
score ++;
tf.text="打中了,已经打中了"+score+"抢";
}else{
miss ++;
tf.text="失误了,已经失误了"+miss+"抢";
}
if(score>=10)
{
stage.dispatchEvent(new Event(YOU_WIN));
}
else if(miss>=10)
{
stage.dispatchEvent(new Event(YOU_LOSE));
}
}
function win(e:Event):void{
tf.text="you win";
stage.removeEventListener(MouseEvent.CLICK,kill);
stage.removeEventListener(YOU_WIN,win);
stage.removeEventListener(YOU_LOSE,lose);
}
function lose(e:Event):void{
tf.text="you lose";
stage.removeEventListener(MouseEvent.CLICK,kill);
stage.removeEventListener(YOU_WIN,win);
stage.removeEventListener(YOU_LOSE,lose);
}