1 / 30

Scripting for Interactive Media

Scripting for Interactive Media. Declaring and Using Arrays. Agenda. Why Use an Array Array Declaration in ActionScript Searching and Sorting an Array The for - next loop Parallel Arrays Array Access Errors Tutorial Example AFI Movies. Need to Store Five Numbers.

Download Presentation

Scripting for Interactive Media

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scripting for Interactive Media Declaring and Using Arrays

  2. Agenda • Why Use an Array • Array Declaration in ActionScript • Searching and Sorting an Array • The for - next loop • Parallel Arrays • Array Access Errors • Tutorial Example AFI Movies

  3. Need to Store Five Numbers Use individual variables for each number stored: var numberOne:number var numberTwo:number var numberThree:number var numberFour:number var numberFive:number messy - have to process as individual items rather than as a collection of grouped items. Solution - use an Array - contiguous data storage

  4. Arrays and Data Storage Elements Arrays store values in ‘elements’ (compartments) i.e. contiguous memory locations 0 15 1 95 The value stored at array element number 2 is 14 2 14 3 70 23 4

  5. Arrays and Data Storage Elements Array element numbering starts at 0 0 15 1 95 The value stored at array element number 4 is 23 2 14 The array has 5 elements 0..4 3 Thus the size of the array is 5 70 23 4

  6. Arrays and Data Types Gibson 15 Les Paul 23 An Array ofnumbers (integers) Array of strings Martin 14 Fender 70 5 Aria

  7. Declaring Arrays in ActionScript // create a new array varnumberArray = new Array(); // store the numbers into the array // 15 95 14 70 23 numberArray[0] = 15; numberArray[1] = 95; numberArray[2] = 14; numberArray[3] = 70; numberArray[4] = 23;

  8. Accessing Array Elements // iterate through the array for (var index = 0; index <= 4; index++) { trace (nameArray[index]); } // outputs eachname in the Name Array

  9. The for – next Loop // loops for a set number of times for (start; condition; increment) { // do somethinghere }

  10. Typical Aspects of using Arrays • Search an array for a given value • Sort an array in a given order • Find the size of the array • Use the length method in searching algorithms • Compare the contents of one array with another array

  11. Summing the Values of an Array // total the numbers stored in the number array varindex:Number; varsum:Number; sum = 0; for (index = 0; index <= 4; index = index + 1){ sum = sum + numberArray[index] } trace("The sum of the numbers in the array is " + sum)

  12. Summing the Values of an Array 2 // total the numbers stored in the number array using array // length method varindex:Number; varsum:Number; sum = 0; for (index = 0; index <= numberArray.length; index++){ sum = sum + numberArray[index] } trace("The sum of the numbers in the array is " + sum)

  13. Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size. for (index = 0; index <=5; index++) The array has only five elements, but the code is attempting to access element number six (5), which does not exist. The computer speak is the code has ‘fallen off the end of the array’

  14. Parallel Arrays name email jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com ben@uwe.ac.uk Ben Freda freda@unity.com

  15. Parallel Array Search name email Search on name jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com Extract email on name ben@uwe.ac.uk Ben Freda freda@unity.com

  16. Parallel Array Search name email Search on name jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com Find occurrences on name of email ben@uwe.ac.uk Ben Freda freda@unity.com

  17. Arrays of Data (sounds) footsteps.mp3 shell.mp3 Array of .mp3 audio files siren.mp3 alarm.mp3 ambience.mp3

  18. Summary • Arrays are a fundamental structured data type in any programming language • Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data • Typical array access is to search and sort the contents • Associated data may be held in parallel arrays and use cross referencing to extract the required information • Algorithms that manipulate arrays must stay within the array bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.

  19. Tutorial Example AFI Movies The first of the American Film Institute (AFI) 100 Years series of cinematic milestones. AFI's 100 Years…100 films is a list of the 100 best American films, as determined by the American Film Institute from a poll of more than 1,500 artists and leaders in the film industry who chose from a list of 400 nominated films. The 100-best list was unveiled in 1998.

  20. Parallel Arrays filmList and userRating filmList userRating Citizen Kane 7 6 Casablanca 8 The Godfather Gone with the Wind 3 9 Lawrence of Arabia

  21. Parallel Arrays filmList and userRating element 0 not used Citizen Kane 7 1 6 2 Casablanca 3 8 The Godfather Gone with the Wind 3 4 9 5 Lawrence of Arabia

  22. AFI Movies Array- filmList var filmList:Array = new Array(); filmList[1] = "Citizen Kane"; filmList[2] = "Casablanca"; filmList[3] = "The Godfather"; filmList[4] = "Gone with the Wind"; filmList[5] = "Lawrence of Arabia"; . . filmList[99]= "Guess Who's Coming to Dinner"; filmList[100]= "Yankee Doodle Dandy";

  23. AFI Movies Array- userRating var userRating:Array = new Array(); userRating[1] = 7; userRating[2] = 6; userRating[3] = 8; userRating[4] = 3; userRating[5] = 9; . . userRating[99] = 2; userRating[100] = 7;

  24. Index Loop Variable and Array Initialization var index:int // loop variable for the for loop index // two functions to initialize the data setFilms();// initializes the film list setRating(); initializes the user-ratings

  25. Output all the films in the filmList Array // output all the films in the filmList array for(index=1; index <= 100; index++){ trace(filmList[index]); }

  26. Output all the films and show the user rating // output all the films in the array // and show the user rating for(index=1; index <= 100; index++){ trace(filmList[index] + " " + userRating[index]); } }

  27. Output all the films with a user rating greater than 7 // output all the films where userRating > 7 for(index=1; index <= 100; index++){ if(userRating[index] > 7){ trace(filmList[index] + " " + userRating[index]); } }

  28. Output all the films with a user-rating of 5 // output all the films where userRating = 5 for(index=1; index <= 100; index++){ if(userRating[index] == 5){ trace(filmList[index] + " " + userRating[index]); } }

  29. Watch Out for a Logical Error! correct if(userRating[index] == 5)comparison incorrect if(userRating[index] = 5)assignment this will output all the movies!

  30. Tutorial with Working Code for you to Modify // use the these lecture slides to carry out the exercises // e.g. output all the films in the filmList array for(index=1; index <= 100; index++){ // put your code here }

More Related