ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Lists Introduction to Computing Science and Programming I.
Python November 14, Unit 7. Python Hello world, in class.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.
Introduction to JavaScript for Python Programmers
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Hello AP Computer Science!. What are some of the things that you have used computers for?
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Dear Power point User, This power point will be best viewed as a slideshow. At the top of the page click on slideshow, then click from the beginning.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
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.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2012SCM-CityU1.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
PHP Constructs Advance Database Management Systems Lab no.3.
ActionScript: Addressing Display Objects, Duplicating MovieClips, Attaching MovieClips Dynamically MMP 220 Multimedia Programming This material was prepared.
ActionScript: Loading External Content with MovieClipLoader Class, Listener Objects, Preloaders, HitTest, Loading Sound MMP 220 Multimedia Programming.
ActionScript: Functions, Parameters, Functions that return a value, Variables, setInterval Functions MMP 220 Multimedia Programming This material was prepared.
Today’s Announcements Assignment 8 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Classes, Objects, and World-level Methods MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
ActionScript: Classes, Properties, EventHandler Methods, Datatypes & Control Structures MMP 220 Multimedia Programming This material was prepared for students.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Computer Game Design ActionScript is… Object-oriented programming Everything you do in ActionScript does something to some object* Some objects.
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Language Find the latest version of this document at
Interactive Programming MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum redesign project.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Parameters MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum redesign project funded.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
JavaScript and Ajax (Control Structures) Week 4 Web site:
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
JavaScript, Sixth Edition
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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;
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Programming the Web using XHTML and JavaScript
3rd prep. – 2nd Term MOE Book Questions.
Accumulators in Programming
3rd prep. – 2nd Term MOE Book Questions.
Chapter 19 and Material Adapted from Fluency Text book
Introduction to JavaScript for Python Programmers
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
Coding Concepts (Basics)
Animation Programs: Scenarios and Storyboards
Loops and Arrays in JavaScript
Presentation transcript:

ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia Programming as as part of a curriculum redesign project funded by National Science Foundation grant # Co PI’s Christopher Stein and Jody Culkin BMCC CUNY

Loops Loops allow you to repeat lines of code Writing loops in ActionScript repeats lines of code like in Alice but there are two major differences: –ActionScript Loops aren’t good for animation –In ActionScript you usually use the iterator, but not in Alice

Why Loops aren’t good for Animation in ActionScript One reason you can’t use loops for animation in ActionScript is that the computer runs them too fast. So fast that it usually does everything in the loop in a fraction of a second. (in Alice you can set how long each line of code takes to run) The second reason is that ActionScript doesn’t do anything else while it’s looping. (in Alice you can do other things while a loop is running)

Loop Syntax There are three parts to a Loop –Initialize expression (init): this sets the value of the iterator –Test condition: This is like the test condition in an if statement, if it evaluates to true the loop runs, if not it stops running. It usually tests the iterator. –Next expression: this is done after every loop. It usually adds to (increments) the iterator.

Loop Syntax 2 Here is the basic syntax for (init; condition; next) { statement(s); } Here is an example (will loop 10 times) for (var i:Number = 0; i < 10; i++) { trace( i ); } This examples traces the numbers from 0 to 9.

The Problem Sometimes it helps to think of programming as problem solving. There is something you want to do and you have to figure out the solution to make it work. The problem Loops solve is repeating similar lines of code over and over. See the next page for an example.

The Problem: an Example Let’s say you want to move 7 movie clips 50 pixels down: clip1_mc._y += 50; clip2_mc._y += 50; clip3_mc._y += 50; clip4_mc._y += 50; clip5_mc._y += 50; clip6_mc._y += 50; clip7_mc._y += 50; That’s a lot of very similar code.

The Problem: A Solution Here’s how a loop would do the same thing: for ( var i:Number = 1; i <= 7; i++){ this[“clip” + i + “_mc”]._y += 50; } A lot less code. But some new concepts. Let’s look at them.

Concept 1: Concatenation Concatenation means adding strings of text together to get one longer string. You use the + operator. It is usually done when you want to create a string with variable values. Example Var myString:String = “New York”; trace(“Hello “ + myString);// traces Hello New York myString = “World”; //change the variable trace(“Hello “ + myString); //traces Hello World So “clip” + i + “_mc” would be clip1_mc if i = 1.

Concept 2: This The keyword this is a way to refer to the object the code you’re writing runs in. We will explain it in more detail in the future. For now all you need to understand is that it refers to the main movie.

Concept 3: Square Brackets [] Technically these are called array access operators. They are mainly used in two ways. One is with Arrays which we will see later. The other is to let you dynamically create movie clip names. For example instead of writing myClip1_mc._alpha = 50; You could write: this[“myClip” + i + “_mc”]._alpha = 50; And then by changing the value of i you could change which clip you meant.

The Problem: Part 2  Let’s say you wanted to loop through a bunch of clips but they weren’t all named the same like, clip1, clip2, clip3 etc. How do you do it without writing a separate line of code for each clip?  Here are the names of the clips. You want to set them all to have an alpha of 0 so they disappear. square_mc, circle_mc, triangle_mc, octagon_mc

The Problem: Part 2 Solution We solve the problem by putting the clips in an array and then using a for..in statement var clipArray:Array = new Array(square_mc, circle_mc, triangle_mc, octagon_mc); for( var index in clipArray){ clipArray[index]._alpha = 0; } This uses another new concept, the Array

Concept 4: Arrays A normal variable only allows you to store one value at a time. Arrays allow you to store multiple values in a single name. To keep the values separate arrays number each value. The number is called the index. The first value always gets an index of 0 (not 1 like you might think). If this is my array: –Var myArray:Array = new Array(“larry”, “curly”, “moe”); To trace the value of the second item, “curly,” you would write: –Trace( myArray[1] ); This is not a typo, you use the number 1 because the numbering starts at 0. Notice this is the second way we use the brackets. There is no space after the name of the array and the index number is inside the brackets.

Assignment Read Chapter 7 of the ActionScript Bible on Arrays Create a movie that uses at least two loops to change the properties of a group of movie clips in some way. (like the examples we showed in class)