Code4
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import caurina.transitions.Tweener;
[SWF(width = "465", height = "465", frameRate = "24", backgroundColor = "#000000")]
public class code4 extends Sprite {
private var _bar:Sprite = new Sprite();
private var _ball:Sprite = new Sprite();
private var _blockarea:Sprite = new Sprite();
private var _ballSize:Number;
private var _velocity:Number;
private var _velocityX:Number;
private var _velocityY:Number;
private var i:Number;
// コンストラクタ
public function code4() {
initializeHandler();
}
// 初期化
private function initializeHandler():void {
addChild(_blockarea);
_ballSize = 5;
_velocity = 15;
blockCreateHandler();
}
// スタンバイ
private function standbyHandler():void {
stage.addEventListener(MouseEvent.CLICK, startHandler);
}
// スタート
private function startHandler(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, startHandler);
_velocityX = _velocity * (Math.random() * 2) - 1;
_velocityY = _velocity * -1;
_bar.addEventListener(Event.ENTER_FRAME, barEnterFrameHandler);
_ball.addEventListener(Event.ENTER_FRAME, ballEnterFrameHandler);
}
// バームーブ
private function barEnterFrameHandler(event:Event):void {
_bar.x += (mouseX - _bar.x - _bar.width / 2) * 0.25;
if (_bar.hitTestObject(_ball)) {
_velocityY *= -1;
_ball.y = _bar.y - 5;
_bar.alpha = 0.5;
Tweener.addTween(_bar, { alpha:1, time:0.5, transition:"easeOutCirc" });
}
}
// ボールムーブ
private function ballEnterFrameHandler(event:Event):void {
_ball.x += _velocityX;
_ball.y += _velocityY;
wallCheckHandler();
}
// 壁
private function wallCheckHandler():void {
if (_ball.x - _ballSize < 0) {
_velocityX *= -1;
_ball.x = _ballSize;
}
else if (stage.stageWidth < _ball.x + _ballSize) {
_velocityX *= -1;
_ball.x = stage.stageWidth - _ballSize;
}
if (_ball.y - _ballSize < 0) {
_velocityY *= -1;
_ball.y = _ballSize;
}
else if (_ball.y > stage.stageHeight + 100) {
_bar.removeEventListener(Event.ENTER_FRAME, barEnterFrameHandler);
_ball.removeEventListener(Event.ENTER_FRAME, ballEnterFrameHandler);
Tweener.addTween(_blockarea, { alpha:0, time:0.5, transition:"easeInCirc", onComplete:initializeHandler });
}
}
// ブロック生成
private function blockCreateHandler():void {
if (_blockarea.numChildren > 0) {
for (i = 0; i < _blockarea.numChildren; i++) {
_blockarea.removeChildAt(i);
}
}
_blockarea.x = 10;
_blockarea.y = 10;
_blockarea.alpha = 0;
var LineX:Number = 0;
var LineY:Number = 0;
for (i = 0; i < 105; i++) {
var _block:Sprite = new Sprite();
_block.graphics.beginFill(Math.random() * 0xfffff0);
_block.graphics.drawRect(0, 0, 25, 15);
_block.graphics.endFill();
_block.x = 30 * LineX;
_block.y = 20 * LineY;
_block.addEventListener(Event.ENTER_FRAME, ballHitTestHandler);
_blockarea.addChild(_block);
LineX++;
if (LineX == 15) {
LineX = 0;
LineY += 1;
}
}
Tweener.addTween(_blockarea, { alpha:1, time:1, transition:"easeInOutCirc", onComplete:barCreateHandler });
}
// ブロック消滅
private function ballHitTestHandler(event:Event):void {
if (event.currentTarget.hitTestObject(_ball)) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, ballHitTestHandler);
_velocityX *= -1;
_velocityY *= -1;
_ball.y = (event.currentTarget.y + event.currentTarget.height) + _ballSize;
event.currentTarget.alpha = 0.5;
event.currentTarget.y -= 3;
Tweener.addTween(event.currentTarget, { y:stage.stageHeight + event.currentTarget.height, time:1, transition:"easeInCirc", onComplete:function() { event.currentTarget.y = -100; } });
}
}
// バー生成
private function barCreateHandler():void {
if (stage.getChildByName("bar") == null) {
_bar.graphics.beginFill(0x666666);
_bar.graphics.drawRect(0, 0, 80, 5);
_bar.graphics.endFill();
_bar.name = "bar";
_bar.x = stage.stageWidth / 2 - _bar.width / 2;
_bar.y = 445;
stage.addChild(_bar);
}
Tweener.addTween(_bar, { x:stage.stageWidth / 2 - _bar.width / 2, y:445, time:1, transition:"easeInOutCirc", onComplete:ballCreateHandler });
}
// ボール生成
private function ballCreateHandler():void {
if (stage.getChildByName("ball") == null) {
_ball.graphics.beginFill(0xffffff);
_ball.graphics.drawCircle(0, 0, _ballSize);
_ball.graphics.endFill();
_ball.name = "ball";
stage.addChild(_ball);
}
_ball.x = stage.stageWidth / 2;
_ball.y = 400;
Tweener.addTween(_ball, { x:stage.stageWidth / 2, y:439, time:0.5, transition:"easeOutBounce", onComplete:standbyHandler });
}
}
}