シンプルなタイミングバー
初投稿テスト
タイミングよくバーをクリックすると
アタリ判定を返す
ゲームもどきです。
/**
* Copyright kokejp ( http://wonderfl.net/user/kokejp )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/89SX
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
[SWF(frameRate="50")]
public class SimpleTimingBarGame extends Sprite {
private var timer:Timer = new Timer(20);
private var textArea:TextField = new TextField();
private var timingBar:TimingBar = new TimingBar( new HitArea(20, 8), 300, 60);
public function SimpleTimingBarGame() {
// write as3 code here..
timingBar.addEventListener(TimingBar.AREA_HIT, gameEventHandler);
timingBar.addEventListener(TimingBar.AREA_MISSED, gameEventHandler);
addChild(timingBar);
textArea.width=300;
textArea.y = 80;
textArea.selectable=false;
textArea.text="赤いラインが緑エリアを通っているときに\nバーをクリックするとアタリ判定";
addChild(textArea);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(e:TimerEvent):void
{
timingBar.update();
}
private function gameEventHandler(e:Event):void
{
switch(e.type) {
case TimingBar.AREA_HIT:
textArea.text = "hit!";
break;
default:
case TimingBar.AREA_MISSED:
textArea.text = "miss...";
break;
}
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
internal class TimingBar extends Sprite
{
protected var _length:int;
protected var _height:int;
private var _hitArea:HitArea;
protected var _cursorPosition:int = 100;
public static const AREA_HIT:String = "hit!";
public static const AREA_MISSED:String = "miss..";
protected var bar:Sprite;
public function TimingBar(hitarea:HitArea,length:int=100,height:int=20)
{
_length = length;
_height = height;
_hitArea = hitarea;
bar = new Sprite();
addChild(bar);
drawBarBox();
addEventListener(MouseEvent.MOUSE_DOWN,mousedownHandler);
}
private function mousedownHandler(e:MouseEvent):void
{
if ( _hitArea.hitAreaCheck(_cursorPosition)) {
dispatchEvent(new Event(AREA_HIT))
}else {
dispatchEvent(new Event(AREA_MISSED));
}
}
public function update():void {
_cursorPosition--;
if (_cursorPosition < 0) _cursorPosition += _length;
drawBarBox();
drawCursor();
}
private function drawBarBox():void {
bar.graphics.clear();
bar.graphics.beginFill(0x000000);
bar.graphics.drawRect(0, 0, _length, _height);
bar.graphics.beginFill(0x00ff00);
bar.graphics.drawRect(_hitArea.begin, 0, _hitArea.width, _height);
bar.graphics.endFill();
}
protected function drawCursor():void {
bar.graphics.lineStyle(1, 0xff0000);
bar.graphics.moveTo(_cursorPosition, 0);
bar.graphics.lineTo(_cursorPosition, _height);
}
}
internal class HitArea
{
private var _begin:int = 10;
private var _width:int = 10;
public function HitArea(begin:int,width:int)
{
_begin = begin;
_width = width;
}
public function hitAreaCheck(cursorPosition:int):Boolean {
return (cursorPosition >= _begin && cursorPosition <= _begin + _width);
}
public function get begin():int { return _begin; }
public function get width():int { return _width; }
}