tweenerでスプライトを移動するアニメーションを作る。
tweenerはFlex/Flashで使えるトウィーンライブラリです。定期的にスプライトの属性を変更することで、
- スプライトの移動
- スプライトの透明化
といったアニメーションがさくっと作成できます。
インストール
tweenerのサイトからダウンロードします。Flexで使うので、「tweener_1_31_74_as3_swc.zip」を使用しました。展開したswcはlibrary-pathに追加して使います。
$ mxmlc -library-path=tweener.swc Sample.as
クリックでスプライトを移動するアニメーションのサンプル
以下はtweenerを使ってクリックした位置にスプライトを移動するサンプルです。
package { import flash.display.*; import flash.events.MouseEvent; import caurina.transitions.Tweener; // Tweenerのクラスをインポート public class Sample extends Sprite { public function Sample(){ // 円 var circle:Sprite = new Sprite(); addChild(circle); var g:Graphics = circle.graphics; g.beginFill( 0xFF4444 ); g.drawCircle( 0, 0, 20 ); g.endFill(); // イベントハンドラを設定 stage.addEventListener(MouseEvent.CLICK, function(ev:flash.events.MouseEvent):void { // Tweenerでクリックした位置にスプライトを移動する。 Tweener.addTween( circle, {x:ev.stageX, y:ev.stageY, time:0.5, transition:"linear"}); }); } } }
なかなか便利。