ホームに戻る
ActionScript3.0 ヒット&ブロー
// Main.as
package{
import flash.display.Sprite;
import flash.text.TextField;
[SWF(width="350", height="200", backgroundColor="0xFFFFFF")]
public class Main extends Sprite
{
private var px:int;
private var py:int;
private var button:Array;
private var rect1:Rect1;
private var text_field:TextField;
private var answer:Array;
// ヒット&ブローの判定処理
public function judge():void {
var i:int;
var c:int;
var hit:int = 0;
var blow:int = 0;
for(i = 0; i < 4; i++){
c = button[i].getSelectedColor();
if(c - answer[i] == 0){
hit++; // 位置と色が同じならばヒットとする
}
}
for(i = 0; i < 4; i++){
c = button[i].getSelectedColor();
for(var t:int = 0; t < 4; t++){
if(c - answer[i] == 0){
continue; // ヒットの場合はブローでカウントしないようにする
}
if(c - answer[t] == 0){
blow++; // ブローをカウントする
break; // 1つの位置でブローを2度カウントしないように break する
}
}
}
// ヒット&ブローを表示
text_field.appendText("Hit:" + hit + " Blow:" + blow + "\n");
}
public function Main()
{
button = new Array();
for(var i:int = 0; i < 4; i++){
button[i] = new Circle1(50 * (i + 1), 50);
addChild(button[i]);
}
rect1 = new Rect1(this, 50, 100, 150, 50);
addChild(rect1);
text_field = new TextField();
text_field.border = true; // 枠を表示する
text_field.x = 250; // x 座標
text_field.y = 0; // y 座標
text_field.width = 100 - 1; // 幅
text_field.height = 200 - 1; // 高さ
addChild(text_field);
answer = new Array();
for(var t:int; t < 4; t++){
// ランダムで解答を用意
answer[t] = Math.floor(Math.random() * 5);
trace(answer[t]);
}
}
}
}
// Circle1.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Graphics;
public class Circle1 extends Sprite
{
private const red:int = 0xFF0000; // 赤
private const blue:int = 0x0000FF; // 青
private const green:int = 0x00FF00; // 緑
private const yellow:int = 0xFFFF00; // 黄
private const orange:int = 0xFF8F00; // 橙
private const r:int = 20; // 円の半径
private var px:int; // 現在の位置 X座標
private var py:int; // 現在の位置 Y座標
private var colorsArray:Array; // 色の記録用
private var selectedColor:int = 0; // 現在の色
private var g:Graphics;
public function Circle1(px:int = 0, py:int = 0)
{
this.px = px;
this.py = py;
colorsArray = new Array();
colorsArray[0] = red;
colorsArray[1] = blue;
colorsArray[2] = green;
colorsArray[3] = yellow;
colorsArray[4] = orange;
g = this.graphics;
this.draw();
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
public function setX(px:int):void { this.px = px;}
public function setY(py:int):void { this.py = py;}
public function getX():int { return this.px;}
public function getY():int { return this.py;}
public function draw():void
{
g.lineStyle(1, 0x000000, 1.0); // 線の太さ、色、アルファ値
g.beginFill(colorsArray[selectedColor], 1.0); // 面のスタイル設定
g.drawCircle(this.getX(), this.getY() , this.r); // 円を描画
g.endFill();
}
public function getSelectedColor():int
{
return selectedColor;
}
private function onMouseDown(event:MouseEvent):void
{
selectedColor++;
if(selectedColor == colorsArray.length)
{
selectedColor = 0;
}
this.draw();
}
}
}
// Rect1
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Graphics;
import flash.text.TextField;
import flash.text.TextFormat;
public class Rect1 extends Sprite
{
private var px:int;
private var py:int;
private var pw:int;
private var ph:int;
private var g:Graphics;
private var text_field:TextField;
private var main:Main;
public function Rect1(main:Main, px:int = 0, py:int = 0, pw:int = 0, ph:int = 0)
{
this.main = main;
this.px = px;
this.py = py;
this.pw = pw;
this.ph = ph;
g = this.graphics;
this.draw();
text_field = new TextField();
var format:TextFormat = new TextFormat();
format.size = 30; // 文字のポイントサイズ
text_field.defaultTextFormat = format;
text_field.text = "Judge";
text_field.selectable = false; // 枠を表示する
text_field.x = this.px + (pw - text_field.textWidth) / 2; // x 座標
text_field.y = this.py + (ph - text_field.textHeight) / 2; // y 座標
text_field.width = 200; // 幅
text_field.height = 100; // 高さ
addChild(text_field);
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
public function setX(px:int):void { this.px = px;}
public function setY(py:int):void { this.py = py;}
public function getX():int { return this.px;}
public function getY():int { return this.py;}
public function draw(color:int = 0xFFFFFF):void
{
g.lineStyle(2, 0x000000, 1.0); // 線の太さ、色、アルファ値
g.beginFill(color, 1.0); // 面のスタイル設定
g.drawRoundRect(this.px, this.py, this.pw, this.ph, 20, 20);
g.endFill();
}
private function onMouseDown(event:MouseEvent):void
{
this.draw(0x000000);
text_field.textColor = 0xFFFFFF;
}
private function onMouseOut(event:MouseEvent):void
{
this.draw();
text_field.textColor = 0x000000;
}
private function onMouseUp(event:MouseEvent):void
{
this.draw();
text_field.textColor = 0x000000;
main.judge(); // Main の judge を呼ぶ
}
}
}