2008年12月09日
AS3でカスタムイベント
ActionScriptのカスタムイベントを使ってみたテスト。 自分でイベントを定義できるって便利。
で、つくってみたもの。 マウスカーソルの位置に波紋ができます。 イベントがどうとかは見た目じゃわからないけども。
FlashPlayer9以上が必要です。
ソースコードは続きに。
メインのクラス。 タイマーを設定して一定時間おきに波紋を発生させる。 波紋が消失したときのイベントをハンドルするようにしておき、 消失した際にはRippleのインスタンスを破棄する。
package { import flash.display.Sprite; import flash.events.TimerEvent; import flash.utils.Timer; public class Main extends Sprite { public function Main() { var timer:Timer = new Timer(100); timer.start(); timer.addEventListener(TimerEvent.TIMER, touch); } public function touch(evt:TimerEvent) :void { var ripple:Ripple = new Ripple(stage, mouseX, mouseY); stage.addChild(ripple.getRipple()); // カスタムイベントを使用 ripple.addEventListener(VanishEvent.VANISH, function (evt:VanishEvent) { // 波紋消失時にオブジェクトを破棄 ripple = null; }); } } }
波紋クラス。 フレーム毎に拡大しつつ薄くなる。 アルファが一定以下になるとVANISHイベントを発生。
package { import flash.display.Sprite; import flash.display.Stage; import flash.events.EventDispatcher; import flash.events.Event; public class Ripple extends Sprite { private var mRipple :Sprite; private var mStage :Stage; private const V_SCALE :Number = 1.08; private const V_ALPHA :Number = 0.9; public function Ripple(stage, x, y, color = 0xFFFFFF, radius = 5, weight = 1) { mStage = stage; mRipple = new Sprite(); mRipple.x = x; mRipple.y = y; mRipple.graphics.lineStyle(weight, color); mRipple.graphics.drawCircle(0, 0, radius); mRipple.addEventListener(Event.ENTER_FRAME, step); } private function step(evt:Event) :void { if(mRipple.alpha < 0.01) { mRipple.removeEventListener(Event.ENTER_FRAME, step); mStage.removeChild(mRipple); // カスタムイベントを発生させる dispatchEvent(new VanishEvent(VanishEvent.VANISH)); return; } mRipple.scaleX *= V_SCALE; mRipple.scaleY *= V_SCALE mRipple.alpha *= V_ALPHA; } public function getRipple() :Sprite { return mRipple; } } }
カスタムイベントを定義するクラス。 基本はEventクラスを継承するだけ。
package { import flash.events.Event; // このクラスがイベントリスナーが受け取るイベントオブジェクトになる class VanishEvent extends Event { public static const VANISH :String ='VANISH'; public function VanishEvent(type :String) { super(type); } } }
GoogleReaderを3ペイン表示にするスクリプトのα版
クリック動作を無効にするジョークブックマークレット
文字サイズを拡大縮小するスクリプト
ダブルクリックでスクロールするスクリプト
電卓を表示するブックマークレット
ごくごく一部の顔文字を絵文字に置き換えるスクリプト
クリック動作を無効にするジョークブックマークレット
文字サイズを拡大縮小するスクリプト
ダブルクリックでスクロールするスクリプト
電卓を表示するブックマークレット
ごくごく一部の顔文字を絵文字に置き換えるスクリプト
Posted by Handle at 01:20│Comments(0)│プログラミング
コメントフォーム