flash on 2010-1-14
/**
* Copyright akitsukada ( http://wonderfl.net/user/akitsukada )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gs0m
*/
package {
import flash.events.*;
import flash.text.TextField;
import flash.utils.IDataInput;
import flash.display.Sprite;
public class BlockBreak extends Sprite {
private const stageWidth:int = 8;
private const stageHeight:int = 18;
private const stageX:int = 30;
private const stageY:int = 45;
private const blockWidth:int = 40;
private const blockHeight:int = 20;
private var currentStage:int = 1;
private var blocks:Array;
private var walls:Array;
private var bar:Bar;
public function BlockBreak() {
this.init();
this.addListeners();
this.loadStage(1);
}
// 初期化。ステージの背景と外枠を描画など
public function init():void {
// 色設定
var bgColor:uint = 0x677667;
// 背景塗りつぶし
var bg:Sprite = new Sprite();
graphics.beginFill(bgColor);
graphics.drawRect(
this.stageX + this.blockWidth,
this.stageY + this.blockHeight,
this.blockWidth * this.stageWidth,
this.blockHeight * this.stageHeight
);
addChild(bg);
// 壁を配置していく
var w:Wall;
// 天井
for (var i:int = 1; i < (stageWidth + 2); i++) {
w = new Wall(
this.stageX + (i * this.blockWidth),
this.stageY,
this.blockWidth,
this.blockHeight
);
addChild(w);
}
// 左右の壁
for (i = 0; i < (stageHeight + 1); i++) {
w = new Wall (
this.stageX,
this.stageY + (i * this.blockHeight),
this.blockWidth,
this.blockHeight
);
addChild(w);
w = new Wall (
this.stageX + (this.stageWidth + 1) * this.blockWidth,
this.stageY + (i * this.blockHeight),
this.blockWidth,
this.blockHeight
);
addChild(w);
}
// load bar;
this.bar = new Bar(
this.stageX + (this.blockWidth * Math.floor(this.stageWidth / 2)),
this.stageY + this.blockHeight * (this.stageHeight - 1)
);
addChild(bar);
}
public function addListeners() :void {
var tf:TextField = new TextField();
tf.text= "!!!";
tf.width=400;
tf.height=200;
addChild(tf);
tf.addEventListener(
MouseEvent.CLICK,
function(e:MouseEvent):void {
this.bar.move();
tf.text = "11133";
}
);
}
public function loadStage(num:int):void {
var b:Block;
// remove current stage's blocks
for each (b in blocks) {
removeChild(b);
}
this.blocks = new Array();
// read new stage's blocks data
var st:Array = Stage.getStage(num);
// put blocks on stage
for (var y:int = 0; y < st.length; y++) {
for (var x:int = 0; x < st[y].length; x++) {
switch (st[y][x]) {
case 1:
b = new Block(
(x + 1) * blockWidth + stageX,
(y + 1) * blockHeight + stageY,
blockWidth,
blockHeight
);
stage.addChild(b);
this.blocks.push(b);
break;
case 0:
break;
}
}
}
}
}
}
import flash.display.Sprite;
class Stage {
private static var stageData:Array = [
[ // Stage 1
[0,0,0,0,0,0,0,0],
[1,1,1,1,1,0,1,1],
[1,1,1,1,0,1,1,1]
],
[ // Stage 2
[0,0,0,0,0,0,0,0],
[1,1,1,1,0,1,1,1]
]
];
public static function getStage(num:int):Array {
return stageData[num - 1];
}
}
import flash.display.Sprite;
class Bar extends Sprite {
private var speed:int = 5;
private var barW:int = 80;
private var barH:int = 20;
public function Bar(x:Number, y:Number) {
var bdColor:uint = 0x000000;
var inColor:uint = 0x22ff22;
graphics.beginFill(bdColor);
graphics.drawRoundRect(x, y, this.barW, this.barH, 15, 15);
graphics.beginFill(inColor);
graphics.drawRoundRect(x + 1, y + 1, this.barW - 2, this.barH - 2, 15, 15);
}
public function move() :void {
// dist == 1 -> 右 , dist == 0 -> 左
x += 50;
}
}
class Block extends Sprite {
protected var bx:int;
protected var by:int;
protected var bw:int;
protected var bh:int;
public function Block(x:int, y:int, w:int, h:int) {
this.bx = x;
this.by = y;
this.bw = w;
this.bh = h;
this.draw();
}
public function draw():void {
var borderColor:uint = 0x000000;
var innerColor:uint =0x00ffff;
graphics.beginFill(borderColor);
graphics.drawRect(this.bx, this.by, this.bw, this.bh);
graphics.beginFill(innerColor);
graphics.drawRect(this.bx + 1, this.by + 1, this.bw - 2, this.bh - 2);
}
}
class Wall extends Block {
public function Wall(x:int, y:int, w:int, h:int) {
super(x, y, w, h);
}
public override function draw():void {
var borderColor:uint = 0x000000;
var innerColor:uint =0x434343;
graphics.beginFill(borderColor);
graphics.drawRect(bx, by, bw, bh);
graphics.beginFill(innerColor);
graphics.drawRect(bx + 1, by + 1, bw - 2, bh - 2);
}
}