View Full Version : a function receiving a movieClip
KaizerKaizer
07-11-2008, 10:28 AM
I'm trying to learn ActionScript 3, doing a few tutorials etc... My code works fine, but I have a question which never gets explained, which I'd like to have explained for me.
In this tutorial I'm creating the game "Pong" - a paddle and a ball.
so, I have a paddle object called mcPaddle which of I create an instance called paddle_mc (which is a MovieClip). This part I do understand. Here comes the part I don't understand:
function checkHitLocation(paddle:MovieClip):void
this function receives a paddle:MovieClip. But what does plain paddle stand for? I have nothing but a layer called paddle, but that doesn't make sense, or? I've actually written "ball" instead just to test, but nothing changed when i ran the game.
So please, could anyone explain what it is or what it does, because this is the first encounter in my code where it pops up.
DiamondDog
07-11-2008, 01:46 PM
Probably easier to explain with a simple function like this
function doubleIt(anyNumber:int):int
{
return anyNumber*2;
} // end doubleIt
'anyNumber' is just a parameter, that's a label we use for the value passed into that function.
(If you want a function to act ON something, it's not possible to write that function unless you give that something a label.)
So if I have a variable called y, and I want to double it, I could do this
var y:int = 4;
trace y; // 4
var z:int = doubleIt(y); // 'anyNumber' will be given the value y
trace(z); // 8
So in your checkHitLocation function, that 'paddle' is just a parameter, or a label representing the movie clip passed into your function, which will be referred to in the code of that function.
Note that not ALL functions need parameters.
This function, for example, just says HELLO. It does its job without us needing to pass in any parameters.
function beFriendly():void // no parameters needed
{
trace("HELLO");
}
Hope that makes sense.
KaizerKaizer
07-11-2008, 02:39 PM
So I could have named the parameter (whatever:MovieClip) and still call the function using checkHitLocation(paddle_mc) since the parameter is just a label which should resemble a MovieClip? Because that is what I'm doing now, and that works, or? Just want to be clear about this.
DiamondDog
07-11-2008, 02:49 PM
So I could have named the parameter (whatever:MovieClip) and still call the function using checkHitLocation(paddle_mc) since the parameter is just a label which should resemble a MovieClip? Because that is what I'm doing now, and that works, or? Just want to be clear about this.
Yes, you can use any name you want.
function someFunction(anyNameYouWant:Object)
{
... anyNameYouWant ... // as long as that's the name you're using in the code
}
then call it using the actual name of the thing you want it to work on
var fred:Object = someFunction(jack:Object);
KaizerKaizer
07-11-2008, 06:00 PM
Yes, you can use any name you want.
function checkHitLocation(paddle:MovieClip):void
as long as that's the name you're using in the code
{
var hitPercent:Number;
var ballPosition:Number = ball_mc.y - paddle_mc.y;
hitPercent = (ballPosition / (paddle_mc.height - ball_mc.height)) -.5;
xDirection *= 1.05;
yDirection = hitPercent * 10.05;
}
then call it using the actual name of the thing you want it to work on
checkHitLocation(paddle_mc);
So by doing this, I can call checkHitLocation(somethingElse_mc) and it will activate the same code (as long as it's defined before somewhere "var somethingElse:MovieClip") within the curly braces, as writing the actual paddle_mc for calling the function.
Why is this possible?
the logical thing from my perspective would be to tell the function that it should take specifically function checkHitLocation(paddle_mc) and that would do the same thing.
What are the pros-cons of doing the latter one?
DiamondDog
07-11-2008, 06:26 PM
Not sure I understand the question.
Bear in mind that when you write a function you won't know (well, usually you won't know) the actual names of the variables, or other objects, that you want to use that function on.
For example, going back to my simple examplefunction doubleIt(anyNumber:int):int
{
return anyNumber*2;
} // end doubleIt
That function would be practically useless if you had to know in advance the name of the number you wanted to double.
As it is, you can use that function any number of times, applying it to different numbers each time:
var x:int = 20;
var y:int = 15;
var z:int = -12;
trace(doubleIt(x)); // 40
trace(doubleIt(y)); // 30;
trace(doubleIt(z)); // -24
Nowhere does the code of the function itself have to mention any of x, y, or z, and when you call the function doubleIt you don't have to mention 'anyNumber'.
'anyNumber' only appears in the function.
In that way, your function, and the calls to that function, are completely separate. That makes your function extremely flexible.
Hope that helps.
KaizerKaizer
07-12-2008, 11:15 AM
You use the parameter of the function (anyNumber:int) and then use it inside the function where you state return anyNumber*2, which I understand.
Sorry if I'm repeating myselft (I'm not that good at this yet) ;)
Really my question is:
if you wrote something that didn't use anyNumber inside the function (except anyNumber:int as a parameter). How does that work, what is the purpose of writing (anyNumber:int)?
I'll try to show you with an example (which maybe isn't correct but could illustrate my question further).
var x:int = 20;
function doubleIt(anyNumber:int):int
{
var thisIsANumber:Number;
thisIsANumber = x*2;
return thisIsANumber;
}
trace(doubleIt(x));
thanks for all the help in clarifying this subject to me!
DiamondDog
07-12-2008, 05:20 PM
No need to apologise for repeating yourself.
If you had a better teacher you wouldn't need to. ;)
If you put your code on the first frame of a .fla file, it will work.
var w:int = 20;
function doubleIt(anyNumber:int):int
{
var thisIsANumber:Number;
thisIsANumber = w*2;
return thisIsANumber;
}
trace(doubleIt(w)); // 40
(I've used w instead of x, because Flash isn't keen on us using x as a name for a variable.)
But notice what happens if you add the following lines:var s:int = 250;
trace(doubleIt(s)); // 40
Instead of getting the answer '500', you get '40' once again.
Why?
Because in your function, you're always doubling the value of w, and returning the answer.
That means your function is much less flexible than mine.
In fact, it's worse than less flexible, it's actually a bit dangerous because it returns answers we might not be expecting.
Your function is fine, if you know that you're only ever going to want to work out the value of 2 x w.
My function will work out 2 x w, just like yours does, but I can also work out 2 x y, 2 x d, 2 x fred, 2 x anything. And that extra flexibility comes from the way that my function has been written.
When I call my function, I pass in the name of the variable that I want doubled. With your function, you can also pass in the name of any variable BUT THE CODE IN YOUR FUNCTION NEVER REFERS TO THE NAME OF THE VARIABLE THAT'S PASSED IN. Nowhere in your function do you actually do anything with 'anyNumber'.
In your function, 'anyNumber' appears in brackets as the name of the parameter, but when you work out the value to be returned by the function, you always double the value of w. So the only value that your function ever returns is 2 x w. If w always stays at 20, then your function will always return 40, irrespective of what variable you pass in.
If you want a function that returns 2 x w, then your function is fine.
Although you don't need to pass in ANY parameter in that case.
You could just have this function:
function doubleW():void // no parameter needed
{
return 2*w;
} // end doubleW
But the following function is probably more useful:
function doubleIt(anyNumber:int)
{
return 2*anyNumber; // my code uses the name of that parameter which appears in brackets in the previous line
} // end doubleIt
Why is it more useful?
Because we can use it to work out 2 x w
var answer:int = doubleIt(w);
But we can also use it to work out 2 x d, or 2 x f etc etc
var answer1:int = doubleIt(d);
var answer2:int = doubleIt(f);
So the second function does what the first function does PLUS it does lots more.
If your function is going to do something to a movieclip, and you're certain that the movieclip is always going to have a particular name, and you're never going to want to apply that function to any other movieclip, then it's fine to write a function that doesn't take any parameter. In the code for your function just refer to the particular movieclip that you know in advance you're going to want to apply that function to.
But if there's any chance that actually, you might want to apply that function to several different movieclips, each with different names, you could write the function so that it takes a parameter, and then when you call the function, pass in the name of the specific clip you want it to work on, on that occasion:
function someFunction(anyClip):void
{
anyClip.alpha = 0.5; // again, our code uses the name of that parameter in brackets in the previous line
anyClip.x += 10;
} // end someFunction
And you'd call it in this way:
someFunction(fred); // using your function on the clip called 'fred'
someFunction(janet); // using your function on the clip called 'janet'
someFunction(goliath); // using your function on the clip called 'goliath'
Hope that helps.
Let me know if it doesn't.
KaizerKaizer
07-13-2008, 01:39 PM
Well, that sums up a few loose ends :) , yet 3 questions remain.
The paddle:MovieClip (check below) is just stating it will receive a MovieClip and not a variable called paddle, correct?
And if it is so, that it only accepts MovieClips, why tell it to get the specific paddle_mc (checkHitLocation(paddle_mc);) when it actually could receive any MovieClip and execute the function checkHitLocation correctly because it gets all necessary information about the MovieClips ball_mc and paddle_mc anyway inside the function?
function checkHitLocation(paddle:MovieClip):void
{
var hitPercent:Number;
var ballPosition:Number = ball_mc.y - paddle_mc.y;
hitPercent = (ballPosition / (paddle_mc.height - ball_mc.height)) -.5;
xDirection *= 1.05;
yDirection = hitPercent * 10.05;
//no paddle in here...
}
//and no paddle out here
checkHitLocation(paddle_mc);
Above, the function checkHitLocation(paddle:MovieClip):void is what the function will accept, but paddle:MovieClip never appears anywhere in my code (inside the curly braces or outside the rest of my code) but right there in that parameter. Nowhere else in my code paddle appears. This is the most confusing part. So here's my third question:
I actually don't need to demand the function to have a parameter containing paddle:MovieClip, leaving it empty, since it only will be applied for checking the hit location for the ball_mc and the paddle_mc, am I right? The paddle:MovieClip seems somewhat useless since I can use the MovieClips ball_mc and paddle_mc anyways inside, and most importantly don't use plain "paddle" anywhere, right? So why use it if i can spare my code a few useless words by writing it in this manner instead:
function checkHitLocation():void
{
var hitPercent:Number;
var ballPosition:Number = ball_mc.y - paddle_mc.y;
hitPercent = (ballPosition / (paddle_mc.height - ball_mc.height)) -.5;
xDirection *= 1.05;
yDirection = hitPercent * 10.05;
}
checkHitLocation();
DiamondDog
07-13-2008, 03:03 PM
As I said in my previous post, if you know in advance that your function is going to be applied to a particular movie clip, and you know the name of that movie clip, then sure, go ahead and write a function that doesn't take a parameter.
KaizerKaizer
07-14-2008, 12:35 PM
Thanks a million DiamondDog! I really got this now! I'll be back in short with a thousand more questions! :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.