-
[F8] Functions
How do I use function? I wanna make a function that says that a few blocks will move left, so instead of writing :
_root.block1._x -= 10
_root.block2._x -= 10
_root.block3._x -= 10
_root.block4._x -= 10
_root.block5._x -= 10
I just wanna put em all together in a function so I can move em all. (and they aren't just 5, way more.
-
PHP Code:
var blockNum:int = 5;//however many you have
function moveIt():void {
for (var i:int=1; i<=blockNum; i++)
_root["block"+i]._x -= 10;
}
moveIt();
That should do it.
-
You can make it self contained and send values as arguments to make it flexible for other situations where you need this with other clips:
PHP Code:
function moveFlexible( sIDName:String, nDistanceX:int, nDistanceY:int, nNumberToMove:int, mcTarget:MovieClip ) : void {
// default to _root if no target mc specified
mcTarget = mcTarget == undefined ? _root : mcTarget;
// loop through the clips and apply co-ordinate changes
for( var i:int = 1; i <= nNumberToMove; i++ ) {
mcTarget[ sIDName + i ]._x += nDistanceX;
mcTarget[ sIDName + i ]._y += nDistanceY;
}
}
moveFlexible( "block", 10, 0, 5, _root );
RipX
-
RipX, That's a little complicated for me, plus it would be easier to name them all block1, block2 etc...
Oh and another question, if I put this code whenever I want them to move:
var blockNum:int = 5;
for (var i:int=1; i<=blockNum; i++)
_root["block"+i]._x -= 10;
would it work? Instead of using functions?
-
Yes, but it would become messy very quickly...the idea is that you can call the function with a single reference to that function.
PHP Code:
function sayHello( ) : void {
trace( "Hello" );
}
// call the function
sayHello( );
// call it again
sayHello( );
// one more time
sayHello( );
RipX
-
Riiiiiiiiiiiiiiiight, so how do I call the function you posted?
moveFlexible();
only or what?
-
Well...yes, but there is a little bit more to it. You can add arguments to functions, arguments are values that can be passed and reference inside your function to make it more reusable (reading this next bit thoroughly will save you a lot of head scratching)...
for example:
PHP Code:
function squareNumber( nNumber:Number ) : void {
trace( nNumber * nNumber );
}
// trace : square the number 7. - traces 49
squareNumber ( 7 );
// trace : sqare the number 89. - traces 7921
squareNumber ( 89 );
So as you can see this function can accept an argument and is re-usable, in this case the argument is a number, but you may pass more than 1 argument as a commer delimited list such as:
PHP Code:
function addNumbers( nArgument1:Number, nArgument2:Number ) : void {
trace( nArgument1 + nArgument2 );
}
addNumbers( 20, 12 ); // traces 32
addNumbers( 45, 6.65 ); // traces 51.65
And one last one for you...see the 'void' keyword in the previous examples? Well this is there because these functions do not return a value, but a function may return a value to store inside a variable, such as:
PHP Code:
function parseTwoWords( sString1:String, sString2:String ) : String {
return sString1 + " " + sString2;
}
var sMyParsedString:String = parseTwoWords( "Hello", "world!" );
// trace the variable where you stored the result of the function call.
trace( sMyParsedString ); // traces "Hello world!"
So, now you should know how to call that function :D
RipX