SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.

Slides:



Advertisements
Similar presentations
Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
SM1205 Interactivity Topic 05: Properties and Events Part II Spring 2011SCM-CityU1.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
SM1205 Interactivity Topic 03: Flow Control Spring 2010SCM-CityU1.
Loops – While, Do, For Repetition Statements Introduction to Arrays
SM1205 Interactivity Topic 06: Sound Spring 2010SCM-CityU1.
Information Technology Center Hany Abdelwahab Computer Specialist.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
SM1205 Interactivity Topic 09: Motion Tracking Part I Spring 2010SCM-CityU1.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2012SCM-CityU1.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Getting started with the turtle Find the latest version of this document at
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Controlling Program Flow with Decision Structures.
Multidimensional Arrays Computer and Programming.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
JavaScript, Sixth Edition
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Topic 02: Introduction to ActionScript 3.0
var variableName:datatype;
Topic: Iterative Statements – Part 1 -> for loop
REPETITION CONTROL STRUCTURE
Loop Structures.
Scripts & Functions Scripts and functions are contained in .m-files
While Loops in Python.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
T. Jumana Abu Shmais – AOU - Riyadh
Algorithm Discovery and Design
Loops CIS 40 – Introduction to Programming in Python
String and Lists Dr. José M. Reyes Álamo.
Loops and Arrays in JavaScript
Class code for pythonroom.com cchsp2cs
REPETITION Why Repetition?
Presentation transcript:

SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1

Control Multiple Objects How to control multiple objects with similar behaviors? Spring 2010SCM-CityU2

Control Multiple Objects Possible solution – Assign different names to each object ? – Write same code for all objects? How about too many objects? How to build dynamic objects? We need a more flexible way to access display objects Spring 2010SCM-CityU3

Control Multiple Objects Iterations and arrays is a better solution – Loop and operate on multiple objects – Code once run many – No need to assign different name to all objects First of all we need to understand iteration (looping) in AS 3.0 Spring 2010SCM-CityU4

Iteration Example – output text from 1 to 10 Spring 2010SCM-CityU5 trace(1); trace(2); trace(3); trace(4); trace(5); trace(6); trace(7); trace(8); trace(9); trace(10); Simple … right?

Iteration How about printing numbers from 1 to 100? Copy and paste? – Not a good idea – Long code – Hard to debug Spring 2010SCM-CityU6 trace(1); trace(2); trace(3); trace(4); trace(5); trace(6); trace(7); trace(8); trace(9); trace(10);

Iteration Can we only use fewer lines of code? Yes we can! Spring 2010SCM-CityU7 for (var i:int = 1; i<=10; i++) { trace(i); } Let’s try it out

For Structure The is our first for looping statement – Only 4 lines of code – Can print numbers from 1 to any number – trace() is called for many time but you only need to write once Spring 2010SCM-CityU8 for (var i:int = 1; i<=10; i++) { trace(i); }

For Structure General case of the for structure “for” is the keyword Statements inside the blocks run continuously while the test part evaluates to true Spring 2010SCM-CityU9 for (init; test; update) { // statements }

For Structure General case of the for structure Init part – assigns an initial value to the testing variables used in the test Update part – modifies the value of testing variable AFTER each iteration Spring 2010SCM-CityU10 for (init; test; update) { // statements }

For Structure For structure runs in the following order: 1.The init statement is run 2.The test is evaluated to true of false 3.If the test is true, goto step 4. Otherwise goto step 6 4.Run the statements within the block 5.Run the update statement and goto step 2 6. Exit the structure and continue running the program Spring 2010SCM-CityU11 for (init; test; update) { // statements }

For Structure Example: Spring 2010SCM-CityU12 for (var i:int = 1; i<=3; i++) { trace(i); } var i:int = 1 // 1<=3 is true trace(i); i++; // 2<=3 is true trace(i); i++; // 3<=3 is true trace(i); i++; // 4<=3 is false // exit looping Output: Output: init test loop body update test loop body update test loop body update

Printing Numbers Simple exercise: – Print odd numbers within 1 to 20 – Print even numbers within 1 to 20 – How many solution you can think about? Recall the format of for structure Spring 2010SCM-CityU13 for (init; test; update) { // statements }

Printing Numbers First solution (odd number): – Use an if statement to test each number Spring 2010SCM-CityU14 for (var i:int = 1; i<=20; i++) { if (i%2 == 1) { trace(i); } if i is an odd number print it

For Structure Another solution (odd number): – Use another updating step – Increase variable i by 2 after each iteration – Recall that i+=2 is the same as i = i + 2 – How about printing even number? Spring 2010SCM-CityU15 for (var i:int = 1; i<=20; i+=2 ) { trace(i); }

Nested For Structure Question - How to find the prime numbers? – 2, 3, 5, 7, 11, 13, … Hints – Using nested for structure – For structure is with in another for structure – Similar format to nested if structure – The inner for structure is used to check a number is prime or not – with boolean flag and if statement Spring 2010SCM-CityU16

Nested For Structure Solution - How to find the prime numbers? Spring 2010SCM-CityU17 for (var i:int = 1; i<=20; i++) { var isPrime:Boolean = true; for (var j:int = 2; j<i; j++) { if (i%j == 0) { isPrime = false; } if (isPrime) { trace(i); } starting from 2 to i-1 if i can be divided by j, i is not prime

Using Array Array can store multiple data with one variable name – Can hold any type of data – Each element can be individually read or updated. Example – storing 3 number into one array Spring 2010SCM-CityU18 var a:Array = new Array(); a[0] = 1; a[1] = 2; a[2] = 3; trace(a);

Using Array Array is a complex data type – Use keyword “new” to define an array Storing data – we need an index to specify the location of the slot in the array – Indices are just an integers, starts with zero – The index must put inside the brackets [ and ] Spring 2010SCM-CityU19 a[0] = 1; // assign 1 to a[0] a[1] = 2; // assign 2 to a[1] a[2] = 3; // assign 3 to a[2] var a:Array = new Array();

Using Array Reading data – Same syntax as storing data to array – Use the brackets [ and ] Spring 2010SCM-CityU20 var a:Array = new Array(); a[0] = 1; // assign 1 to a[0] a[1] = 2; // assign 2 to a[1] a[2] = a[0] + a[1]; // a[2] = 1 + 2

Using Array Processing elements in array with for structure – Access and update each element in array – Use the array length as testing condition Example – find the sum of all numbers in an array Spring 2010SCM-CityU21 var sum:int = 0; for (var i:int = 0; i<a.length; i++) { sum += a[i]; } trace(sum);

Control Multiple Objects Example – Swimming ducks – Dynamically create objects (ducks!) – Store and access objects with array and for structure Spring 2010SCM-CityU22

Control Multiple Objects Open duck.fla – You can see nothing in the stage – We are going to create objects and add them to the stage by codes Spring 2010SCM-CityU23

Control Multiple Objects Open duck.fla – You can see nothing in the stage – We are going to create objects and add them to the stage by codes Spring 2010SCM-CityU24

Control Multiple Objects Check the property of the “Symbol Duck” Enable the options – Export for ActionScript – Export in frame 1 This allows us to create duck object in AS Spring 2010SCM-CityU25

Control Multiple Objects Open Action Panel Insert the code below to create the ducks Spring 2010SCM-CityU26 stop(); // create array var ducks:Array = new Array(); // create ducks for (var i:int = 0; i<5; i++) { var d:SymbolDuck = new SymbolDuck(); d.x = 400;// set position of duck d.y = i * 60; addChild(d);// add ducks into stage ducks[i] = d;// add ducks into array }

Control Multiple Objects Result New function: addChild(obj) – Add display object to the current stage Spring 2010SCM-CityU27

Control Multiple Objects Insert the code below to move the ducks Spring 2010SCM-CityU28 // create new timer var timer:Timer = new Timer(20); timer.start(); // timer event listener timer.addEventListener(TimerEvent.TIMER, onTimer); function onTimer(evt:TimerEvent):void { // move all ducks to left for (var i:int=0; i<ducks.length; i++) { ducks[i].x -= Math.random() * 4; }

Control Multiple Objects Result New function – Math.random() – Return random number within 0.0 and 1.0 Spring 2010SCM-CityU29

Control Multiple Objects Exercise 1.Make the ducks enter the stage from right side when they go outside of the stage 2.Add code to allow user to click and add a new duck (using the Array.Push() function) 3.Remove ducks when they go outside of stage (using the removeChild() function) Spring 2010SCM-CityU30