PDA

View Full Version : Targetting certain duplicated clip...



blah-de-blah
August 19th, 2003, 09:38 AM
Ok, 3rd thread today....so you can see i'm not having a very smart day :P Anyways, this is a bitta code i got:


amount = 1;
i = 1;
while (amount<20) {
duplicateMovieClip(_root.box, "box"+i, i);
}


With that done, you can now do something like:

_root["box"+i].onRelease or whatever you want am i right??

So with that said, that targets all the duplicated clips. So is it possible to just target certain ones? like the 3rd, 9th etc? Thanks!!

Since the duplicated boxes should be called box1, box2 etc, I have tried something like:

_root.box1.onRelease.....

but no luck either :-\

thoriphes
August 19th, 2003, 10:31 AM
yeah....just use an if statement:
...
if (i == 3 || i == 9 || i == 12) {
_root["box"+i].onRelease = function () {...};
}
...
Also, I hope that's not your code, because that's an infinite loop you got there (the value of amount doesn't change within the loop).

blah-de-blah
August 19th, 2003, 10:35 AM
nope thats not all my code :P Just to make sure...the || checks to see if any 3 of those are true right? Anyways thanks, i'll go test it out now :)

Voetsjoeba
August 19th, 2003, 10:46 AM
Yeah Thoriel is right. Use a for loop instead

for (i=1;i<=20;i++) {
duplicateMovieClip(_root.box, "box"+i, i);
mc = _root["box"+i]
mc.onRelease = function (){...} /* mc targets all duplicated movieclips */
}
_root["box"+3].onRelease = function (){...}/* _root["box"+3] targets the third duplicated movieclip */


The following code is an example. You'll see ;)

for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
mc = _root["box"+i];
mc._x = i*40
}
_root["box"+3]._visible = false;

blah-de-blah
August 19th, 2003, 10:54 AM
Thanks for the help so far...ok so this is what i have:


for (i=1; i<20; i++) {
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
_root["box"+3] = fadeOut();


And i expect box3 to execute function "fadeOut" (which is a gradual alpha fade). I can't see whats wrong though cause its not working?? :-\

Voetsjoeba
August 19th, 2003, 10:58 AM
Please post the function fadeOut();

blah-de-blah
August 19th, 2003, 11:00 AM
Sure :)


for (i=1; i<20; i++) {
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
_root["box"+3] = fadeOut();
alphaspeed = 20;
MovieClip.prototype.fadeOut = function() {
this.onEnterFrame = function() {
this._alpha -= alphaspeed;
if (this._alpha<=0) {
delete this.onEnterFrame;
}
};
};

Voetsjoeba
August 19th, 2003, 11:13 AM
function fadeOut(boxnr, alphaspeed) {
_root.onEnterFrame = function() {
_root["box"+boxnr]._alpha -= alphaspeed;
if (_root["box"+boxnr]._alpha<=0) {
delete this.onEnterFrame;
}
};
}
/*MovieClip.prototype.fadeOut = function(alphaspeed) {
this.onEnterFrame = function() {
this._alpha -= alphaspeed;
if (this._alpha<=0) {
delete this.onEnterFrame;
}
};
};*/
/* Movieclip prototypes apply on all movieclips, so all boxes. Instead, I created a fadeOut function*/
for (i=1; i<20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
alphaspeed = 10;
fadeOut(3, alphaspeed);

blah-de-blah
August 19th, 2003, 11:18 AM
wow that is really good voetsjoeba thanks!! Just a quick question since i'm not too familiar with functions...

What do the things in brackets mean??

function fadeOut(boxnr, alphaspeed) {

such as 'boxnr' and 'alphaspeed'. Why do you need to put them in there? thanks again!!

Voetsjoeba
August 19th, 2003, 11:22 AM
function fadeOut(boxnr, alphaspeed) {
_root.onEnterFrame = function() {
_root["box"+boxnr]._alpha -= alphaspeed;
if (_root["box"+boxnr]._alpha<=0) {
delete this.onEnterFrame;
}
};
}


'boxnr' and 'alphaspeed' are variables that the function uses. When you call the function:

fadeOut(3, alphaspeed);

you need to assign values to those vars for the function to use.

blah-de-blah
August 19th, 2003, 11:23 AM
ooh ya i pretty much understand that now after reading the code over again a few times. thanks for like the 100th time :P

Voetsjoeba
August 19th, 2003, 11:24 AM
You're welcome ;)

blah-de-blah
August 19th, 2003, 12:02 PM
Ok sorry to bother you again voetsjoeba, i thoght i could handle the next part, but i figured i couldn't :-\

Ok so i'm trying to use setInterval to keep repeating the function. This is what i've tried...


Interval = setInterval(fadeOut(boxnum, alphaspeed), 1000);


but that doesn't work. I did boxnum++ on the code you gave me before so it will fade each box 1 by 1.

I was wondering how you can do this? Turned out harder than i thought :-\

Voetsjoeba
August 19th, 2003, 12:06 PM
I'd love to help you, but I have to get off this computer now. But ! I'll continue upstairs, when I get back here I'll post the code :)

blah-de-blah
August 19th, 2003, 12:09 PM
I'll be waiting ;)

In the meantime, i must tell you that i also tried this:

function fadeFunction() {
fadeOut(boxnum, alphaspeed)
}
Interval = setInterval(fadeFunction, 1000);


But no luck with that either :-\

Voetsjoeba
August 19th, 2003, 01:35 PM
function fadeOut(startboxnr) {
clearInterval(Interval);
_root.onEnterFrame = function() {
_root["box"+startboxnr]._alpha -= 10;
if (_root["box"+startboxnr]._alpha<=0) {
startboxnr++;
Interval = setInterval(fadeOut, 500, startboxnr);
delete this.onEnterFrame;
}
};
}
for (i=1; i<20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
//Set the initial interval (= the time before it starts fading)
Interval = setInterval(fadeOut, 1000, 0);


This will make the next box start to fade after the one that is currently fading has finished fading. Here, setInterval indicates the time between one has finished fading and another one starts. If you want them to fade right after eachother, set 500 to 1.

If you want to start them fading immediately, change the 1000 from the inital interval to 1.

At first, you won't notice they are fading but they are. That's because it decreases with 10. 100-10 = 90, so still very good visible. Around about 60-70 you notice it fading.

And, to make them fade more quickly, increase 10 in ["box"+startboxnr]._alpha -= 10;

blah-de-blah
August 19th, 2003, 11:59 PM
wow thats is good, thx! Just wondering, why do you havta clearInterval each time??

Voetsjoeba
August 20th, 2003, 02:52 AM
You don't. It'll work without clearInterval too, but I prefer to do it this way :)

blah-de-blah
August 20th, 2003, 10:10 AM
ok thanks, everything is great, but to be more specific to my problem (which i tried to work on more from your code), i sort of want the next box to start fading when the first box's alpha is on 50. I have got this:


this.onEnterFrame = function() {
Interval = setInterval(fadeOut, 300, startboxnr);
trace("INTERVAL");
startboxnr++;
};
startboxnr = 1;
MovieClip.prototype.fadeOut = function(startboxnr) {
_root["box"+startboxnr]._alpha -= 10;
if (_root["box"+startboxnr]._alpha<=50) {
_root["box"+startboxnr]._alpha -= 10;
//startboxnr++;
if (startboxnr>20) {
startboxnr = 1;
}
trace(startboxnr);
}
};
for (i=1; i<20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}


As you can see, that decreases 10 from each box before it goes back to the first one. I've tried many times but this is the closest i can get. So i was wondering how i could change this code so it decreases 10 from the first box until the alpha hits 0. Then, while it is decreasing alpha from the first box, it begins the second. Thanks. You don't havta help me if you don't want :P

EDIT: oh yea btw, if you have an idea on this but dont' have the time to code it that would help me too thanks :)

Voetsjoeba
August 20th, 2003, 02:08 PM
ok thanks, everything is great, but to be more specific to my problem (which i tried to work on more from your code), i sort of want the next box to start fading when the first box's alpha is on 50. I have got this:


This should do it ;)


function continueFade(mc, speed) {
mc.onEnterFrame = function() {
mc._alpha -= speed;
if (mc._alpha<=0) {
delete mc.onEnterFrame;
}
};
}
function fadeMC(mcnr, speed) {
_root["box"+mcnr].onEnterFrame = function() {
_root["box"+mcnr]._alpha -= speed;
if (_root["box"+mcnr]._alpha<=50) {
_root["box"+mcnr].onEnterFrame = null;
continueFade(_root["box"+mcnr], speed);
mcnr += 1;
fadeOut(mcnr, speed);
}
};
}
function fadeOut(startboxnr, speed) {
fadeMC(startboxnr, speed);
}
for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
fadeOut(1, 10);

blah-de-blah
August 20th, 2003, 10:17 PM
wow that is extremely amazing voets :thumb:!!! Thanks, just have a few quick quesitons like usual :P

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 :)

Voetsjoeba
August 21st, 2003, 02:54 AM
Ok, code explanation:

First, Flash defines the functions:

function continueFade(mc, speed) {...}
fadeMC(mcnr, speed) {...}
function fadeOut(startboxnr,speed) {...}
After that, it creates the boxes:

for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}

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.

fadeOut(1, 10);


Flash enters the function fadeOut, and calls the function fadeMC with the same vars (1 and 10).

function fadeOut(startboxnr, speed) {
fadeMC(startboxnr, speed);
}
Flash enters the function fadeMC with the values it got from fadeOut, which are the values fadeOut received.

function fadeMC(mcnr, speed) {

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

_root["box"+mcnr].onEnterFrame = function() {

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.

_root["box"+mcnr]._alpha -= speed;
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.

if (_root["box"+mcnr]._alpha<=50) {
_root["box"+mcnr].onEnterFrame = null;
continueFade(_root["box"+mcnr], speed);
mcnr += 1;
fadeOut(mcnr, speed);
}
};
}


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

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.

blah-de-blah
August 21st, 2003, 12:18 PM
wow thanks for the detailed explanation ;) Ok so there is actually another part to it :P 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 :) 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 :) thanks

Voetsjoeba
August 21st, 2003, 01:01 PM
Well, that depends on how you set up the script that counts from 1 to 100. If you place

if(value%5 == 0){
dosomething
}

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

The following is an example:

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

blah-de-blah
August 22nd, 2003, 01:43 AM
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!

blah-de-blah
August 22nd, 2003, 04:20 AM
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?


if (functionChoice == 1 || functionChoice == 6 || functionChoice == 11 || functionChoice == 16) {

thanks

Voetsjoeba
August 22nd, 2003, 05:14 AM
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 ?

blah-de-blah
August 22nd, 2003, 05:25 AM
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 :P Just tryin to shorten it now because it'd be easier to load for slow connections right?


for (i=1; i<=20; i++) {
_root.box._visible = false;
duplicateMovieClip(_root.box, "box"+i, i);
_root["box"+i]._x = 17*i;
}
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;
}
};
}


well here it is. Long eh :P

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 ;) and ignore the comments i know they're hard to understand :)

Voetsjoeba
August 22nd, 2003, 05:56 AM
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 ?

blah-de-blah
August 22nd, 2003, 05:58 AM
yep thats right :) so can my code be shortened by mucH?

Voetsjoeba
August 22nd, 2003, 06:01 AM
Yes. But, why did you ask for the %5 thingy ? I'm confused about that.

blah-de-blah
August 22nd, 2003, 06:03 AM
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 :P

Voetsjoeba
August 22nd, 2003, 06:26 AM
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


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

blah-de-blah
August 22nd, 2003, 06:32 AM
pretty much :) but it doens't get the same bounce i was lookin for :P Nice though, thanks :thumb:

Voetsjoeba
August 22nd, 2003, 06:35 AM
Oh right, I was trying out some other effects but I forgot to change it back to the original. To do that, replace


speedy -= gravity;
_root["box"+nr]._y -= speedy/5;


with


speedy += gravity;
_root["box"+nr]._y += speedy/5;


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

blah-de-blah
August 22nd, 2003, 06:39 AM
now that you've said that, i think so too :P

blah-de-blah
August 22nd, 2003, 06:41 AM
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?

Voetsjoeba
August 22nd, 2003, 07:14 AM
No, because the function uses different onEnterFrames for each box. Each box's onEnterFrame that is. Look at the code:


function dropBox(nr, speedy, gravity, bounce, floor) {
_root["box"+nr].onEnterFrame = function() {
...
};
}


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

blah-de-blah
August 22nd, 2003, 07:58 AM
ahh yes thats right :P