2009-03-02 66 views
4

我有一個timerEvent,將25個影片剪輯添加到舞臺上,並從x:0,y:0對它們進行動畫處理,這一切都正常!我想要做的是爲每個影片剪輯分配比上一次添加到舞臺上的影片剪輯多25px的y值。我做了一個小測試,試圖在每次定時器執行循環時增加一個數值,但它沒有增加。我應該使用for循環/數組嗎?Flash AS3計時器問題

在此先感謝。 城野

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); 
    } 
} 
+0

Tweener.addTween(MC,{Y:mc.y + 50中,x:0,時間:0.9,過渡: 「easeOutExpo」}); 如果我添加/使用了一個for循環,我可以引用每個對象並分配mc [i] + 50,所以只是想知道我是否可以集成 – Jono 2009-03-02 11:38:03

回答

3

老實說,我會設置這一切最初與for循環。完成for循環後,相應地放置每個項目,然後使用TweenLite或您最喜歡的補間包。

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 * 25 = 0; i ++; i * 25 = 25; i ++; i * 25 = 50;

正如你所看到的,我們已經達到了我們要找的間距。

布賴恩霍奇
hodgedev.com blog.hodgedev.com

0

沒有真正找到了一部分,你想增加一個變量或添加25px的爲y,但試試這個:

function itemTimer_tick(e:TimerEvent):void { 

    ... 

    mc.y = itemTimer.currentCount * 25; 
    addChild(mc); 

    ... 

} 
+0

Thanks dude!這就是我使用currentCount * jobbie作爲變量的地方var var mcFinal:Number =(itemTimer.currentCount * 50); Tweener.addTween(mc,{y:mcFinal,x:0,time:.9,transition:「easeOutExpo」}); 將其排序出來!謝謝一堆! – Jono 2009-03-02 11:49:12

+0

你知道,你也應該接受答案,如果它是可以接受的話:) – kkyy 2009-03-02 11:53:49

+0

哎呀,對不起這個遊戲新! – Jono 2009-03-02 12:02:31

0

我真的很不同的方法可以是使用delayTweener.addTween方法的性質。啓動所有影片剪輯並只增加一點點延遲?

2

使用這樣的:

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); 
    } 
}