Action Script 3.0 で絵を描く

今回は,as3で絵を描いていきたいと思います。

絵を描くのとそれをクリアするとこまで。

 

package {
import flash.display.*;
import flash.events.*;
import flash.media.*;

public class Main extends Sprite {

private var canvas:Sprite;
private var onClick:Boolean = false;  <span>// マウスが押されているか</span>

private var Clear:Clear1 = new Clear1();  <span>//クリアボタン</span>

public function Main() {

<span> // constructor code</span>

<span>//カンバス設定</span>
canvas = new Sprite();
canvas.graphics.beginFill(0xFFFFFF);
canvas.graphics.drawRect(0, 0, 500, 400);
canvas.graphics.endFill();
canvas.graphics.lineStyle(3, 0x000000);
addChild(canvas);

canvas.addEventListener(MouseEvent.MOUSE_DOWN, drawstart);
canvas.addEventListener(MouseEvent.MOUSE_MOVE, drawmove);
canvas.addEventListener(MouseEvent.MOUSE_UP, drawend);

<span>//クリアボタン表示</span>
addChild(Clear);
Clear.y = 350;
Clear.buttonMode = true;

<span>//クリアイベント</span>
Clear.addEventListener(MouseEvent.CLICK, drawClear);
function drawClear(e:MouseEvent):void {
canvas.graphics.clear();  <span>//canvasをクリア</span>
<span>                //canvasを再設定</span>
canvas.graphics.beginFill(0xFFFFFF);
canvas.graphics.drawRect(0, 0, 500, 400);
canvas.graphics.endFill();
canvas.graphics.lineStyle(3, 0x000000);
}
}

<span>//マウスクリック</span>
private function drawstart(e:MouseEvent):void {
onClick = true;
canvas.graphics.moveTo(canvas.mouseX, canvas.mouseY);
}

<span>//マウスドラッグ</span>
private function drawmove(e:MouseEvent):void {
if(onClick) canvas.graphics.lineTo(canvas.mouseX, canvas.mouseY);
}

<span>//マウスを離す</span>
private function drawend(e:MouseEvent):void {
onClick = false;
}

}
}

 

今度はこれにWEBカメラとキャプチャ機能をつけてWiiリモコンで描けるようにしようかと。