Shooting.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:action="action.*" layout="absolute"
width="400" height="420">
<action:ShootingAction />
<mx:Label id="scoreLabel" x="280" height="20" fontSize="15" />
<mx:UIComponent id="field" y="20" width="400" height="400" />
</mx:Application>
action/ShootingAction.as
package action {
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import model.*;
import mx.controls.Alert;
import mx.core.IMXMLObject;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
public class ShootingAction implements IMXMLObject {
private var view:Shooting;
private var score:int = 0;
public function ShootingAction() {
}
public function initialized(document:Object, id:String):void {
view = document as Shooting;
view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
}
private function creationCompleteHandler(event:FlexEvent):void {
Alert.show("Start", "", 4, null, alertCloseHandler);
}
private function circleClickHandler(event:MouseEvent):void {
event.target.removeEventListener(MouseEvent.CLICK, circleClickHandler);
score += 10;
view.scoreLabel.text = "Score: " + score;
view.field.removeChildAt(0);
var c:Circle = new Circle();
c.addEventListener(MouseEvent.CLICK, circleClickHandler);
view.field.addChild(c);
}
private function alertCloseHandler(event:CloseEvent):void {
score = 0;
view.scoreLabel.text = "Score: " + score;
if (view.field.numChildren > 0) {
view.field.removeChildAt(0);
}
var c:Circle = new Circle();
c.addEventListener(MouseEvent.CLICK, circleClickHandler);
view.field.addChild(c);
var timer:Timer = new Timer(10000, 1);
timer.addEventListener(TimerEvent.TIMER, timerCompleteHandler);
timer.start();
}
private function timerCompleteHandler(event:TimerEvent):void {
Alert.show("得点は" + score + "点です", "", 4, null, alertCloseHandler);
}
}
}
model/Circle.as
package model {
import flash.display.Sprite;
public class Circle extends Sprite {
public function Circle() {
this.x = Math.random() * 380 + 10;
this.y = Math.random() * 380 + 10;
this.graphics.beginFill(0xFF0000);
this.graphics.drawCircle(0, 0, 10);
this.graphics.endFill();
}
}
}
PR