Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions & Objects Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Similar presentations


Presentation on theme: "Functions & Objects Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"— Presentation transcript:

1 Functions & Objects Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer thomas.lovgren@humlab.umu.se

2 Functions Functions are blocks of code that carry out specific tasks and can be reused Functions are blocks of code that carry out specific tasks and can be reused Your function name should be unique and describe the value the function returns (same rules for namegiving as for variables) Your function name should be unique and describe the value the function returns (same rules for namegiving as for variables) A function can be called at any time by a function call A function can be called at any time by a function call Functions can take parameters Functions can take parameters A function should carry/have just one specific task A function should carry/have just one specific task If a function does not return a value, it’s return type should be void If a function does not return a value, it’s return type should be void Tip! If you often write similar code segments, a function could save time and space

3 Function syntax Example of the different parts and structure of a function declaration/contents Example of the different parts and structure of a function declaration/contents

4 Function The structure of a user defined function (two ways): The structure of a user defined function (two ways): //example 1 function functionName([parameter0,..parameterN]):returnType{ // statement(s) } //example 2 functionName = function ([parameter0,..parameterN]):returnType{ // statement(s) } //example of a basic function function showMsg():void{ trace("I love ActionScript3!"); //output ”I Love ActionScript3!” } showMsg(); //function call

5 Function with parameters (1/2) This function takes two parameters, calculates and return the area This function takes two parameters, calculates and return the area //set up the function function getArea(x:Number, y:Number):Number{ //return type:Number return x * y; //calculate and return the area } getArea(100, 25); //function call, send parameters

6 Function with parameters (2/2) This function takes an Array as a parameter, loop through the Array and make a trace output of the data: This function takes an Array as a parameter, loop through the Array and make a trace output of the data: var length_array = new Array(); //declare the array length_array = [72, 75, 89, 92, 65, 79]; //assign values to the array //function that takes an array function printSubjectData(subjectArray:Array):void{ for(var i:int = 0; i < subjectArray.length; i++){ //for loop trace(i + ":" + subjectArray[i]); //output }} printSubjectData(length_array); //function call Output 0:72 1:75 2:89 3:92 etc …

7 Functions & scope Two examples: We make a function call for the calculate function and trace the output like: Two examples: We make a function call for the calculate function and trace the output like: //example 1 function calculate():Number{ var num1:Number = 50; var num2:Number = 100; var myResult:Number; myResult = num1 + num2; } trace(myResult); //THIS WILL GENERATE AN ERROR! ------------------------------------------------------------------------------------------------------- //example 2 var myResult:Number; function calculate ():Number { var num1:Number = 50; var num2:Number = 100; myResult = num1 + num2; } trace(myResult); //THIS WILL NOT GENERATE AN ERROR

8 Objects In ActionScript 3.0, every object is defined by a class In ActionScript 3.0, every object is defined by a class The Object class serves as the base class for all class definitions The Object class serves as the base class for all class definitions A Class can be thought of as a template or a blueprint for a type of Object A Class can be thought of as a template or a blueprint for a type of Object Objects are created by constructors using the new operator syntax, and can have properties assigned to them dynamically Objects are created by constructors using the new operator syntax, and can have properties assigned to them dynamically Functions defined in objects are known as methods Functions defined in objects are known as methods var myObject:Object = new Object(); //create a new object myObject.property = value; //property for the object

9 Objects and methods ActionScript contains a number of Built-in classes that are part of the core language, for example: Date, Math, Mouse, Array, Keyboard, String, XML etc ActionScript contains a number of Built-in classes that are part of the core language, for example: Date, Math, Mouse, Array, Keyboard, String, XML etc For example the Math or Date Class/Object have methods like: For example the Math or Date Class/Object have methods like: var myDate = new Date(); //date object myDate.getFullYear(); //object method: returns current year //random Numbers var rnd:int = Math.random(); //generates number between 0 and 1 var rnd:int = Math.random() * 10; //a number between 0 and 10

10 Custom Object A Custom Object is a self-defined object where we can set up our own set of properties for the object A Custom Object is a self-defined object where we can set up our own set of properties for the object A collection of properties describes the object A collection of properties describes the object Ex. An apple has properties like smell, color, size and position The object can contain different data types The object can contain different data types Positve: Return the Object (all properties) in one function call Positve: Return the Object (all properties) in one function call Example1 How to create a custom object: var user:Object = new Object(); //create the object user.name = "Irving"; //assign properties user.age = 32; user.hobby = "Drinking!";

11 Custom Objects Objects in their basic form can be used as Associative Arrays that contain key-value pairs, where keys are Strings and values may be any type Objects in their basic form can be used as Associative Arrays that contain key-value pairs, where keys are Strings and values may be any type A Custom Object can also be created by assigning an object literal, as in the following example: A Custom Object can also be created by assigning an object literal, as in the following example: 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!"};

12 Custom Objects & functions Return the Object (all properties) in one function call Return the Object (all properties) in one function call //create the object and assign some properties var user:Object = {name:"Irving", age:32, hobby:"Drinking!"}; //function that outputs the user data function getUserData(obj:Object):void{ trace(obj.name);trace(obj.age);trace(obj.hobby);} //function call getUserData(user); //send the user object as a parameter

13 Custom Objects & Loops We can also loop through a Custom Object and get all the properties like: We can also loop through a Custom Object and get all the properties like: //set up the object with properties var myObject:Object = {firstName:"Tara", age:27, city:"San Francisco"}; //loop through the object for (var prop:String in myObject){ //prop string variable trace("myObject."+prop+" = "+myObject[prop]); trace("myObject."+prop+" = "+myObject[prop]);} /* output myObject.firstName = Tara myObject.age = 27 myObject.city = San Francisco */


Download ppt "Functions & Objects Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"

Similar presentations


Ads by Google