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

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Introducing JavaScript
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Web Warrior Guide to Web Design Technologies
Chapter 5 Functions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Encapsulation by Subprograms and Type Definitions
B.Sc. Multimedia ComputingMultimedia Authoring Introduction to ActionScript.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Guide To UNIX Using Linux Third Edition
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Object Oriented Software Development
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer
Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is.
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Introduction to Computer Programming
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
A High Flying Overview CS139 – Fall 2006 How far we have come.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Presented by Ted Higgins, SQL Server DBA C Programming - Pointers.
Variables & datatypes Flash ActionScript Introduction to.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
For Loops & Arrays. my_rect_pink_mc.width = 40 my_rect_pink_mc.alpha =.5 trace my_rect_pink_mc.x= 20 my_rect_pink_mc.y= 20 for (var i:int = 0; i < 250;
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
JavaScript Programming. What Is JavaScript “the most popular programming language in the world”— w3schools.com “the language for HTML, the Web, computers,
A High Flying Overview CS139 – Fall 2010 How far we have come.
Javascript Arrays Ch.19. Array definition & for loop var quiz = [85,90,100,0]; // creates an array var ex = []; ex[0] = 89; // add the quiz grades quizTotal.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
CS0004: Introduction to Programming
ActionScript Programming Help
Topic 02: Introduction to ActionScript 3.0
Chapter 7 User-Defined Methods.
CS139 – Fall 2010 How far we have come
Chapter 9: Value-Returning Functions
Presentation transcript:

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

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

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

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

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

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 …

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

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

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

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!";

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!"};

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

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 */