Targetting certain duplicated clip

wow that is extremely amazing voets :thumb:!!! Thanks, just have a few quick quesitons like usual :stuck_out_tongue:

What is the point of this line:

_root[“box”+mcnr].onEnterFrame = null;

If i am correct, it sort of deletes the actions which are suppose to be in that function right? So then what tells flash to keep decreasing alpha from the previous box while it moves on to the next one?? thanks :slight_smile:

Ok, code explanation:

First, Flash defines the functions:

[AS]function continueFade(mc, speed) {…}
fadeMC(mcnr, speed) {…}
function fadeOut(startboxnr,speed) {…}[/AS]
After that, it creates the boxes:
[AS]
for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, “box”+i, i);
_root[“box”+i]._x = 17i;
}
[/AS]
The boxes will have the instance names box1,box2,box3,… until box20. And, the x position of box i equals 17
i. So it takes box i, and sets its _x value to 17*i. And that, it does for each box.

Then, it calls the function fadeOut with the vars 1 and 10, where one is the number of the box to start fading at and 10 is the speed of fading. A higher number will make them fade faster.
[AS]
fadeOut(1, 10);
[/AS]

Flash enters the function fadeOut, and calls the function fadeMC with the same vars (1 and 10).
[AS]
function fadeOut(startboxnr, speed) {
fadeMC(startboxnr, speed);
}[/AS]
Flash enters the function fadeMC with the values it got from fadeOut, which are the values fadeOut received.
[AS]
function fadeMC(mcnr, speed) {
[/AS]
_root[“box”+mcnr] is the path to the mc with the instance name “box”+mcnr, where mcnr is the value received from fadeOut.
So that box, here it’s still box1, since the var received is 1. So onEnterFrame box1 calls an anonymous function.
[AS]
_root[“box”+mcnr].onEnterFrame = function() {
[/AS]
It decreases the alpha value of box+mcnr (so still box1) with the value received ‘speed’. This is 10, since we assigned that value to it when we called fadeOut. This line is in the anonymous onEnterFrame function.
[AS]
_root[“box”+mcnr]._alpha -= speed;
[/AS]This is also in the onEnterFrame function. It checks if _alpha is equal or less than 50, and if so, it sets the onEnterFrame of the _root.box+mcnr (which is still box1 because mcnr is still 1) to null, which is basically the same as delete _root[“box”+mcnr].onEnterFrame.

Then it calls the function continueFade with the functions _root[“box”+mcnr"] (so the current box) and speed (still 10). Then it increases mcnr with one. While this is done, continueFade is running. I think you can figure that one out yourself, so I wont be explaining it. And then, it calls fadeOut with the values mcnr+1 and speed, which will do this whole explanation over only with _root.box2. And, it ends the anonymous onEnterFrame function.
[AS]
if (_root[“box”+mcnr]._alpha<=50) {
_root[“box”+mcnr].onEnterFrame = null;
continueFade(_root[“box”+mcnr], speed);
mcnr += 1;
fadeOut(mcnr, speed);
}
};
}
[/AS]

About the continuefade: the system actually works like this: it decreases the value of the currentbox (let’s take for example box4 as the currentbox) to 50, and then starts decreasing box5 to 50, but before it does that, it calls continuefade, which will make box4 fade out completely. And so, we have what you wanted :wink:

Also, notice: I always used _root[“box”+mcnr].onEnterFrame. I can’t really explain good, but this is what happens to the onEnterFrames, maybe that will give you an idea of what I mean.
Let’s say the currentbox is one.

_root.box1.onEnterFrame is created.
_root.box1.onEnterFrame is deleted (=null)
The above is the essential part: _root.box1.onEnterFrame is emptied. In continuefade, I redefine it because it has to continue fading, but in continuefade it has to be currentbox.onEnterFrame, otherwise another mc would fade. That’s why I always use _root[“box”+mcnr].onEnterFrame.

wow thanks for the detailed explanation :wink: Ok so there is actually another part to it :stuck_out_tongue: But i really wanna figure this part out myself. So i’m just asking a small bit of what i really want done. I’m not starting a new thread since i’ve got your attention here and i think you’d know :slight_smile: so here it is:

I have this counter which adds from 1 to 100. And i need it so every 5 (such as 5, 10, 15, 20 etc), it does something. You can do if statements, but that would take ages. So i was wondering if there was a shorter way :slight_smile: thanks

Well, that depends on how you set up the script that counts from 1 to 100. If you place

[AS]if(value%5 == 0){
dosomething
}[/AS]

inside that script, it might work, depending on the setup of the script.

The following is an example:
[AS]
value = 0;
function inCaC() {
if (value<100) {
clearInterval(Plus);
value++;
trace(value);
if (value%5 == 0) {
trace(“Something”);
//do anything here
}
Plus = setInterval(inCaC, 1000);
} else {
clearInterval(Plus);
}
}
Plus = setInterval(inCaC, 1000);[/AS]

thanks voets, that works great just another quick question! is it possible to make a function create several others dynamically? Since the content of one function will always be changing, i need lots of functions. And it would be a pain to make each function one by one, so is there a way to do it dynamically? thanks!

When i think about it, i realize that its pretty much no possible :-
So i did it the longer way. But my code is pretty long, so is it possible to shorten this up?

[AS]
if (functionChoice == 1 || functionChoice == 6 || functionChoice == 11 || functionChoice == 16) {
[/AS]
thanks

It’s very hard for me to tell with only that piece of code. It depends on how you’ve set up your functions. I know you want to solve this by yourself, but can you please post the entire code ?

yep sure, i’ve got it working now. But it is VERY long…This was the effect i was tryin to make since the start :stuck_out_tongue: Just tryin to shorten it now because it’d be easier to load for slow connections right?

[AS]
for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, “box”+i, i);
_root[“box”+i]._x = 17i;
}
counter = 0;
function functionTimings() {
if (counter<100) {
clearInterval(Plus);
counter++;
if (counter%5 == 0) {
boxDecision();
functionChoice++;
}
Plus = setInterval(functionTimings, 50);
} else {
clearInterval(Plus);
}
}
Plus = setInterval(functionTimings, 50);
/BELOW IS FUNCTION TO CHOOSE WHICH BOX TO GO HAVE CODE EXECUTED THROUGH
/
functionChoice = 1;
gravity = 2;
floor = 100;
bounce = 0.5;
speedy = 20;
boxnum1 = -4;
boxnum2 = -3;
boxnum3 = -2;
boxnum4 = -1;
boxnum5 = 0;
function boxDecision() {
if (functionChoice == 1 || functionChoice == 6 || functionChoice == 11 || functionChoice == 16) {
boxnum1 += 5;
bouncer1(boxnum1, gravity, floor, bounce, speedy);
} else if (functionChoice == 2) {
boxnum2 += 5;
bouncer2(2, gravity, floor, bounce, speedy);
} else if (functionChoice == 3) {
boxnum3 += 5;
bouncer3(3, gravity, floor, bounce, speedy);
} else if (functionChoice == 4) {
boxnum4 += 5;
bouncer4(4, gravity, floor, bounce, speedy);
} else if (functionChoice == 5) {
boxnum5 += 5;
bouncer5(5, gravity, floor, bounce, speedy);
}
}
/END/
///START BOUNCER FUNCTIONS-To be able to quickly process functions for each box
///*************************
//*****************//
function bouncer1(boxnum1, gravity, floor, bounce, speedy) {
trace(“bouncer1 executed”);
_root[“box”+boxnum1].onEnterFrame = function() {
speedy += gravity;
_root[“box”+boxnum1]._y += speedy/5;
if (_root[“box”+boxnum1]._y>floor) {
_root[“box”+boxnum1]._y = floor;
speedy *= -bounce;
}
};
}
function bouncer2(boxnum2, gravity, floor, bounce, speedy) {
_root[“box”+boxnum2].onEnterFrame = function() {
speedy += gravity;
_root[“box”+boxnum2]._y += speedy/5;
if (_root[“box”+boxnum2]._y>floor) {
_root[“box”+boxnum2]._y = floor;
speedy *= -bounce;
}
};
}
function bouncer3(boxnum3, gravity, floor, bounce, speedy) {
_root[“box”+boxnum3].onEnterFrame = function() {
speedy += gravity;
_root[“box”+boxnum3]._y += speedy/5;
if (_root[“box”+boxnum3]._y>floor) {
_root[“box”+boxnum3]._y = floor;
speedy *= -bounce;
}
};
}
function bouncer4(boxnum4, gravity, floor, bounce, speedy) {
_root[“box”+boxnum4].onEnterFrame = function() {
speedy += gravity;
_root[“box”+boxnum4]._y += speedy/5;
if (_root[“box”+boxnum4]._y>floor) {
_root[“box”+boxnum4]._y = floor;
speedy *= -bounce;
}
};
}
function bouncer5(boxnum5, gravity, floor, bounce, speedy) {
_root[“box”+boxnum5].onEnterFrame = function() {
speedy += gravity;
_root[“box”+boxnum5]._y += speedy/5;
if (_root[“box”+boxnum5]._y>floor) {
_root[“box”+boxnum5]._y = floor;
speedy *= -bounce;
}
};
}
[/AS]

well here it is. Long eh :stuck_out_tongue:

EDIT: i just needa add the line i had in the previous post to each of the IF’s, then it’d be fine. But then it’d be longer than it already is so just askin if you can shorten it :wink: and ignore the comments i know they’re hard to understand :slight_smile:

So you want to have box1 start to drop and after 50 ms you want the following to start droppping, and after 50 msecs the following, and so on … right ?

yep thats right :slight_smile: so can my code be shortened by mucH?

Yes. But, why did you ask for the %5 thingy ? I’m confused about that.

Oh the counter thing? so i can control the rate of the thing, and its gonna sorta be a preloader so later when i change the code for a preloader it would be easy :stuck_out_tongue:

I think this is what you wanted. The Interval indicates the time before 1 is addded to counter. So if the interval would be set to 1000 (= 1 second), it would take 5 seconds for the next box to start droppping.

By the way, you can create really cool effects with this code ! Try playing around with += and -= in the function dropBox and see what the effect is ! I’ve already tried out tons of cool ones !

Don’t forget to set the framerate quite high, like 40 or 50.

*Oh, also: the amount of time between two boxes dropping is also the amount of time before the first one starts dropping, plus the milliseconds of the initial interval

[AS]
for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, “box”+i, i);
_root[“box”+i]._x = 17*i;
}
counter = 0;
boxnr = 1;
gravity = 2;
floor = 100;
bounce = 0.5;
speedy = 20;
function dropBox(nr, speedy, gravity, bounce, floor) {
_root[“box”+nr].onEnterFrame = function() {
speedy -= gravity;
_root[“box”+nr]._y -= speedy/5;
if (_root[“box”+nr]._y>floor) {
_root[“box”+nr]._y = floor;
speedy *= -bounce;
if(speedy<1){
delete _root[“box”+nr].onEnterFrame
}
}
};
}
function timing() {
if (counter<100) {
clearInterval(Plus);
counter++;
if (counter%5 == 0){
dropBox(boxnr, speedy, gravity, bounce, floor);
boxnr++;
}
Plus = setInterval(timing, 50);
} else {
clearInterval(Plus);
}
}
Plus = setInterval(timing, 50);
[/AS]

pretty much :slight_smile: but it doens’t get the same bounce i was lookin for :stuck_out_tongue: Nice though, thanks :thumb:

Oh right, I was trying out some other effects but I forgot to change it back to the original. To do that, replace

[AS]
speedy -= gravity;
_root[“box”+nr]._y -= speedy/5;
[/AS]

with

[AS]
speedy += gravity;
_root[“box”+nr]._y += speedy/5;
[/AS]

But I think the one it has now is nicer :stuck_out_tongue:

now that you’ve said that, i think so too :stuck_out_tongue:

ok now that i’ve read your code, something confuses me. If you change boxnr every 15ms, then won’t it stop the other boxes from bouncing?

No, because the function uses different onEnterFrames for each box. Each box’s onEnterFrame that is. Look at the code:

[AS]
function dropBox(nr, speedy, gravity, bounce, floor) {
_root[“box”+nr].onEnterFrame = function() {

};
}
[/AS]

See ? It takes the boxnr it receives (nr) and defines the onEnterFrame for that box. When the boxnr increases it defines the onEnterFrame for the following box. And so on …

ahh yes thats right :stuck_out_tongue: