TweenerのaddCallerをクリアする [ Flash ]
オデのためのオデによるブログ "apeirophobia"です。
さて
TweenerでaddCallerを途中で解除したいぜ!というときにどすればええのん?
という質問をされたので書いておきます。
addCallerの場合はproperyが設定されていないのでオデはざっくりと
Tweener.removeTweens(this);で対応しています。
ただこれだとその要素に対して設定されているすべてのTweenが削除されてしまいますが・・。
オデは基本的にaddCallerを停止させるときはremoveのタイミングで、他のモーションも一括で削除することが多いからそんなに不便さは感じていませんけど、まぁ個別に細かく制御したい場合はaddCallerを割り当てるためのSprite(Shapeでもいいのかしら?)を作ってそこで管理するって感じですかね?
そうする場合の注意点なのですが、
そのDisplayObjectに複数のtweenを割り当て、特定のプロパティのみをクリアしようとした場合、
import caurina.transitions.Tweener;
Tweener.addCaller(this, { time:1, count:1, onComplete:onCount1 } );
Tweener.addCaller(mc, { time:2, count:1, onComplete:onCount2 } );
Tweener.addTween(mc,{x:200,y:100,time:20});
function onCount1() {
trace("onCount1");
}
function onCount2() {
trace("onCount2");
}
function clearCaller(e) {
Tweener.removeTweens(this);
Tweener.removeTweens(mc,"x");
}
mc.addEventListener(flash.events.MouseEvent.CLICK,clearCaller);で実行すると
TypeError: Error #1009: null のオブジェクト参照のプロパティまたはメソッドにアクセスすることはできません。
at caurina.transitions::Tweener$/affectTweens()
at caurina.transitions::Tweener$/removeTweens()
at test_addCallerClear_fla::MainTimeline/clearCaller()
が戻ってきます。
これ自分自身(this)だとエラーにならないんですけどね、多分onCompleteの参照が良くないんでしょうけど・・。
なのでaddCallerを管理するばやいは
import caurina.transitions.Tweener;
Tweener.addCaller(this, { time:1, count:1, onComplete:onCount1 } );
Tweener.addCaller(mc2, { time:2, count:1, onComplete:onCount2 } );
Tweener.addTween(mc,{x:200,y:100,time:20});
function onCount1() {
trace("onCount1");
}
function onCount2() {
trace("onCount2");
}
function clearCaller(e) {
Tweener.removeTweens(this);
Tweener.removeTweens(mc2);
Tweener.removeTweens(mc,"x");
}
mc.addEventListener(flash.events.MouseEvent.CLICK,clearCaller);という風にきちんとaddCallerだけを管理するようにしてあげてください。
この辺はaddCallerがなんか戻り値とか戻してくれればいいんですけどねー。まぁメモリの問題を考えるとあまり参照を持たせたくないんだと思いますが・・。自分で昔作っていたモーションエンジンはそういうつくりにしていたので、任意のタイマーをクリアとかできたんだけど、その辺に少し不便さを感じます。
今後の対応に期待。
※もし他の方法があれば教えてください。エロイ人。
