Flash AS3 Timer Question

I have a timerEvent that adds 25 movie clips to the scene and animates them from x: 0, y: 0, everything works fine! What I would like to do is assign each movie clip a y value of 25 pixels more than the last movieClip added to the scene. I did a little test, trying to increase the numerical value every time the timer did a cycle, but it did not increase. Should I use a for / array loop too?

Thanks in advance. Jono

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); } } 
+4
source share
4 answers

Honestly, I would put all this first on a for loop. Once you finish the for loop, put each element accordingly, then use TweenLite or your favorite Tweening package.

 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. } } } 

It is important to note that screen updates do NOT occur in code blocks such as for and while loops. You could not place them on the basis of each other as I showed, because although the previous x elements can just be set, it was not displayed on the screen; To create a system in which they say that all images do not have the same width, but need to start after another, we must wait for the Event.COMPLETE event for this loader so that we can access it in width and other attributes. Since you just wanted to place them at a distance of 25 pixels from each other, and they are the same size, we just use โ€œiโ€ to separate them.

What happens: i * 25 = 0; I ++; i * 25 = 25; I ++; i * 25 = 50;

as you see, we have reached the interval we were looking for.

Brian Hodge
hodgedev.com blog.hodgedev.com

+3
source

use this:

  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 width for different x value 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); } } 
+2
source

You donโ€™t actually find the part where you tried to increase the value of the variable or add 25px to y, but try the following:

 function itemTimer_tick(e:TimerEvent):void { ... mc.y = itemTimer.currentCount * 25; addChild(mc); ... } 
0
source

I really could use the delay method of the Tweener.addTween method. Initiate all movie clips and slightly increase the delay for each of them?

0
source

All Articles