AS3 Dynamically Load and Autoscroll Random size Movieclips

I'm very new to AS3 & Flash and wonder if anyone can take a look at this test piece.

Within my Flash file I have a number of movieclips that are randomly & dynamically added to the stage when a button is pressed. All the movieclips are the same width but have different heights and all the movieclips have their registration points positioned bottom-left (I'm using the button at this stage for test purposes only).

What I would like to do is 'automatically' (rather than using a button) add an endless stream of randomly chosen mc's from the library to the stage (that is to say mc's should be added in succession - i.e. one after the other without a gap between each one) that auto-scroll vertically downwards from the top of the stage towards the bottom (in the fashion of a never ending conveyor belt) and are then returned to the library when they are no longer visible on stage.

Anyone have any ideas.

//mc's are dynamically loaded & returned to the library
//mc's have 'export for Actionscript' property
//mc's have their anchor point placed bottom left

//stop all
stop();

//Speed of the vertical auto-scroll movement
var scrollSpeed:uint = 1;

//auto load random mc from library & place top left corner of stage

//load random mc via button for test purposes
McButton.addEventListener(MouseEvent.CLICK,attachMovieclip);
function attachMovieclip(event:MouseEvent):void{

//create a random number for choosing a mc from the array
var newNumber:int = (Math.random ()*14)

//define the mc's
var mc1:Red01 = new Red01();
var mc2:Red02 = new Red02();
var mc3:Red03 = new Red03();
var mc4:Orange01 = new Orange01();
var mc5:Orange02 = new Orange02();
var mc6:Orange03 = new Orange03();
var mc7:Yellow01 = new Yellow01();
var mc8:Yellow02 = new Yellow02();
var mc9:Green01 = new Green01();
var mc10:Green02 = new Green02();
var mc11:Blue01 = new Blue01();
var mc12:Blue02 = new Blue02();
var mc13:Purple01 = new Purple01();
var mc14:Purple02 = new Purple02();

//create an array which holds all the mc's
var Mcarray:Array = newArray(mc1,mc2,mc3,mc4,mc5,mc6,mc7,mc8,mc9,mc10,mc11,mc12,mc13,mc14);

//add child (or random mc) to the stage
addChild(Mcarray[newNumber]);

//place mc at specific starting point coordinate - i.e. top of the stage
Mcarray[newNumber].x=0
Mcarray[newNumber].y=0

//trace mc random numeric value for test purposes
trace(newNumber);

//auto-scroll the randomly chosen mc vertically down the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
function moveScroll(e:Event):void{
Mcarray[newNumber].y += scrollSpeed;

//once first mc is completley on stage load the next random mc

//once a mc has completely left the bottom of the stage return it to the library
}
}

Answers


Off the top of my head so it may be a bit rough but....

Start with the first clip and store it in an "onscreenClips" array (will be used like a queue):

1.) set the starting clip in "onscreenClips" to y = -height This would line up the bottom of the clip to the top of the stage.

Then within your enter frame loop:

1.) move any clips in "onscreenClips" down by the speed

2.) check if the first object in "onscreenClips" has reached the bottom yet (the y property would be equal to stage height). If so, remove it from display (as it would be offscreen now) and off the queue. The first object is always the 'oldest' in the queue.

3.) check if the last object in "onscreenClips" has reached the top of the stage yet (y property has reached 0 and is no longer negative). This would mean that the top edge is lined up with the top of the stage, if moved down any more, there would be a gap. If this happens, then add the next clip set at y = -height, then push it on to the queue.

4.) Continue until no more objects to add. Then keep checking the step 2 condition until the "onscreenClips" array/queue is empty.


Managed to progress a little further with this one.

Have added checks to determine when the full height of a mc is visible & 'on stage' and when the full height of a mc is non-visible & 'off stage' and both are functioning correctly.

I've also added a remove child statement, so that when the mc has become non-visible & offstage it is removed from the stage and returned to the library. This works in visual terms (i.e. the mc does disappear from the stage) however, according to the output results the mc (though not visible) still appears to be present & travelling ever further past the visible stage limit.


Need Your Help

operating over strings, python

python split tokenize

How to define a function that takes a string (sentence) and inserts an extra space after a period if the period is directly followed by a letter.

how to treat input as a generator

python input serial-port generator

I am reading lines from a serial connection (pyserial), at the moment I am using a while loop to read the line and then perform a series of functions on that input and then store it in an object ( a