Functions

Hi guys :pope:
Can anyone show me how would you arrage this code into a function because Im not very familiar with functions.

I have this test im making of coins and dollar bills and what i allways have done is to paste the same code over and over in each movie clip but Yeldarb told me that there was a much easier way and efficient way to do that using loops arrays & functions and the code is :

//----start-----
on(press){
startDrag(this);
}
on(release){
stopDrag();
if(this.hitTest(_root.Harry)){
_root.Value1 = 0.25;//this is a quarter coin
}else{
_root.Value1 = 0;
}
}
//—end-----

and on a blue box with an instance name of Harry i puted

//–start–

onClipEvent(enterFrame){
_root.TotalValue = _root.Value1+_root.Value2+_root.Value3;//keeps going until _root.Value 33
}
//–end–

Thanks,

[COLOR=Red]wait, wait, wait Is this even possible I’ll take hints too[/COLOR] :stuck_out_tongue:

cardjorg :slight_smile:

Ok I’ll put this easier
Instead of puttingn this code in 33 different Movie Clips

if(this.hitTest(_root.Harry)){
_root.Value1 = 0.25;//this is a quarter coin
}else{
_root.Value1 = 0;
}

how can I make it a function like
testing = function(){
if(this.hitTest(_root.Harry)){
_root.Value1 = 0.25;
}else{
_root.Value1 = 0;
}
}
and I know "this.hitTest " wont work but what can I use?

Any one knows what im talking about besides me :stuck_out_tongue:

for that function to work, your going to have to call it from the onClipEvent part of the code. So since your function is called “testing”, your code might look like this:
onClipEvent(enterFrame){
_root.testing();
}
If this.hitTest wont work, you can always try to pass a variable to the function. So something like this would be the function declaration:
testing = function(clipToUse){
if(clipToUse.hitTest(_root.Harry)){
_root.Value1 = 0.25;
}else{
_root.Value1 = 0;
}
}
//while the calling code might look like this
onClipEvent(enterFrame){
_root.testing(this);
}

Ooooo I see thanks :), now one more question
how could I put an array so it gives a new value lets say another quarter is dragged to the blue box (Harry) now instead of Value1 = 0.25 Value2 = 0.25 to later add them all.
Thanks bobdoe :slight_smile:

Well why dont you just add them on the spot? Or are you making some kind of record of these things? Anyway, this would be somewhat easy to fix. First, I think you would have to declare an array somewhere on the root timeline:
my_values = [];
index = 0; //index we are going to use.
//function is going to change a bit.
testing = function(clipToUse){
if(clipToUse.hitTest(_root.Harry)){
_root.my_values[index] = 0.25;
index++; //increment index by 1 after something hits harry.
}else{
_root.Value1 = 0;
}
}

Thanks man you just tought me on how to use arrays and functions in two simple posts :slight_smile:

no problem, if you can use them properly then your set. Make sure you have the right syntax tho, because mine is sorta sloppy =(