PDA

View Full Version : help on random color thing.



lostandfund
September 11th, 2002, 08:55 PM
QUESTION:

how do i go about having the color of one movie clip follow the same color of the random colored movie clip?


thanks to Mr.Ilyas's nice and short tutorial, i did my FIRST actionscript, Random Colors. yeay!

i can't seem to find the tutorial, but the coding went like this:

myColor = Math.round (Math.random()*0xFFFFFF);
myColoredObject = new Color (this);
myColoredObject.setRGB(myColor);

i added this action to one movie clip. now, can i "synchoronize" the color of another movie clip to this one?
Can anyone help me? heh heh

lostinbeta
September 11th, 2002, 11:24 PM
WOO HOO SCORE! I got this one.

Inside your frame add these actions...

_root.onEnterFrame = function() {
myColor = Math.round(Math.random()*0xFFFFFF);
};
MovieClip.prototype.makeColor = function() {
myColoredObject = new Color(this);
myColoredObject.setRGB(myColor);
};

now directly on your movie clip add this...


onClipEvent (enterFrame) {
this.makeColor();
}


You can add that to as many movie clips as you want and they will all become the same random color value at the same time:)

lostinbeta
September 11th, 2002, 11:26 PM
Here, I posted a .zip file with a .fla and .swf for examples.

PS: I only knew this one because I just got done doing a gradient experiment with random colors.

lostandfund
September 11th, 2002, 11:31 PM
thank you! thank you!. i'm at work right now, so i can't try it out and see the result, but i'll definitely work on it when i get home! thanks, beta.

lostinbeta
September 11th, 2002, 11:36 PM
no problem fund:)

<B>EDIT:</B> This will only work in Flash MX. You didn't state what version you had. Hopefully it is MX, because if it isn't, I can't help you out. I don't know Flash 5.

lostandfund
September 11th, 2002, 11:55 PM
oh...

i have flash 5....

well, maybe i can figure something out. oh. i gotta go.
thanks anyways!

lostinbeta
September 12th, 2002, 12:03 AM
oh :( that sucks. Maybe you will be able to figure it out in 5 from what I showed you.

Sorry for misleading you.

upuaut
September 12th, 2002, 01:51 AM
I'm not all that sure what it is you're trying to do. Perhaps if you both could explain it a little more to me, I could provide a 5.0 translation for LIB's MX method.

lostinbeta
September 12th, 2002, 01:56 AM
I can explain....

onClipEvent(enterFrame){
myColor = Math.round (Math.random()*0xFFFFFF);
myColoredObject = new Color (this);
myColoredObject.setRGB(myColor);
}

That is applied directly to the movie clips actions. This randomly changes the color on enterFrame.

what lif wants is more than 1 movie clip that uses the same actions, but they all change to be the same color.

Although my example is kind of tacky, it shows exactly what my code above for MX does. Which is what lif wants.

Check this link for example (http://www.8ballcreations.com/lostinbeta/ranColorSync.html)

upuaut
September 12th, 2002, 02:32 AM
*******I'm still writing this up.. it doesn't work yet.******

Well.. I suppose there are a number of ways that could be done. Personaly, I'd use a "for. . . in" loop or maybe just a function.

for in loops detect all of something in something else, but I haven't gotten them to work correctly, all of the time, on movie clips yet.. we'll see how this code works.

function colormall(){
myColor = Math.round (Math.random()*0xFFFFFF);
for(mc in _root){
myColoredObject = new Color (mc);
myColoredObject.setRGB(myColor);
}
}

can't tell if that will work yet.. but then you would just place the following on ONE of the clips.. not all of them.

onClipEvent(enterFrame){
_root.colormall();
}

The problem with this as I see it is that it will set any movie clip that's on the stage.. including those that you might not want to have set.

I'm thinking on this problem. :)

*******************************************

pom
September 12th, 2002, 08:02 AM
Well, you don't really need to run that enterFrame, do you? :)
Basically, what you have to do is get that color out of the movie clip, on the _root for instance:
_root.myColor=...If you don't want to do that, and that you have clips clip1 and clip2, you can call clip1's color from clip2 (assuming that they are at the same depth):
myColoredObject = new Color (this);
myColoredObject.setRGB(_parent.clip1.myColor);You' d have to put that in a handler of clip2 of course.

pom :asian:

Glad you like it!

pom
September 12th, 2002, 08:07 AM
And since I'm such a nice guy, here's a new one for you (I learnt it from the Bit himself, Keith Peters): put this on a movie clip
onClipEvent (EnterFrame){
red=Math.sin(ra+=.02)*127+128;
green=Math.sin(rb+=.05)*127+128;
blue=Math.sin(rc+=.09)*127+128;
myCol=red<<16|green<<8|blue;
myColored=new Color(this);
myColored.setRGB(myCol);
}I let you discover the effect (if the code works because I didn't test it :P). This should be in the AS tricks section, it's just soooo cool :)

pom =)

upuaut
September 12th, 2002, 08:08 AM
I don't understand your post Pom.. :)

pom
September 12th, 2002, 08:21 AM
Which one? :)

upuaut
September 12th, 2002, 08:49 AM
the first one.

upuaut
September 12th, 2002, 08:50 AM
oh and by the way that last one does work. nice effect.

pom
September 12th, 2002, 09:11 AM
We need mind-readers... I just wanted to say that with the original piece of code, the variable myCol is located inside the movie clip, so if you want to access that variable, you can either put it in the _root so that the second clip can call the same variable, _root.myCol.
Or clip2 can access the variable directly inside clip1 with the right path.

A classic variable scope issue...

pom :)

lostinbeta
September 12th, 2002, 11:53 AM
Wow, very nice random color fade Ilyas. I worked out the same effect, but to nowhere near the effeciency of your code. I am using that from now on when I need to :)

Keith Peters is a genius no doubt.

upuaut
September 12th, 2002, 12:03 PM
ahh.. I see. No mind readers needed.. sometimes I'm just slow. :)

lostandfund
September 12th, 2002, 05:13 PM
woH! people. thanks for the help!
(i had to read each posts about good twenty times to understand what you guys were talking about. okay, FINE. i lied. about fourty times.... :)

i'm at work right now, but i'm gonna try mr.Ilya's coding when i get home. the one with "_parent.mc1" thing.



oh yeah, Mr. Ilyas, btw, what is it mean by:

(assuming that they are at the same "DEPTH"):
that was your first post, i think. Is that the length of mc?

oh, boy. i can't wait to come up with other questions to ask you guys to solve. haha. just kidding.

anyway, thank you.
laf

lostinbeta
September 12th, 2002, 10:23 PM
Depth is the just that. The depth your movie is loaded. Your movie default loads at depth 0.

Think of depth as using the layers, you create a new layer and if it is higher than the other levels, you can see it on top over everything under it. This is because it is on a higher depth level.

pom
September 13th, 2002, 05:46 AM
Actually, Lost, that's not exactly what I meant, and it's my fault because I didn't express myself correctly (and I think you're confusing levels and depths, but that's another story). I should have said something even more confusing like 'at the same level in the tree'.

Basically, _root is the root of your tree. Any clip that's going to be on the scene is a branch. Then, each branch can have movie clips in it, that is to say little branchs and so on and so forth.

So let's take a simple case of 2 clips in the _root, clip1 and clip2. You want to access clip2 from clip1. But they are not directly connected, they are connected through the _root, which is the branch that is 1 level up in the tree. To go one level up, you use _parent, which (in this case) gets us on the _root.

Finally, _parent.clip2 gets us on that other branch we're trying to reach.

Wow, I impressed myself on that one :P

pom :asian:

pom
September 13th, 2002, 05:48 AM
We're going a bit astray, but this should be in the BOKirupa, don't you think?

upuaut
September 13th, 2002, 05:56 AM
(technicaly speaking though the _root is more of the trunk of the tree than the roots... but that's just nit picking.. plus I don't think that _trunk would have gone over as well for some reason. :) )

Bezzer
September 13th, 2002, 07:25 AM
hrmmm i've always used

(Math.random() * 255 << 16 | Math.random() * 255 << 8 | Math.random() * 255)

But yeah it probably works the same as Math.random()*0x000000

:):pirate:

pom
September 13th, 2002, 09:19 AM
Yeah, more or less the same. Except in your code you control independently the red, blue and green. But since it's random... :P

Rate the thread, guys!!

pom :asian:

Bezzer
September 13th, 2002, 09:44 AM
rateing threads is so overated... hehehe :)

lostinbeta
September 13th, 2002, 11:25 AM
Ok, I am late on the subject but....

Ilyas: You are right I got myself all mixed up there for a second. I have no clue what went on being it was only 10:30pm so it wasn't like when I mix myself up at 3:00am. Hmm, odd, oh well, thank you for correcting me.

pom
September 14th, 2002, 01:42 PM
Originally posted by david
(technicaly speaking though the _root is more of the trunk of the tree than the roots... but that's just nit picking.. plus I don't think that _trunk would have gone over as well for some reason. :) ) :P
_trunk.gotoAndPlay(5);I like that.

pom :P

lostinbeta
September 14th, 2002, 03:52 PM
Haha, we should take that up with Macromedia. Maybe they will change it to _trunk:P

fadingblack
September 14th, 2002, 06:42 PM
sorry i came in too late... but i have a question about the code ilyaslamasse posted.
Could someone explain the Math.sin thing. I don't get it!

:o

Bezzer
September 15th, 2002, 01:11 AM
hehe well maybe it means that the "root" is the begining of everything....its the thing that holds it all together.. :)

Bezzer
September 15th, 2002, 01:14 AM
Math.sin is triganometry...its the sine thingy... it uses your angle to work out a posostion that a thing should be so like
_x = Math.sin(angle*Math.PI/180)*radius+centerX;

If you make something like that it should move up and from left to right.... :) have fun :)

fadingblack
September 15th, 2002, 05:34 AM
Well, thanks for the explanation, although i'm afraid i will never be able to use such thing. At least conciously... i was never good when messing with triganometry.

Ummm, in fact i think i will never be good at coding because i have always been veeeeery bad at maths. God, triganomatry makes me shake and sweat! he, he, he...

Anyway, i'll give it a try, just for fun. Thanks again :)

Bezzer
September 15th, 2002, 07:24 AM
you dont have to be that great at maths to do things like this....im not really that great at maths...i just understand basic stuff....u should still be able to do programing even if your not a maths wiz... :)

pom
September 15th, 2002, 02:40 PM
The big interesting thing about the sine function is that its values are located between -1 and 1. It goes up, and then down very smoothly. And most important: it's cycling. Exactly what we want for our colours.

You can also check the plotter I did in the Open source section for a plot of the function.

pom :asian:

upuaut
September 15th, 2002, 03:11 PM
and as for higher math.. don't you worry about it Lostandfund, I am one of the worst math students anywhere and even I'm getting into sines and cosines. The beuty of it is that you really only need to know what the functions do. Like a calculator.. Flash does the hard part of calculating it for you. :)

Bear with it, and try to do small tutorials that take advantage of them. That is the way to learn how they work.

I remember doing a tutorial on a bouncing ball which used a sign wave to create the movement. It was so simple, and the first time I had a clue what sine did.

fadingblack
September 16th, 2002, 01:54 PM
Well, I'll have to believe you, because this weekend i finally succeded in coding a function that worked! I couldn't believe it. And what's even funniest, i don't even know how i did it... Human mind is a mistery, yes sir!

EDIT:ummm I wonder how to use this with alpha... I'll give it a try..

Ryall
September 18th, 2002, 05:40 PM
hey since depth was mentioned, and I am too busy to try it out... Flash always publishes from "Bottom Up"... if you swaped this for the other option "Top Down" would that make a depth of 1 the highest ans 2 lower etc?

Peace

lostinbeta
September 18th, 2002, 05:45 PM
I don't think that changes the order. It just changes the order that your movie loads. So it loads from top to bottom instead of bottom to top without changing the order.

Ryall
September 18th, 2002, 06:01 PM
ahhhh, I knew it did that, but good to know it doesnt mess with depth... Thanks lost

Peace

lostinbeta
September 18th, 2002, 06:01 PM
No problem.