functions & objects flash actionscript 3.0 introduction to thomas lövgren, flash developer...
TRANSCRIPT
Functions & ObjectsFunctions & Objects
Flash ActionScript Flash ActionScript 3.03.0
Introduction toIntroduction to
Thomas Lövgren, Flash developer
FunctionsFunctions Functions are Functions are blocks of code blocks of code that carry out specific that carry out specific
tasks and can be tasks and can be reusedreused Your function Your function namename should be unique and describe the should be unique and describe the
value the function returns (value the function returns (same rules for namegiving same rules for namegiving as for variables)as for variables)
A function can be called at any time by a A function can be called at any time by a function callfunction call Functions can take Functions can take parametersparameters A function should carry/have just one specific taskA function should carry/have just one specific task If a function does not return a value, it’s return type If a function does not return a value, it’s return type
should be should be void void
Tip!Tip! If you often write similar code segments, a function If you often write similar code segments, a function could save time and spacecould save time and space
Function syntaxFunction syntax Example of the different parts and structure of a Example of the different parts and structure of a
function declaration/contentsfunction declaration/contents
FunctionFunction The structure of a user defined function (two ways):The structure of a user defined function (two ways):
//example 1//example 1function functionName([parameter0,..parameterN]):returnType{ function functionName([parameter0,..parameterN]):returnType{
// statement(s) // statement(s) } }
//example 2//example 2functionName = function ([parameter0,..parameterN]):returnType{ functionName = function ([parameter0,..parameterN]):returnType{
// statement(s)// statement(s)}}
//example of a basic function //example of a basic function function showMsg():void{function showMsg():void{
trace("I love ActionScript3!"); trace("I love ActionScript3!"); //output ”I Love ActionScript3!”//output ”I Love ActionScript3!”}}
showMsg(); showMsg(); //function call//function call
Function with parameters (1/2)
This function takes two parameters, calculates This function takes two parameters, calculates and return the areaand return the area
//set up the function //set up the function
function getArea(x:Number, y:Number):Number{ function getArea(x:Number, y:Number):Number{ //return type:Number//return type:Number
return x * y; return x * y; //calculate and return the area//calculate and return the area
}}
getArea(100, 25); getArea(100, 25); //function call, send parameters//function call, send parameters
Function with parameters (2/2)
This function takes an Array as a parameter, This function takes an Array as a parameter, loop through the Array and make a trace loop through the Array and make a trace output of the data:output of the data:
var length_array = new Array(); var length_array = new Array(); //declare the array//declare the arraylength_array = [72, 75, 89, 92, 65, 79]; length_array = [72, 75, 89, 92, 65, 79]; //assign values to the array//assign values to the array
//function that takes an array//function that takes an arrayfunction printSubjectData(subjectArray:Array):void{function printSubjectData(subjectArray:Array):void{
for(var i:int = 0; i < subjectArray.length; i++){ for(var i:int = 0; i < subjectArray.length; i++){ //for loop//for looptrace(i + trace(i + ""::"" + subjectArray[i]); + subjectArray[i]); //output//output
}}}}
printSubjectData(length_array); printSubjectData(length_array); //function call//function call
Output0:721:752:893:92etc …
Functions & scope Two examplesTwo examples: We make a function call for the : We make a function call for the
calculate function and trace the output like:calculate function and trace the output like:
//example 1//example 1function calculate():Number{ function calculate():Number{
var num1:Number = 50;var num1:Number = 50;var num2:Number = 100;var num2:Number = 100;var myResult:Number;var myResult:Number;myResult = num1 + num2;myResult = num1 + num2;
}}trace(myResult); trace(myResult); //THIS WILL GENERATE AN ERROR! //THIS WILL GENERATE AN ERROR! --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//example 2//example 2var myResult:Number;var myResult:Number;function calculate ():Number {function calculate ():Number {
var num1:Number = 50;var num1:Number = 50;var num2:Number = 100;var num2:Number = 100;myResult = num1 + num2;myResult = num1 + num2;
}}
trace(myResult); trace(myResult); //THIS WILL NOT GENERATE AN ERROR//THIS WILL NOT GENERATE AN ERROR
ObjectsObjects In ActionScript 3.0, every object is defined by a classIn ActionScript 3.0, every object is defined by a class The Object class serves as the base class for all class The Object class serves as the base class for all class
definitionsdefinitions A Class can be thought of as a template or a A Class can be thought of as a template or a
blueprint for a type of Objectblueprint for a type of Object Objects are created by constructors using the Objects are created by constructors using the newnew
operator syntax, and can have operator syntax, and can have propertiesproperties assigned assigned to them dynamicallyto them dynamically
Functions defined in objects are known as Functions defined in objects are known as methodsmethods
var myObject:Object = new Object(); var myObject:Object = new Object(); //create a new object//create a new object
myObject.property = value; myObject.property = value; //property for the object//property for the object
Objects and methodsObjects and methods ActionScript contains a number of ActionScript contains a number of Built-in classes Built-in classes
that are part of the core language, for example: that are part of the core language, for example: Date, Math, Mouse, Array, Keyboard, String, XML Date, Math, Mouse, Array, Keyboard, String, XML etcetc
For example the Math or Date Class/Object have For example the Math or Date Class/Object have methods like:methods like:
var myDate = new Date(); var myDate = new Date(); //date object//date object
myDate.getFullYear(); myDate.getFullYear(); //object method: returns current year//object method: returns current year
//random Numbers//random Numbers
var rnd:int = Math.random(); var rnd:int = Math.random(); //generates number between 0 and 1//generates number between 0 and 1
var rnd:int = Math.random() * 10; var rnd:int = Math.random() * 10; //a number between 0 and 10//a number between 0 and 10
Custom ObjectCustom Object A Custom Object is a A Custom Object is a self-definedself-defined object where we can set object where we can set
up our own set of properties for the objectup our own set of properties for the object A collection of A collection of propertiesproperties describes the object describes the object
Ex. An apple has properties like smell, color, size and Ex. An apple has properties like smell, color, size and positionposition
The object can contain different data typesThe object can contain different data types Positve:Positve: Return the Object (all properties) in one function Return the Object (all properties) in one function
callcall
Example1 How to create a custom object:Example1 How to create a custom object:
var user:Object = new Object(); var user:Object = new Object(); //create the object //create the object
user.name = "Irving"; user.name = "Irving"; //assign properties//assign properties
user.age = 32;user.age = 32;
user.hobby = "Drinking!";user.hobby = "Drinking!";
Custom ObjectsCustom Objects Objects in their basic form can be used as Objects in their basic form can be used as
Associative Arrays Associative Arrays that contain key-value pairs, that contain key-value pairs, where keys are Strings and values may be any where keys are Strings and values may be any typetype
A Custom Object can also be created by A Custom Object can also be created by assigning an object literal, as in the following assigning an object literal, as in the following example:example:
Example2 create a custom object:Example2 create a custom object:
//create the object and assign some properties//create the object and assign some properties
var user:Object = {name:"Irving", age:32, hobby:"Drinking!"};var user:Object = {name:"Irving", age:32, hobby:"Drinking!"};
Custom Objects & Custom Objects & functionsfunctions
Return the Object (all properties) in one function Return the Object (all properties) in one function callcall
//create the object and assign some properties//create the object and assign some properties
var user:Object = {name:"Irving", age:32, hobby:"Drinking!"};var user:Object = {name:"Irving", age:32, hobby:"Drinking!"};
//function that outputs the user data//function that outputs the user data
function getUserData(obj:Object):void{function getUserData(obj:Object):void{
trace(obj.name);trace(obj.name);
trace(obj.age);trace(obj.age);
trace(obj.hobby); trace(obj.hobby);
}}
//function call//function call
getUserData(user); getUserData(user); //send the user object as a parameter //send the user object as a parameter
Custom Objects & LoopsCustom Objects & Loops We can also loop through a Custom Object and get all We can also loop through a Custom Object and get all
the properties like:the properties like:
//set up the object with properties//set up the object with properties
var myObject:Object = {firstName:"Tara", age:27, city:"San Francisco"};var myObject:Object = {firstName:"Tara", age:27, city:"San Francisco"};
//loop through the object//loop through the object
for (var prop:String in myObject){ for (var prop:String in myObject){ //prop string variable//prop string variable
trace("myObject."+prop+" = "+myObject[prop]); trace("myObject."+prop+" = "+myObject[prop]);
} }
/* output/* output
myObject.firstName = Tara myObject.firstName = Tara
myObject.age = 27 myObject.age = 27
myObject.city = San FranciscomyObject.city = San Francisco
*/*/