Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming 101 EPGY Middle School Programming Adam Leeper.

Similar presentations


Presentation on theme: "Programming 101 EPGY Middle School Programming Adam Leeper."— Presentation transcript:

1 Programming 101 EPGY Middle School Programming Adam Leeper

2 Variables Algebra: a variable is a name for a value – So X = 3 means that in an expression, you can substitute the number 3 for an X. In computer science, a variable is a name used to store a piece of information.

3 Variables In programming, every variable has a type. In ActionScript 3, some common types are: – Number: a “decimal” number, e.g. 2 or 3.1415 – String: a literal string of text e.g. “1st Street” – int: an integer number, e.g. -5 or 27 – uint: a non-negative integer, e.g. 3 or 100 – Boolean: a value of “true” or “false”.

4 Variables Syntax: var variable_name:TYPE; var variable_name:TYPE = 0; var variable_name:TYPE = new TYPE();

5 Variables Naming rules: NO spaces Must start with a letter Must not be a keyword OK: my_variable, myVariable, ship1, hEaLtH NO: 1stShip, money left, function

6 Functions A function “does something.” – It might need information from you. – It might give you back information. “Real-world” functions – Soda machine: takes money & choice; returns soda – Blender: takes food, returns liquid

7 Functions You often don’t care HOW the function works It’s a “black box” or “function machine” 27 3 9 Multiplier Function

8 Functions Arguments: data you pass to the function Return value: data the function passes back function myFunc( input1:TYPE, input2:TYPE):RETURN_TYPE { // do stuff // if RETURN_TYPE is not void, must return a value return some_variable; }

9 Functions Examples function addTwoNumbers(num1:Number,num2:Number):Number { var result:Number = num1 + num2; return result; } function printName(my_name:String):void { trace(“Hello “, my_name); }

10 Functions You CALL a function with its name and any arguments. You must include parentheses. gotoAndStop(); // returns void mySum = addTwoNumbers( num1, num2 ); //returns Number

11 Style Code must be functional Code must be easy to read This is a bad example: function myFunc(num:Number):void{ var sum:Number=27; sum = sum + num; return sum; }

12 Style Basic rules: – Indent code that belongs to a function or conditional statement function myFunc(num:Number):void { var sum:Number=27; sum = sum + num; return sum; }

13 Style Basic rules: – Include comments to make your intent clear // Adds two numbers and returns the result function sum(num1:Number, num2:Number):void { var sum:Number; // Create a variable sum = num1 + num2; // Add numbers return sum; // return the result }

14 Style Basic rules: – Make you function and variable names descriptive – Easy to use: health_remaining, zombiesKilled, current_panel – Hard to use: hr, zk, cp, foo, bar

15 Style Basic rules: – Put a semicolon (;) at the end of each statement function myFunc(num:Number):void { var sum:Number=27; sum = sum + num; return sum; }

16 Debugging Why do we care about style? – It makes our code easier to debug! – You can often spot a mistake just because the formatting is wrong.

17 Debugging Tips READ the error the compiler gives you! Usually it will TELL you what it doesn’t like Examples: – Duplicate names (variables or function definitions) – Expected } or ; before some other character – Unknown name (you forgot to create a variable) If you double-click on the error in the compiler, it will take you to that line in the code so you can look at it.

18 Debugging Tips put a “trace” command in your code to identify where an error is occurring trace(“Step 1”); var mySum:Number = 27; trace(“Step 2”); myFunction(mySum); trace(“Step3”);

19 Conditional Statements Control program flow by making decisions EVERY decision reduces to true or false if ( some_condition ) { // do some stuff if it’s true } else { // do other stuff if it’s false }

20 Conditional Statements Common comparison operators ==equality !=inequality >greater than < less than >= greater than or equal to <= less than or equal to

21 Conditional Statements Can check if multiple things are true – if( hungry AND lunch_time ) Common logical operators &&“and” ||“or” !“not”

22 Conditional Statements true && true  true true && false  false true || true  true true || false  true !true  false !false  true

23 Conditional Statements Examples if( count > 5 ) //number comparison if( my_name == “Adam”) //string comparison //logical combination of two conditions if( (money > 10) && !(alreadyHave) )

24 Conditional Statements: switch For many conditional checks, you could do if(case1){ //do stuff } else if (case2) { //do stuff } else if (case3){ //do stuff }... else if(case100){ //do stuff } THIS IS NOT VERY EFFICIENT

25 Conditional Statements: switch A better way is to use a switch statement switch ( myNum ) { case (1): // do something if myNum == 1 break; case (2): case (3): // do something if myNum == 2 or 3 break; default: // do something if no case was true }

26 Classes A space ship might be represented by: var id:String = “Red1”; var model:String = “x-wing”; var armor:Number = 200; var fuel:Number = 100;

27 Classes Now what if you want two ships? var id1:String = “Red1”; var model1:String = “x-wing”; var armor1:Number = 200; var fuel1:Number = 100; var id2:String = “Red2”; var model2:String = “x-wing”; var armor2:Number = 200; var fuel2:Number = 100; This could quickly get out of hand...

28 Classes The solution is to use classes A class is a type of variable that acts as a container for many variables and functions. You have already been using classes – MovieClips, buttons and text boxes are examples of classes btn.label = “Start Button” btn.addEventListener(MouseEvent.CLICK, onClick)

29 Classes You instantiate a class the same as a variable You access the members of a class with “.” var classInstanceName:CLASS_TYPE = new CLASS_TYPE(); var loader:URLLoader = new URLLoader; loader.source = “path/to/file.jpg”; var ship1:xWing = new xWing(); ship1.id = “Red Leader” ship1.setFuel(100);

30 Classes We will discuss writing your own classes later. It is helpful to look at documentation to know what variables and functions are available. http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/

31 Writing Classes We create classes in external Actionscript files. We can then make instances of these in our project. A class should be reusable.

32 Writing Classes Common syntax: package { import flash.display.MovieClip; public class Ellipse extends MovieClip { public function Ellipse( arguments:Type ) { // This is the constructor } // Other members or methods (functions) go here. }

33 Writing Classes To make an instance of a class, you call its constructor function, with any arguments. // assume we have a class called Ellipse var my_instance:Ellipse = new Ellipse();

34 Arrays We would like a way of storing lots of similar objects. Better than saying: var student1:String; var student2:String;... var student16:String;

35 Arrays We use an array. It is a list of similar objects. You add objects to the array with push(). You get an object from the array by number. var names_list:Array = new Array(); names_list.push(“Ross”); names_list.push(“Manu”); trace(“The first on the list is “, names_list[0] );

36 Magic Numbers Often we put various numbers in our code. These numbers are HARD to change later. ship1.fuel = 99; ship2.fuel = 99; ship3.fuel = 99; ship4.fuel = 99;

37 Magic Numbers Better to define these CONSTANT VARIABLES. You use the keyword const instead of var. Only need to change one number later. const STARTING_FUEL:Number = 99; ship1.fuel = STARTING_FUEL; ship2.fuel = STARTING_FUEL; ship3.fuel = STARTING_FUEL; ship4.fuel = STARTING_FUEL;


Download ppt "Programming 101 EPGY Middle School Programming Adam Leeper."

Similar presentations


Ads by Google