Reply to topic  [ 14 posts ] 
 Functions and why they're so great! (AS2) 
Author Message
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
 Functions and why they're so great! (AS2)
What you're about to learn is something that will increase your game's maintainability, changeability, readability and gives you more functionality with less code and less errors.

A function (Also called method) is a bunch of code packed together neatly in a little package, kind of like the variable except for code instead of a variable.
You can use a function just like a variable with the exception that you cannot give the function a value with the equal character ( = ), in fact, a function cannot (or rather, should not) be changed during runtime. Once you get the hang of how it works, it will make your life so much easier.

Where to begin...

Code:
function test()
{

}

What you see above is an empty function it does absolutely nothing. the first thing you specify for a function is the name. In this case we have named it "test".
The second thing you'll see is an empty parenthesis. Later on you'll see what it is used for. For now, let's just leave it at that. Next you'll the brackets { } that specify where your function starts and ends. Anything within these brackets belong to the function. This is where you add the code.

Let's try and call it.

Code:
test();


alright, so we called an empty method, and nothing happened. Surprised? Well there is no code in the function so of course it doesn't do anything.

Let us go back to the function and add a trace call. (trace prints text to the output. A great way of checking what values your variables are in runtime.)

Code:
function test()
{
      trace("Hello Forkheads!");
}


now try and call test();

person 1: OMFGHOLYSHITWHATTHEFCUKISTHISWHATHAVEIBEENDOINGWITHMYLIFEIAMHORNY!
person 2: So what, you basically just called trace. Why can't I just call trace without a function thingymajigg?

You could call trace anywhere you want, but that's not the point. The point with this is that you can do THIS:

Code:
function test()
{
        var oneArray:Array = new Array(5, 6, 4, 33, 6, 78, 89, 34, 2, 5, 6, 6, 4, 3);
   for (var k:Number = oneArray.length - 1; k >= 0; k--)
   {
      for (var i:Number = 0; i < k; i++)
      {
         if (oneArray[i] > oneArray[i + 1])
         {
            var temp:Number = oneArray[i];
            oneArray.splice(i,1,oneArray[i + 1]);
            oneArray.splice(i + 1,1,temp);
   
         }
   
      }
   }
        trace("[");
   for (var i:Number = 0; i < oneArray.length; i++)
   {
      trace(oneArray[i]);
   }
   trace("]");
}

and then execute ALL that code simply by calling test();

person 2:Why not just doing all that code by the time I need it. Why do I need to make a function out of it? AND WHAT THE FUCK IZZZZZZZZ DAT?

First off, it's a sorting algorithm called bubble sort, don't pay attention to what's in the function. And why? Well... What if you wanted to do ALL that more than once? What if you wanted to do the same thing again 4 times at 4 different places and at 4 different times? Would you want to copy the code into all those 4 places? You could, but it would make so much more code to take care of. What if you wanted to make a change to the algorithm? That would mean you'd have to change the code in all 4 places, now, wouldn't it?

person 2: ohshi- Never thought about it that way.

Don't worry about it, though. The function fixes everything. With the function, you only need to have the code at one place, and call it from all places using test(); Sounds easy, right?

person 2: What have I been doing with my life...?

Well, it certainly wasn't professional. Now to get more into details.

I told you about the empty parenthesis, right? Well, it's time you learn the true power of the function.

When creating a function, you can fill the parenthesis with variables that you send with it as you call the function. You can specify any kind of variable to send, even objects, simply by putting a variable name (and type, but you don't have to. I suggest it, though.) in the parenthesis.
Very important note: All variables created in the function are ONLY available inside the function. They do not exist outside it.

Here's an example.
Code:
function test(coolStoryBro:Number)
{
     
}

Now your function test requires you to send a number (or a variable representing a number) when you call it. If you didn't specify type, you can pretty much send anything you want, but that will slow down the compiling a bit since the compiler will have to specify type instead.
for example:
Code:
test2(47);
var desu = 23;
test(desu);


neato, huh? And what can you do with this number, you ask?
Answer: FUCKING ANYTHING YOU WANT, BRAH!

Let us remove the array from the algorithm. (this part: var oneArray:Array = new Array(5, 6, 4, 33, 6, 78, 89, 34, 2, 5, 6, 6, 4, 3); )
and make it a global variable (a variable you can access anywhere) and let us also make another array (don't worry about how an array works. It's basically a list of numbers.)

So let's say on the first frame in your flash you put
let's also rename them so they don't have the same name as in the function.
Code:
var array1:Array = new Array(5, 6, 4, 33, 6, 78, 89, 34, 2, 5, 6, 6, 4, 3);
var array2:Array = new Array(8,4,2,1,7);


Then back to the function. Let us add an Array variable in the parenthesis!

Code:
function test(oneArray:Array)
{
   for (var k:Number = oneArray.length - 1; k >= 0; k--)
   {
      for (var i:Number = 0; i < k; i++)
      {
         if (oneArray[i] > oneArray[i + 1])
         {
            var temp:Number = oneArray[i];
            oneArray.splice(i,1,oneArray[i + 1]);
            oneArray.splice(i + 1,1,temp);

         }

      }
      trace("[");
      for (var i:Number = 0; i < oneArray.length; i++)
      {
         trace(oneArray[i]);
      }
      trace("]");
   }
}


now try calling test(array1);
and then test(array2);

Congratulations. You can now with one piece of code, sort any array with numbers you want simply by sending that array as a parameter (also called argument) when calling the function.

Make sure the variable types match so that you don't try to send a string to a function that expects a number, as it will cause strange errors if not the compiler stops you completely from testing.

You can send more than one parameter to a function. But only if you specify it in the function. A function call must always match the function, so if your function expects 2 integers, 1 string and 2 bools, then you must include all these variables in the correct order in the function call.
example:
Code:
function desu(haithar:Number, coolean:Boolean, bra:String, snelhest:Boolean, fartValue:Number)
{

}

desu(12,true,"hai",false,55);


or
Code:
num1:Number = 5;
num2:Number = 6;
coolBool:Boolean = true;
nawMan:Boolean = false;
gottaHaveAtleastOneLoliInThisTutorial:String = ":)";

desu(num1,coolBool,gottaHaveAtleastOneLoliInThisTutorial, nawMan, num2);


So now you know how to send stuff with a function. But what if you want the function to return something to you?
A variable can be used to set a value to another variable like so: var1 = var2;
What about functions?

Well, in functions it is a little bit more complicated than that. (notice a little)

As for a function to return something you must add a return statement in the code (and I also suggest specifying what type the function returns to reduce work for the compiler.)

And here is how:

First off, let us create a new function that takes two parameters, a minimum value and a maximum value. This function is going to return a random value between them.
Code:
function getRandomNumber(min:Number, max:Number):Number
{

      return 0;
}


noticing something new? Sure you do.
Person 2: Yes. There's a "return 0;" OUT OF FUCKING NOWHERE, AND WHAT, THE FUNCTION IS A NUMBER NOW? WHAT IS HAPPENING?!

Well, you see... Now that we're going to have the function RETURN a number, we should specify what it is going to return by adding :Number
and then we'll have to end the function with a return. You can have return anywhere you want, but as soon as return is called, you will leave the function and any code after it wont be read EVER, unless of course you have an if statement or something making you jump over the return calls. Right now, the function returns 0. so if you do like this:
var foo:Number = getRandomNumber(10,15); foo is going to be 0. Not very random, I know. Let's fix this right now.

We want the function to return a random number between the min and max.

Code:
function getRandomNumber(min:Number, max:Number):Number
{
      var difference:Number = max - min; //calculate difference
      var returnValue = Math.floor(Math.random()*( difference+1 ) ) + min; //get the random number
      return returnValue; //return the random number.
}

and just like that we have a function that both receives values and returns one too.
Let's use it!
Code:
//example1
var randomNumber = getRandomNumber(15,20);
trace(randomNumber);

//example2
randomNumber = getRandomNumber(55,900);
trace(randomNumber);

//example3
var desu = 5;
randomNumber = getRandomNumber(desu,10);
trace(randomNumber);



Functions that do not specify a return type return VOID by default, meaning nothing.
Even in functions without return types you can still call return and leave the function. You must make sure your return call has the correct value, though, meaning if the method returns an integer, what you send through the return call must also be an integer. If your function returns VOID, you can just put
Code:
return;

Sounds easy enough, right? Well, that's all for functions! MAKE SURE TO FUCKING USE IT, BECAUSE NO ONE WANTS TO GO THROUGH THE SAME SHITTY CODE OVER AND OVER AGAIN IN YOUR MOTHERFUCKING PILE OF DOG POO CALLED CODE, AND I SPENT LIKE 2 HOURS WRITING ALL THIS DOWN, SO BE THANKFUL! :)

See any errors? Post them here. Something you don't understand? Post it here. Any questions at all? PostHere();

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Mon Nov 19, 2012 9:26 am
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
THIS.

CHANGES.

EVERYTHING.

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Mon Nov 19, 2012 3:14 pm
Profile E-mail WWW
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
User avatar

Cash on hand:
258,935,827.73

Bank:
7,777,777.77
Posts: 19745
Joined: Fri Mar 04, 2011 9:57 pm
Location: ЇИ УОЦЯ MЇЙD FЦCКЇЙG ЇT ЇЙTО ОBLЇVЇОЙ
Group: Їи$aиїту
Country: Nepal (np)
Post Re: Functions and why they're so great! (AS2)
damn right it does

_________________
ImageImage
Image
?
Їи$aиїту Group! | Ultimate Fh Tribute!
© 2010 -2099 Odin Anarkis. All Rights Reserved.

Quotes
Spoiler: show
Image
who149 wrote:
I'm trying i'm trying~ i'm making I'll try too slowly up my posting. At least once a day for a bit. Then I'll up that too twice, then four, then 8 and so on.
Until eventually I wake up one morning and find out that I am actually an Idiot hero.
On some quest too cheat on his gf or raise affection of 5 women who conveniently live in my the same dorm as me.
In which I only have 100 days to seduce them all.

Remon wrote:
Now we can dominate the porn industry, camera industry, AND the world!
YomToxic wrote:
YOU BETTER STAY ALIVE OR ELSE I WILL HUNT YOU DOWN AND RAPE YOU DEAD.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
2 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.


Mon Nov 19, 2012 6:33 pm
Profile E-mail WWW
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
Discovery: Advanced Engineering!

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Mon Nov 19, 2012 6:42 pm
Profile E-mail WWW
Level 1
Level 1
User avatar

Cash on hand:
646,302.11

Bank:
1,569,148.01
Posts: 114
Joined: Mon Jan 02, 2012 4:45 pm
Group: 1991
Post Re: Functions and why they're so great! (AS2)
Protip: This is amazing!

_________________
Suicide Alice



Spoiler: show
11/29/2012
Image



ImageImage

ImageImage

Image

_________________
Click the icon to see the image in fullscreen mode  
4 pcs.
Click the icon to see the image in fullscreen mode  
100 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
36 pcs.


Tue Nov 27, 2012 12:30 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
So here's what I'm trying to do.

On an early frame,
Quote:
function wallright()
{

while (true) {

if (this.hitTest(_root.player._x, _root.player._y, true)) {

_root.player._x++;

} else {

break;

}

}

}


And on the movieclip that prevents our plucky hero from walking through walls...

Quote:
onClipEvent (enterFrame) {

wallright();

}


It works not. Why?

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Wed Dec 12, 2012 1:31 pm
Profile E-mail WWW
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: Functions and why they're so great! (AS2)
since it is placed inside an onclipEvent, it is probably looking for wallright() inside of the movieclip.
use _root.wallright();

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Wed Dec 12, 2012 2:35 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
Quote:
onClipEvent (enterFrame) {

root.wallright();

}


?

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Wed Dec 12, 2012 2:36 pm
Profile E-mail WWW
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: Functions and why they're so great! (AS2)
yes

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Wed Dec 12, 2012 2:43 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
... still not working.

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Wed Dec 12, 2012 3:08 pm
Profile E-mail WWW
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: Functions and why they're so great! (AS2)
you forgot the _ before root.

Also, inside the function it says "this" implying that you want to collide the player with _root, because that's where the function is located.

You need to send a pointer of the movieclip into the function and tell the function to use that object instead of "this"

Code:
function wallright(mc:Movieclip) //except a movieclip
{
      while (true)
      {
            if (mc.hitTest(_root.player._x, _root.player._y, true)) //perform a hitTest with the movieclip and the player
            {
                  _root.player._x++;
            }
            else
            {
                  break;
            }
      }
}

onClipEvent (enterFrame)
{
      root.wallright(this); //send myself to the function
}

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Wed Dec 12, 2012 4:31 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
I'm being launched violently to the far edge of the room. :O_O

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Wed Dec 12, 2012 4:38 pm
Profile E-mail WWW
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: Functions and why they're so great! (AS2)
that's because the origo in your movieclip is hitting the wall.

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Fri Dec 14, 2012 5:22 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: Functions and why they're so great! (AS2)
Chuu~ thank you, parpol-san!

Hurray!
With this, loliparpol will see how smart and talented I am!
She might even give me a kiss...

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Fri Dec 14, 2012 8:56 pm
Profile E-mail WWW
Display posts from previous:  Sort by  
Reply to topic   [ 14 posts ] 
 

Similar topics

 
Your Sexcellency, I Offer Thee Great Praise!
Forum: ./General Spam
Author: RV-007
Replies: 1
The Great Game!
Forum: ./General Spam
Author: Pantsman
Replies: 31
a brief introduction to the great wheel
Forum: Forkheadia Lore
Author: tuypo1
Replies: 16
Make Anime Great Again
Forum: Dev Forum
Author: Lime
Replies: 3
Top


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Mods Database :: Imprint :: Crawler Feeds :: Reset blocks
Designed by STSoftware for PTF.

Portal XL 5.0 ~ Premod 0.3 phpBB SEO