Be a Supporter!

As3 Explaining The Return Function

  • 6,626 Views
  • 7 Replies
New Topic Respond to this Topic
chronicADRENLIN
chronicADRENLIN
  • Member since: Dec. 5, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
As3 Explaining The Return Function 2010-10-18 19:47:59 Reply

Here's a quick tutorial explaining how the return function to return a value in ActionScript 3.

To run the code, create a new Flash AS3 file and create one movieclip on the stage with the instance name "mc_Dot", without the quotation marks.

When the player clicks on the screen, mc_Dot's position will be set to a random location on the screen.

What's more - the random location's value is RETURNED from another function! Joy!

The following code is HEAVILY commented and should be self-explanatory.

//Import Flash Assets (in this case, MouseEvent asset)
import flash.events.MouseEvent;

//Add Event Listener to the stage to listen for whenever the player clicks on the stage
stage.addEventListener(MouseEvent.CLICK, RunMouse);

//Function Runs whenever the player clicks on the stage
function RunMouse(e:MouseEvent):void {
	
	/*
	EXPLANATION OF THIS FUNCTION
	As it is now, function is used to randomize the location of the on-screen object mc_Dot
	To do this, mc_Dot.x is set to a function that RETURNS a value
	This function is RandomizeValue and has three variables that must be inherently set when the function is called, like below
	
	EXPLANATION OF THE FUNCTION RANDOMIZEVALUE AND ITS VALUES
	Using this function, three values are set: bRound (bool, t/f), nHighVal (Number), nLowVal (Number)
	bRound is used to determine if the number will be rounded (TRUE, a whole number) or not (FALSE, a real number, eg. 1.34253)
	nHighVal is used to determine the HIGHEST possible value that the function can randomly return (Number)
	nLowVal is used to determine the LOWEST possible value that the function can randomly return (Number)
	
	EXPLANATION OF RETURNING A VALUE FROM A FUNCTIONS
	When setting a value by returning a value from a function, the function being called must be able to return a value
	Within the function, a value needs to be determined
	(In this case, the variable nReturnVal is being set randomly to a value between the nHighVal and nLowVal values)
	That value is then RETURNED ("return nReturnVal;"), or applied to the variable that called the function (in this case, mc_Dot.x/y)
	*/
	
	//mc_Dot's X value is randomly set between the values of 400 and 100, and the value is ROUNDED (it is a whole number, no decimal point)
	mc_Dot.x = RandomizeValue(true,400,100);
	//mc_Dot's Y value is randomly set between the values of 400 and 50, and the value is ROUNDED (it is a whole number, no decimal point)
	mc_Dot.y = RandomizeValue(true,400,50);
}

//Function is used to return a random value between the values of numbers nHighVal and nLowVal.
//The user can determine whether or not the value will be rounded to the nearest 1 via setting boolean bRound.
function RandomizeValue(bRound:Boolean,nHighVal:Number,nLowVal:Number) {
	//Declaring the variable nReturnVal - this variable will be used to hold the value to be returned while that value is calculated
	var nReturnVal:Number = 0;//Value is inherently set to 0 (because nobody likes null variables)
	//IF Statement is used to determine whether or not to round the value inputted to the nearest 1
	if (bRound) {
		//The final value WILL be rounded.
		//Determine a random value for nReturnVal between nHighVal and nLowVal. This value is rounded to the nearest 1.
		nReturnVal = Math.round(Math.random() * (nHighVal - nLowVal)) + nLowVal;
	} else {
		//The final value WILL NOT be rounded.
		//Determine a random value for nReturnVal between nHighVal and nLowVal.
		nReturnVal = Math.random() * (nHighVal - nLowVal) + nLowVal;
	}
	//Returns the final value of nReturnVal.
	//In other words, the value of nReturnVal is applied to the variable that called this function.
	return nReturnVal;
	
	/*
	NOTE: Functions that RETURN a value cannot have the suffix :void added to them when the variable is declared.
	:void is used to tell Flash that the following function will NOT return a value.
	If the function is going to return a value, do NOT put :void.
	However, if the function is NOT going to return a value, putting :void will make the function run slightly faster.
	Flash knows it won't have to check for a return at any point.
	*/
	
}

//Stop function stops animation from progressing (and if no animation is present, saves an itty bitty bit of processing power)
stop();

Enjoy! -Nick

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to As3 Explaining The Return Function 2010-10-18 20:09:37 Reply

Return isn't a function, it's a statement.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
Fion
Fion
  • Member since: Aug. 21, 2005
  • Offline.
Forum Stats
Member
Level 39
Blank Slate
Response to As3 Explaining The Return Function 2010-10-18 20:15:04 Reply

At 10/18/10 08:09 PM, Redshift wrote: Return isn't a function, it's a statement.

If it was that would be quite paradoxical, I mean how would you return from the return function!


.

BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to As3 Explaining The Return Function 2010-10-18 20:25:56 Reply

Using comments this way sucks.
I would not help you with code like that, if you ever had a problem with it.
Rule of thumb: If you need punctuation in your comments, you FAILed

I had to kill somebody to find this:
http://livedocs.adobe.com/flash/9.0/Acti onScriptLangRefV3/statements.html#return

Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As3 Explaining The Return Function 2010-10-18 20:54:15 Reply

At 10/18/10 08:25 PM, milchreis wrote: Using comments this way sucks.

What's with all the hostility? He wrote this as a tutorial.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to As3 Explaining The Return Function 2010-10-18 21:15:56 Reply

At 10/18/10 08:54 PM, Version2 wrote: What's with all the hostility? He wrote this as a tutorial.

Writing bad tutorials doesn't help anybody.

chronicADRENLIN
chronicADRENLIN
  • Member since: Dec. 5, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As3 Explaining The Return Function 2010-10-18 21:20:23 Reply

At 10/18/10 08:54 PM, Version2 wrote:
At 10/18/10 08:25 PM, milchreis wrote: Using comments this way sucks.
What's with all the hostility? He wrote this as a tutorial.

Agreed. If you have a problem write your own tutorial. And yes, this stuff is available in the AS3 documentation online, this was written as an introductory thing for people who aren't very technical.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to As3 Explaining The Return Function 2010-10-18 21:36:38 Reply

At 10/18/10 09:20 PM, chronicADRENLIN wrote: for people who aren't very technical.

Then you shouldn't waste those guys' time by explaining your algorithm and get straight to the point of the "tutorial".
Instead of explaining the important aspects of the return statement, you tell people what "stop()" does.

Despite the lack of information (which itself would make this thing suck), the presentation is bad.
The text is long and makes the entire thing hard to read.