# Using functions

**URL:** <https://forum.kirupa.com/t/using-functions/28414>\
**Category:** Uncategorized\
**Created:** [August 13, 2003, 3:33pm UTC](https://forum.kirupa.com/t/using-functions/28414 "2003-08-13T15:33:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paisley](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@paisley](https://forum.kirupa.com/u/paisley)\
**Post date:** [August 13, 2003, 3:33pm UTC](https://forum.kirupa.com/t/using-functions/28414/1 "2003-08-13T15:33:02Z")

</div>

I have a problem with functions.

I have a function:

function changeColor(movieclip,color) {  
…code here…  
}

and I have 2 movieclips: square and circle. Once the whole movie starts, I do this:

square.onEnterFrame = changeSquareColor;  
function changeSquareColor() {  
changeColor(square,0xff0000);  
}

circle.onEnterFrame = changeCircleColor;  
function changeCircleColor() {  
changeColor(circle,0x999999);  
}

The result is that both movieclips change to the same color 0x999999.

Why is that?

Help, please

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 3:43pm UTC](https://forum.kirupa.com/t/using-functions/28414/2 "2003-08-13T15:43:05Z")

</div>

```auto

circle.onEnterFrame = function {
changeColor(this,0x999999);
}
square.onEnterFrame = function () {
changeColor(this,0xff0000);
}

```

that should work…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 3:50pm UTC](https://forum.kirupa.com/t/using-functions/28414/3 "2003-08-13T15:50:51Z")

</div>

Jubba,

could you possibly explain why what you put should work and what paisley had wouldnt please.

I’m still learning so it’ll help, just curious.

Regards,  
Viru.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:33pm UTC](https://forum.kirupa.com/t/using-functions/28414/4 "2003-08-13T18:33:43Z")

</div>

Although he was defining 3 functions to change the movieclips color, i dunno why it wasnt working. Maybe if he points what was the changeColor function. :-  
The following would have worked:[AS]function changeColor(movieclip, RGB) {  
myColor = new Color(movieclip);  
myColor.setRGB(RGB);  
}  
square.onEnterFrame = changeSquareColor;  
function changeSquareColor() {  
changeColor(square,0xff0000);  
}

circle.onEnterFrame = changeCircleColor;  
function changeCircleColor() {  
changeColor(circle,0x999999);  
}[/AS]But as Jubba pointed, could be replaced for:[AS]function changeColor(movieclip, RGB) {  
myColor = new Color(movieclip);  
myColor.setRGB(RGB);  
}  
square.onEnterFrame = function() {  
changeColor(this, 0x336699);  
};  
circle.onEnterFrame = function() {  
changeColor(this, 0x990000);  
};[/AS]  
But if he had used the following code, both circle and square will be set to 0x990000.[AS]circle.onEnterFrame = changeColor(this, 0x990000);[/AS]Or maybe he was pointing the setRGB method to “this”[AS]function changeColor(movieclip, RGB) {  
myColor = new Color( **this** ); //all mcs would have its RGB set to the same color  
myColor.setRGB(RGB);  
}[/AS]
