Flash AS3タイマーの質問
-
03-07-2019 - |
質問
25個のムービークリップをステージに追加し、x:0、y:0からアニメーション化するtimerEventがあります。これはすべて正常に機能します。私がやりたいのは、各ムービークリップに、ステージに追加された最後のムービークリップよりも25px多いy値を割り当てることです。タイマーがループするたびに数値をインクリメントしようとして、少しテストしましたが、インクリメントしませんでした。 forループ/配列も使用すべきですか?
事前に感謝します。 ジョノ
import flash.events.*;
import caurina.transitions.*;
bringItemsOn();
var itemTimer:Timer;
function bringItemsOn():void {
itemTimer=new Timer(300);
itemTimer.addEventListener(TimerEvent.TIMER,itemTimer_tick);
itemTimer.start();
}
function itemTimer_tick(e:TimerEvent):void {
itemTimer.currentCount-1;
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(Math.random() * 0x000000);
mc.graphics.drawRect(0,0,stage.stageWidth,25);
mc.x=0;
mc.y=0;
addChild(mc);
Tweener.addTween(mc,{y:Math.random()*1000 - 500,
x:0,
time:.9,
transition:"easeOutExpo"});
if (itemTimer.currentCount>=25) {
itemTimer.removeEventListener(TimerEvent.TIMER,itemTimer_tick);
}
}
解決
正直なところ、最初はforループでこれをすべて設定していました。 forループが終了したら、各アイテムを適切に配置し、TweenLiteまたはお気に入りのTweeningパッケージを使用します。
package
{
import flash.display.Sprite;
import flash.events.Event;
import gs.TweenLite;
import gs.easing.*;
public class ExampleDocumentClass extends Sprite
{
if(stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init(), false, 0, true);
}
private function _init(e:Event =null):void
{
for(var i:int = 0; i < 25; i++)
{
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(Math.random() * 0x000000);
mc.graphics.drawRect(0,0,stage.stageWidth,25);
//Seperates them by 25, their width, so they will touch.
mc.x= i * 25; //i * 25 + 1 leaves a space in between.
mc.y=0;
addChild(mc);
TweenLite.to(mc, 0.9, {y:math.random()*1000-500, x: 0, ease: Expo.easeOut});
if(i == 24) //Total length - 1
{
//The loop ended.
}
}
}
forやwhileループなどのコードブロック内では画面の更新は行われないことに注意してください。前の項目xが設定されたばかりであるにもかかわらず、画面に描画されていないため、これらを互いに示す間隔で配置することはできません。すべての画像が同じ幅ではないが、次から次へと開始する必要があるシステムを構築するには、指定されたローダーに対してEvent.COMPLETEイベントが発生するのを待つ必要があります。属性。 25ピクセル間隔で配置したいだけで、同じサイズなので、単に「i」を使用します。それらを分離する
起きていること: i * 25 = 0; i ++; i * 25 = 25; i ++; i * 25 = 50;
お分かりのように、探していた間隔を達成しました。
ブライアンホッジ
hodgedev.com
blog.hodgedev.com
他のヒント
これを使用:
import flash.events.*;
import caurina.transitions.*;
bringItemsOn();
var extra:int = 0;
var itemTimer:Timer;
function bringItemsOn():void {
itemTimer=new Timer(300);
itemTimer.addEventListener(TimerEvent.TIMER,itemTimer_tick);
itemTimer.start();
}
function itemTimer_tick(e:TimerEvent):void {
itemTimer.currentCount-1;
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(Math.random() * 0x000000);
mc.graphics.drawRect(0,0,stage.stageWidth,25);
mc.x += extra;
mc.y=0;
extra = mc.width;
/*u can use mc's width for different x value's or
make ur own like extra += 10; each mc.x will be different */
addChild(mc);
Tweener.addTween(mc,{y:Math.random()*1000 - 500,
x:0,
time:.9,
transition:"easeOutExpo"});
if (itemTimer.currentCount>=25) {
itemTimer.removeEventListener(TimerEvent.TIMER,itemTimer_tick);
}
}
変数をインクリメントしたりyに25pxを追加しようとした部分は実際には見つかりませんでしたが、これを試してください:
function itemTimer_tick(e:TimerEvent):void {
...
mc.y = itemTimer.currentCount * 25;
addChild(mc);
...
}
本当に異なるアプローチは、 Tweener.addTween
メソッドの delay
プロパティを使用することです。すべてのムービークリップを開始し、すべてのムービークリップの遅延を少し増やしますか?