PDA

View Full Version : loop and time


spyny
09-05-2003, 11:39 AM
Id like to duplicate a MC many times, but not at the same time, i want to delay the duplication of some.

What i thought about the "wait" command, but i want to do something clean.

Danke schone

webguy
09-05-2003, 12:02 PM
hello spyny.. use an interval


// make a levelCount variable so newly created clips don't replace previous clips
levelCount=1;

// make a function that will create a new clip each time it is called
function makeADot () {

// set up some random X and Y Positions based on the size of the movie
randX = Math.random()*Stage.width;
randY = Math.random()*Stage.height;

// make a new empty movieClip so it can be drawn in
newDot = _root.createEmptyMovieClip("dot"+levelCount,levelCount++);

// put it in its random position
newDot._y = randY;
newDot._x = randX;

// use the drawing API to make a dot of sorts :D
newDot.lineStyle(20);
newDot.lineTo(.1,.2);
}

// set up an interval with the variable name makeADotID that executes the function every second, or 1000ms to be exact
makeADotID = setInterval(makeADot,1000);

// when the mouse is pressed anywhere stop the interval so no more clips are made
_root.onMouseDown = function () {
clearInterval(makeADotID);
}


Put this code on frame one of an empty movie and test.

web

spyny
09-07-2003, 06:29 PM
hello,

thanks for the help.

I changed the code to make something like this:

http://spyny.free.fr/green_square.fla

(16k)

This is incomplete and what i want to do, is to make green squares appear randomly at different fixed position (dunno if my english makes sense).
The thing i think i cannot manage is that each new square never appears on an already generated position, so that it doesn't cover another square and gives the impression nothing is happening.

I added and modified the script someone typed in another forum to do the that but it seems not to work.

[AS]
numberList = [];
for (var i = 1; i<15; ++i) {
numberList.push(i);
}
function makeADot() {
index = Math.ceil(Math.random()*numberList.length);
// add the value to the randomSelection array
numberList.splice(index, 1);
// remove the selected value from numebrList array so it can't be picked twice
// set up some random X and Y Positions based on the size of the movie
randX = 2+32*index;
randY = 30;
// make a new empty movieClip so it can be drawn in
newDot = _root.createEmptyMovieClip("Dot"+index, index++);
// put it in its random position
newDot._x = randX;
newDot._y = randY;
newDot.attachMovie("sq", "sq"+index, index++);
}
// set up an interval with the variable name makeADotID that executes the function every second, or 1000ms to be exact
makeADotID = setInterval(makeADot, 200);
[AS]

Thanks for any help

webguy
09-08-2003, 12:46 PM
ok.. so do you want the boxes to cover the entire stage? or do you just want them to tile across the top? If they are going to be in set positions why are you creating their positions randomly? Tell me exactly what you are trying to achieve and I am sure I will be able to help you further. I don't think the way you are using now is the most effecient.

web

spyny
09-08-2003, 03:52 PM
i want the boxes to tile across thetop, but each boxes are separated by space of 2 pixels.

It means that each box is 30 px wide and each position is 34 px away from the last. I hope i make sense...

Well, it's like i have a grid (of one row only) and i want boxes to fill it but in a random order, that is not from the first left cell to the last cell, one after the other.

that's it.

webguy
09-08-2003, 04:15 PM
see now that is much easier :) The way I am doing it is dynamic, so you can have as many boxes as you want, but you can do it simply by putting them on the stage and then adding them to an array and run through the array with an interval like I did.


// set up a levelCounter
levelCount=1;

// a variable for spacing between clips
var spacing = 2;

// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));

// set up a new array to hold the boxes that are created
var boxArray = new Array();

// create the boxes across the stage
for(var i=0;i<boxNumber;i++) {
var newBox = box.duplicateMovieClip("box"+levelCount,levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}

// hide the original
box._visible = false;

// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
rand = Math.floor(Math.random()*boxArray.length);
boxArray[rand]._visible = true;
boxArray.splice(rand,1);
if(boxArray.length==0) {
clearInterval(ID);
}
trace(rand);
}

// setup an interval
ID = setInterval(showBox,100);


demo attached.

web

spyny
09-09-2003, 03:31 AM
this is neat, but what if i want to move each box in a different way (speed, direction, etc.)?

webguy
09-09-2003, 10:01 AM
what if what? You can do whatever you want to these instances..they are movie clips.. if you want to move them tell them to move, if you want to fade them tell them to fade, if you want them to bounce around and explode in a flurry of chickens.. use actionscript and tell them to.

spyny
09-09-2003, 05:51 PM
yeah, you're rite.
As im a noobie and not very logical, i wasn't sure of that. It seems simple.

Now, how can i make all of them move (at once each is visible - i mean they'll start to move one after the other), but once again at a random speed?
do i just have to make a loop?
i'm sorry, the process is still not obvious to me.

thanks for support!

webguy
09-11-2003, 01:29 PM
Now, how can i make all of them move (at once each is visible - i mean they'll start to move one after the other), but once again at a random speed?


The exact same way I did in my code. By looping through the array and doing whatever you want to them. Here is a simple way to make them move in erradic directions


function showBox() {
rand = Math.floor(Math.random()*boxArray.length);
randY = Math.random()*30-15;
randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray.splice(rand,1);
boxArray.onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
if(boxArray.length==0) {
clearInterval(ID);
}
}


So now when this function gets called by the intervail, it will make the boxArray element visible and assign an onEnterFrame event that causes the box to move off stage in random X&Y directions.

Don't worry you will start to understand actionscript after you mess with it for a while. Just don't take none of it's sh*t & tell it who the master is a stern voice every couple hours for the first couple months.

web

spyny
09-14-2003, 07:50 AM
web,

i tried your script in different ways but i couldn't make it work (i mean the "make the boxes move randomly" part).

[AS]// set up a levelCounter
levelCount=1;

// a variable for spacing between clips
var spacing = 2;

// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));

// set up a new array to hold the boxes that are created
var boxArray = new Array();

// create the boxes across the stage
for(var i=0;i<boxNumber;i++) {
var newBox = box.duplicateMovieClip("box"+levelCount,levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}

// hide the original
box._visible = false;

// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
rand = Math.floor(Math.random()*boxArray.length);
randY = Math.random()*30-15;
randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray.splice(rand,1);
boxArray.onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
if(boxArray.length==0) {
clearInterval(ID);
}
}

// setup an interval
ID = setInterval(showBox,100);[AS]

The script works correctly but the boxes don't move at all.

What should i change?

spy

spyny
09-14-2003, 07:52 AM
// // set up a levelCounter
levelCount=1;

// a variable for spacing between clips
var spacing = 2;

// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));

// set up a new array to hold the boxes that are created
var boxArray = new Array();

// create the boxes across the stage
for(var i=0;i<boxNumber;i++) {
var newBox = box.duplicateMovieClip("box"+levelCount,levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}

// hide the original
box._visible = false;

// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
rand = Math.floor(Math.random()*boxArray.length);
randY = Math.random()*30-15;
randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray.splice(rand,1);
boxArray.onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
if(boxArray.length==0) {
clearInterval(ID);
}
}

// setup an interval
showBoxID = setInterval(showBox,100);

webguy
09-15-2003, 11:55 AM
boxArray[rand].onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}


:p

web

spyny
09-15-2003, 02:24 PM
that's what's in the script i pasted rite?

but then the boxes do not move. What do i have to do?

webguy
09-15-2003, 02:28 PM
no that is not what is pasted above. here is a brief explaination of where I funked up.


function showBox() {
// here I set up an random number
rand = Math.floor(Math.random()*boxArray.length);
randY = Math.random()*30-15;
randX = Math.random()*30-15;
// here I use that random number to
// make the clip's visibility true
boxArray[rand]._visible = true;
// here I am messing with the entire Array by just referencing boxArray
boxArray.splice(rand,1);
// here it seems like I am puttin an onEnterFrame event on an array
// which is just not possible
// so changing it to reference the clip in the array makes it effect that clip.
boxArray.onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
if(boxArray.length==0) {
clearInterval(ID);
}
}



//so change this code
boxArray.onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
// to this
boxArray[rand].onEnterFrame = function () {
this._y+=randY;
this._x+=randX;
}
// and it should twerk

I didn' test this code, but I am looking at it and i don't see anything wrong, other than what I pointed out. Let me know if you still can't get it working and I will make up a little demo.

web

spyny
09-15-2003, 05:39 PM
With what i tried, i get a FEW of the boxes to move, but not all of them (let's say half of them), dunno why.

Then, the problem is that they all move in exactly the same way.

Is there a way to make each of them move in a different way?
I suppose this needs again to use a loop.

merci beaucoup!

//// set up a levelCounter
levelCount = 1;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
rand = Math.floor(Math.random()*boxArray.length);
randY = Math.random()*30-15;
randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray.splice(rand, 1);
boxArray[rand].onEnterFrame = function() {
this._y += randY;
this._x += randX;
};
if (boxArray.length == 0) {
clearInterval(ID);
}
}
// setup an interval
showBoxID = setInterval(showBox, 100);

webguy
09-15-2003, 05:51 PM
well the main reason is because I am an idiot and should have tested it before I posted it. If you noticed a few things in testing my previous code, each instance would not move randomly, rather they would use the same random X and Y numbers that were generated, because these numbers were not being destroyed when the function was done, they were being updated. And if you also noticed the interval never stopped.. Because I had the interval name wrong in clearInterval, and (geez there are alot of ands in here) I was splicing the element out of the array prior to using it. So when I went to use it I was actually using the next element in line, because that one was being removed to soon.

Now try this code (I tested it so I know it works :) )


//// set up a levelCounter
levelCount = 1;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
var rand = Math.floor(Math.random()*boxArray.length);
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray[rand].onEnterFrame = function() {
this._y += randY;
this._x += randX;
};
if (boxArray.length == 0) {
clearInterval(showBoxID);
}
boxArray.splice(rand, 1);
}
// setup an interval
showBoxID = setInterval(showBox, 100);


web

spyny
09-15-2003, 06:22 PM
fanx u belly much!

when u have time maybe u can help me dig deeper...

Now, id like to make it different :
suppose i make each box disappear from the stage. I'd like to allow a new box to appear at the position where a box that's just gone out of the stage, appeared.

I hope this is understandable.

Then the generation of boxes is endless, this time, because each time a box disappear, a new box can appear from "there"..it's so difficult to put in word.

I suppose i have to // myArray.push again the position in the boxArray. I'll try that.

webguy
09-16-2003, 10:34 AM
if you want it to continuesly occur, the best way is to set up an interval that picks a random one, duplicates it and assigns the random movement code that is below. This way the original never moves, just duplicates itself and assigns an onEnterFrame event to the duplicate.

Kinda understand? you could use the code that you already have.. omit the parts where it makes the clip invisible, where it assigns the random movement onEnterFrame, and the interval declaration.

ahh, it's just some simple changes to the previous code.. here ya go :)


//// set up a levelCounter
levelCount = 1;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
//newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
var rand = Math.floor(Math.random()*boxArray.length);
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
newBox = boxArray[rand].duplicateMovieClip("dup"+levelCount,levelCount++);
newBox.onEnterFrame = function() {
this._y += randY;
this._x += randX;

// make them invisible and remove it when it is no longer visible
// this is necessary so that you don't have 1000 movieClips off stage
// bogging out the CPU
this._alpha-=1;
if(this._alpha<0) this.removeMovieClip();
};
//if (boxArray.length == 0) {
// clearInterval(showBoxID);
//}
//boxArray.splice(rand, 1);
}
// setup an interval
showBoxID = setInterval(showBox, 500);


web

spyny
09-16-2003, 11:15 AM
1/ is it possible not to make the boxes to fade out and make them invisible as soon as they are outside the stage?

in that way:

//if (this._x<0) {
this.removeMovieClip ();
} else if (this.x>500) {
this.removeMovieClip ();
}

2/ i tried to change the code so that it looks like the matrix code and it seems that a new box can appear at an x position where an older box appeared and is still within the stage, which i would like to prevent.

You can check:

//// set up a levelCounter
levelCount = 1;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
//newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
var rand = Math.floor(Math.random()*boxArray.length);
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
newBox = boxArray[rand].duplicateMovieClip("dup"+levelCount,levelCount++);
newBox.onEnterFrame = function() {
//here i change the variable to a constant to make it matrix-like...hehe:
this._y += 15;
// this._x -= 4;

// make them invisible and remove it when it is no longer visible
// this is necessary so that you don't have 1000 movieClips off stage
// bogging out the CPU
this._alpha-=1;
if(this._alpha<0) this.removeMovieClip();
};
//if (boxArray.length == 0) {
// clearInterval(showBoxID);
//}
//boxArray.splice(rand, 1);
}
// setup an interval
showBoxID = setInterval(showBox, 100);


:cool:

webguy
09-16-2003, 11:28 AM
your learning pretty quick :)

Yes you can remove them if thier bounds are outside of the stage.


if(this._x < 0 || this._x > Stage.width || this._y < 0 || this._y > Stage.height) {
this.removeMovieClip();
}


web

spyny
09-16-2003, 11:39 AM
"[...] it seems that a new box can appear at an x position where an older box appeared and is still within the stage, which i would like to prevent."

webguy
09-18-2003, 10:16 AM
That is because my code is only working in one dimensions (_x) you will need to modify the code to work along y as well. Give it a try, I'm sure you can figure it out.

spyny
09-21-2003, 06:00 PM
hey,

i tried one thing to push the _x position back into the Array in order to let a new box to appear at that position.

//if (this._y>100) {
this._y = 0;
boxArray.push(this._x);
}

It doesn't work, and actually i dont have an idea, i'm lost, cause i don't know really how everything works.

boxArray works in the loop typed before the function, but then...

here's the complete script:

//
//// set up a levelCounter
levelCount = 1;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
var rand = Math.floor(Math.random()*boxArray.length);
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
boxArray[rand]._visible = true;
boxArray[rand].onEnterFrame = function() {
this._y += 20;
//this._x += randX;
};
if (this._y>100) {
this._y = 0;
boxArray.push(this._x);
}
if (boxArray.length == 0) {
clearInterval(showBoxID);
}
boxArray.splice(rand, 1);
}
// setup an interval
showBoxID = setInterval(showBox, 10);

webguy
09-22-2003, 10:35 AM
here is a much longer explaination of the code:


// Long Explaination :)
// Setup a levelCount variable in order to increment the level that each
// new clip is placed on. This keeps clips from getting replaced.
// check out depth and levels for more information
levelCount = 1;
// a variable for spacing between clips. This is used in the for loop
// to properly space the clips a set distance from one another
var spacing = 2;
// this is a simple equation that determines how many boxes it takes to cover the
// entire Stage, taking the spacing variable into account. This number will be used
// in the for loop below, so that exactly enough boxes are created to cover the Stage.
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// Now I am creating a new array object that will hold the instance
// name for all of the boxes created in the for loop below.
var boxArray = new Array();
// now I am running a for loop that will run the number determined
// by the BoxNumber equation.
for (var i = 0; i<boxNumber; i++) {
// here I am creating a duplicate of the box instance that is on stage
// and assigning its instance location (ex. _root.box12) to the variable
// newBox. I can then use the variable newBox to refer to the newly created clip
// this just mearly saves code. I could just as easily have put _root["box"+levelCount-1]
// which is a dynamic reference to the location of the newly created instance
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
// now I am assigning the newly created box to its predetermined position by using this equation
// the newBoxs width + the spacing variable will equal the entire width of each block of our grid.
// I then take that value and multiply it by the iteneration number we are on. If i=0 then it's _x
// position will equal that of the clip used for duplicating. If i=1 then it will be placed
// in the next space to the right
newBox._x = (newBox._width+spacing)*i;
// then I turn off it's visibility to so it can not be seen until we activate it's onEnterFrame
// event using the function showBox below
newBox._visible = false;
// Then I push a reference to the instance into the boxArray array for later use. I do this so that
// I don't have to access the instances directly I only have to reference their index in the boxArray.
// like boxArray[1]._xscale=50; that will make the second element in the array shrink to 50% of its
// original height
boxArray.push(newBox);
}
// Simply hide the original so that it is not underneath the first one, looking like a double duplicate
box._visible = false;

// this is the showBox function. This function has a simple purpose, take a random element from the
// boxArray array and make it visible. Then assign some actions to its onEnterFrame event.
function showBox() {
// here I am randomly pulling a number between 0 and the amount of elements in the boxArray array.
// I do this so that I don't pull a number to an element that does not exist in the array.
// if there are only 10 elements in the array boxArray[15]._xscale = 50; will do nothing because
// the boxArray array has nothing in the 15th index position.
var rand = Math.floor(Math.random()*boxArray.length);
// below I randomly generate numbers between -15 and 15 to be used in the onEnterFrame event below. These number will
// be used to determine the 'speed' that each block moves per frame. This equation works like this.
// a random decimal is generated betwen 0 and 1 and then multiplied by 30. This gives a range of 0-30. Then
// I subtract 15 from it to alter the range. So if the number generated by Math.random()*30; = 29 then I subtract
// 15 from that and get 14, if it was 12 then I would get -3, etc.
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
// here I use the variable rand value to access the instance from the boxArray array. I simply
// make it visible here. So if rand = 3 it is the same as boxArray[3]._visible = true;
boxArray[rand]._visible = true;
// here I use the same instance and assign a function to it's onEnterFrame event. This function
// will execute with each passing frame, giving that the instance and function exist.
boxArray[rand].onEnterFrame = function() {
// with each passing frame this particular instance will move move down 20 pixels.
// it will never stop.. not as long as the movie is playing. Even though an instance
// is not on stage, its events are still executing.
// so if this movie ran for 60 minutes and this instance started at _y=0 at 12 fps it
// would be around _y=864,000 and still going... so be sure to remove clips that are not needed.
this._y += 20;
//this._x += randX;
};
// I'll explain what you are actually doing here. Since you are referencing this inside of the scope of this function
// this is actually refering to the timeline that this function is written on. so this._y actually means
// _root._y and since _root._y will never == 100, unless you assign it of course, nothing will get added
// to the boxArray especially not _root._x :)
if (this._y>100) {
this._y = 0;
boxArray.push(this._x);
}
// here I am running a conditional that says simply if boxArray contains no elements
// then we have run through the entire array and can stopour showBoxID interval
// no sense calling a function that fails to do anything. showBox function will
// not work if an element is not present in the boxArray array.
if (boxArray.length == 0) {
// here I clear the interval so that it no longer executes
clearInterval(showBoxID);
}
// here I am removing the reference to the instance so that it can no longer be called from this function. Doing
// this has positive effects on our script. splicing out an element from the array shortens the array.length()
// by one. Then the next time this function is run, our showBox function will have less elements to work with. Eventually
// scaling down to none where the showBoxID interval is cleared
boxArray.splice(rand, 1);
}
// here I simply create an interval that will be executed every 10 milliseconds. I personally would not
// use 10 milliseconds as that would be executed 100 times per second, and since your movie is moving around
// 24 fps, this interval will execute approximately 4 times per frame. So with each passing frame you will
// have 4 newly visible, animated boxes.
showBoxID = setInterval(showBox, 10);

spyny
11-13-2003, 11:40 AM
hello again!

i tried something inspired by webguy's code. I basically want to "slowdown" the animation but the script i made freezes the program after a while.

I don't figure out why, so if anyone could have a look...
You can DL the fla and look now at the code :


// set up a levelCounter
levelCount = 0;
// a variable for spacing between clips
var spacing = 2;
// find out how many boxes it will take to go across the stage
var boxNumber = Math.ceil(Stage.width/(box._width+spacing));
// set up a new array to hold the boxes that are created
var boxArray = new Array();
// create the boxes across the stage
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
//newBox._visible = false;
boxArray.push(newBox);
}
// hide the original
box._visible = false;
//q = axel factor
q = 1;
// setup an interval
showBoxID = setInterval(showBox, 1000);
// show box function each time it is called it will pull a random box from boxArray and make it visible
// then remove it's reference from the array.
function showBox() {
//slowdown the animation
if (q<8) {
q = q+1;
showBoxID = setInterval(showBox, 1000-100*q);
} else {
showBoxID = setInterval(showBox, 100);
}
//
var rand = Math.floor(Math.random()*boxArray.length);
var randY = Math.random()*30-15;
var randX = Math.random()*30-15;
newBox = boxArray[rand].duplicateMovieClip("dup"+levelCount, levelCount++);
newBox.onEnterFrame = function() {
this._y += 7;
// make them invisible and remove it when it is no longer visible
// this is necessary so that you don't have 1000 movieClips off stage
// bogging out the CPU
if (this._y>=200) {
this.removeMovieClip();
}
};
if (boxArray.length == 0) {
for (var i = 0; i<boxNumber; i++) {
var newBox = box.duplicateMovieClip("box"+levelCount, levelCount++);
newBox._x = (newBox._width+spacing)*i;
//newBox._visible = false;
boxArray.push(newBox);
}
}
boxArray.splice(rand, 1);
}

webguy
11-17-2003, 06:38 PM
Because you are not clearing the previous interval before creating a new one. When you create an interval (ie: ID = setInterval(function,frequency,arg) ), you are creating a variable called ID that contains the 'name' of the interval so that flash player knows what to shutdown when you ask it to clearInterval. Overwriting the variable does not clear or kill the previous interval, only overwrites the name association with the name of the new interval. If you trace an interval variable you will usually get (1 or 2 or 3 or n) which is the actual 'name' of the interval. Simply adding clearInterval(showBoxID); before your reset it should alleviate your problem.

webG

spyny
11-27-2003, 03:18 PM
waw, cool the new design.

Thanks again to webguy from the newbie