itsukichang

フロントエンドが得意なエンジニア.ダーツと旅行とギターが好き

Daily Arduino×Flash #4

今日は手抜き感MAXです.

#4 可変抵抗と壁打ち

可変抵抗でバーを操作して,ボールを跳ね返して行くだけ.
ブロックすらありません.まったく手抜きですね!!1

回路図

プログラム
  • Main.as
package {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import itsukichang.*;
	import funnel.*;
	
	public class Main extends Sprite {
		
		private var _b:Ball;
		private var _bar:Bar;
		
		private var _vx:Number = 10;
		private var _vy:Number = 10;
		private var _count:Number = 0;
		private var _highScore:uint = 0;
		private const R:Number = 10;
		private const BAR_W:Number = 60;
		
		private var _mes:Texts;
		private var _cnt:Texts;
		private var _high:Texts;
		
		private var _pin:Pin;
		private var _arduino:Arduino;
		
		public function Main() {
			init();
		}
		
		private function init():void {
			_arduino = new Arduino(Arduino.FIRMATA);
			_pin = _arduino.analogPin(0);
			
			_b = new Ball(R,0xffffff);
			addChild(_b);
			_bar = new Bar(BAR_W, 10, 0xffffff);
			_bar.y = 350;
			addChild(_bar);
			_mes = new Texts("Click to START", 25, 0xffffff);
			addChild(_mes);
			_cnt = new Texts("", 15, 0xffffff);
			addChild(_cnt);
			_cnt.x = stage.stageWidth - 100;
			_high = new Texts("", 15, 0xffffff);
			addChild(_high);
			ballInit();
			mesInit();
		}
		
		private function ballInit():void {
			_b.x = stage.stageWidth / 2 - R / 2;
			_b.y = stage.stageHeight / 2 - R / 2;
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function mesInit():void {
			_mes.txt = "Click ot START";
			_mes.x = stage.stageWidth / 2 - _mes.width / 2;
			_mes.y = 300;
		}
		
		private function onClick(e:MouseEvent):void {
			_b.addEventListener(Event.ENTER_FRAME, onLoop);
			_mes.visible = false;
			_count = 0.5;
			stage.removeEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onLoop(e:Event):void {
			//trace(_pin.value * );
			var _x:Number = _pin.value * (stage.stageWidth - BAR_W);
			trace(_x);
			_bar.x = _x;
			
			//_bar.x = mouseX;
			//_bar.y = mouseY;
			_b.x += _vx;
			_b.y += _vy;
			if (_b.x >= stage.stageWidth - R/2 || _b.x <= 0 ){
				_vx *= -1;
			}
			if (_b.y <= 0 || (_b.x >= _bar.x) && (_b.x < _bar.x + BAR_W) && (_b.y >= _bar.y - R) && (_b.y < _bar.y + 10)) {
				_vy *= -1;
				_count += 0.5;
				_cnt.txt = int(_count).toString() + "points";
				_cnt.stage.stageWidth / 2 - _cnt.width / 2;
			}
			if (_b.y >= stage.stageHeight - R/2 ) {
				_mes.visible = true;
				_mes.txt = "GAME OVER";
				_mes.x = stage.stageWidth / 2 - _mes.width / 2;
				_mes.y = 300;
				
				trace(_highScore);
				if (int(_count) > _highScore)		_highScore = int(_count);
				_high.txt = "HIGH SCORE :" + _highScore.toString();
				
				_b.removeEventListener(Event.ENTER_FRAME, onLoop);
				ballInit();
			}
		}
	}
}
  • Bar.as //バーを生成するクラス
package {
	
	import flash.display.Sprite;
	
	public class Bar extends Sprite {
		
		private var _w:Number;
		private var _h:Number;
		private var _col:uint;
		
		private var _sp:Sprite;
		
		public function Bar(w:Number, h:Number, col:uint = 0x0) {
			_w = w;
			_h = h;
			_col = col;
			init();
		}
		
		private function init():void {
			_sp = new Sprite();
			_sp.graphics.beginFill(_col);
			_sp.graphics.drawRect(0,0,_w,_h);
			_sp.graphics.endFill();
			addChild(_sp);
		}
	}
}	
  • Ball.as //例によって丸を作るクラス

Ball.asは毎回同じなので前回のとかを参照くださいませ.

動作してる図(動画とか)

バーの操作を可変抵抗をぐりぐり回してやります.それだけです

余談

wonderflにあげるのも恥ずかしいレベルの手抜きなので自粛^o^


次回はちょっと工夫した動き,回路なものつくります><