▼Flashプログラミングメモ▼
マウスイベントの処理
マウスイベントを処理するFlashを作成する。[Flash Lite 2では使用不可]
Mouseクラスのリスナー
onMouseDown マウスボタンを押した時に発生 onMouseUp マウスボタンを離した時に発生 onMouseMove マウスカーソルを動かした時に発生 onMouseWheel マウスホイールを回転した時に発生
ソースコード
MouseEx.as
//マウスイベントの処理 class MouseEx extends MovieClip { private var label:TextField; //ラベル private var mouseDown:String ="アップ";//マウスダウン private var mouseX:Number =0; //マウスX座標 private var mouseY:Number =0; //マウスY座標 private var mouseDelta:Number=0; //マウスホイール //コンストラクタ function MouseEx() { //ラベルの追加 label=Util.addLabel(this); update(); //マウスリスナー Mouse.addListener(this); } //マウスダウンイベントの処理 private function onMouseDown() { mouseDown ="ダウン"; mouseX =Math.floor(_xmouse); mouseY =Math.floor(_ymouse); mouseDelta=0; update(); } //マウスアップイベントの処理 private function onMouseUp() { mouseDown ="アップ"; mouseX =Math.floor(_xmouse); mouseY =Math.floor(_ymouse); mouseDelta=0; update(); } //マウスムーブイベントの処理 private function onMouseMove() { mouseX =Math.floor(_xmouse); mouseY =Math.floor(_ymouse); mouseDelta=0; update(); } //マウスホイールイベントの処理 private function onMouseWheel(delta:Number) { mouseDelta=delta; update(); } //更新 private function update():Void { label.setText( "マウスダウン:"+mouseDown+"\n"+ "マウス座標:"+mouseX+","+mouseY+"\n"+ "マウスホイール:"+mouseDelta); } }
Util.as
//ユーティリティ class Util { //ラベルの追加 static function addLabel(parent:MovieClip, text:String):TextField { //初期値 if (text==undefined) text=""; //インスタンス名の自動生成 if (parent.nameIdx==undefined) parent.nameIdx=0; var name:String="_name"+(parent.nameIdx++); //テキストフィールドの追加 parent.createTextField(name, parent.getNextHighestDepth(),0,0,0,0); var mc:TextField=parent[name]; mc.autoSize ="left";//オートサイズ mc.selectable=false; //選択不可 mc.text=text; //テキスト mc._x =0; //X座標 mc._y =0; //Y座標 //書式 mc.format=new TextFormat(); mc.format.font ="_等幅"; //フォント mc.format.size =12; //文字サイズ mc.format.color=0x000000;//文字色 mc.setTextFormat(mc.format); //XY座標の指定 mc.setXY=function(_x:Number,_y:Number) { this._x=_x; this._y=_y; } //テキストの指定 mc.setText=function(_text:String) { this.text=_text; this.setTextFormat(this.format); } //文字サイズの指定 mc.setFontSize=function(size:Number) { this.format.size=size; this.setTextFormat(this.format); } //文字色の指定 mc.setFontColor=function(color:Number) { this.format.color=color; this.setTextFormat(this.format); } return mc; } }
application.xml
<?xml version="1.0" encoding="iso-8859-1"?> <movie version="7" width="240" height="240" framerate="30"> <background color="#ffffff"/> <clip import="classes.swf" /> <frame> <library> <!-- クラス --> <clip id="Application" class="MouseEx" /> <!-- リソース --> </library> <place id="Application" name="app" x="0" y="0" depth="1000" /> </frame> </movie>
コンパイル
mtasc -version 7 -swf classes.swf -header 240:240:30 MouseEx
swfmill simple application.xml MouseEx.swf