Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ARTDM 170, Week10: Arrays + Using Randomization

0 views

Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

ARTDM 170, Week10: Arrays + Using Randomization

  1. 1. ARTDM 170, Week 10: Arrays and Math <ul><li>Gilbert Guerrero [email_address] gilbertguerrero.com/blog/artdm-170 </li></ul>
  2. 2. Arrays
  3. 3. What is an Array? <ul><li>Arrays group data together in a single element </li></ul><ul><li>Can be thought of as a variable that stores variables var arrayName :Array = new Array(); </li></ul>
  4. 4. Storing and Retrieving <ul><li>Arrays can be used to store data var fruits :Array = new Array(); fruits = [“apples”,“oranges”,“bananas”]; </li></ul><ul><li>Data can be retrieved from arrays using the index numbers trace( fruits[0] ); //apples trace( fruits[1] ); //oranges trace( fruits[2] ); //bananas // I like to eat apples and oranges. trace( &quot; I like to eat &quot;+ fruits[0] +&quot; and &quot;+ fruits[1] +&quot; . &quot; ); </li></ul>
  5. 5. Arrays and Objects <ul><li>Arrays can be used to store objects var redBall :Ball = new Ball(); var orangeBall :Ball = new Ball(); var yellowBall :Ball = new Ball(); var ballArray :Array = new Array(); ballArray = [redBall, orangeBall, yellowBall]; </li></ul>
  6. 6. Arrays and Objects <ul><li>Instead of using the variable names for each ball, we can use the array ballArray = [redBall, orangeBall, yellowBall]; ballArray [0].x = 100; ballArray [0].y = 100; ballArray [1].x = 200; ballArray [1].y = 100; ballArray [2].x = 300; ballArray [2].y = 100; </li></ul>
  7. 7. for() loops <ul><li>for() statement allows you to repeat an operation a certain number of times </li></ul><ul><ul><li>for(var i:int=0; i < N; i++) { trace(i); } </li></ul></ul><ul><li>If N=3, then output would display: </li></ul><ul><ul><li>0 1 2 </li></ul></ul><ul><li>The script runs while i is less than N, but not greater than or equal to it </li></ul>
  8. 8. Using arrays and for loops <ul><li>In the case of our ballArray, we could use a for() statement instead of typing out every line </li></ul><ul><li>for(var i:int=0; i < 3; i++) { ballArray[ i ].x = 100*( i +1); ballArray[ i ].y = 100; } </li></ul><ul><li>The above code would set all the y values to 100. The x values would be 100, 200, and 300. </li></ul>
  9. 9. Creating multiples with arrays <ul><li>We can use a for() loop to create and add the balls to an array </li></ul><ul><ul><li>var ballArray :Array = new Array(); for(var i:int=0; i < 300; i++) { var thisBall :Ball = new Ball(); ballArray.push( thisBall ); ballArray[ i ].x = 50*( i +1); ballArray[ i ].y = 100; } </li></ul></ul><ul><li>In this case we can create hundreds with a few lines of code </li></ul>
  10. 10. Randomization <ul><li>Generate a random number between 0 and .999999 </li></ul><ul><li>Math.random() </li></ul><ul><li>Round down using Math.floor and multiply Math.random by a number to get a random integer between zero and your number </li></ul><ul><li>//generates integers from 0 to 29 </li></ul><ul><li>Math.floor(Math.random()*30) </li></ul>
  11. 11. Randomizing location and velocity <ul><li>Combining for() loops, arrays, and random numbers we can create hundreds of objects and give each one a different location and velocity </li></ul><ul><li>for(var i:int=0; i < 300; i++) { var thisBall :Ball = new Ball(); ballArray.push( thisBall ); ballArray[ i ].x = Math.floor(Math.random()*400); ballArray[ i ].y = Math.floor(Math.random()*500); ballArray[ i ].moveX = Math.floor(Math.random()*20); ballArray[ i ].moveY = Math.floor(Math.random()*20); } </li></ul>
  12. 12. Array Functions <ul><li>Add an item to the end of the array fruits.push(“strawberries”); </li></ul><ul><li>Remove an item from the middle of an array fruits.slice(5,1); </li></ul><ul><li>More array functions: Chapter 5 in AS 3.0 Cookbook </li></ul>
  13. 13. Open Flash <ul><li>Create a new ActionScript 3.0 document: MyAnimation.fla </li></ul><ul><li>Create a new ActionScript file: MyAnimation.as </li></ul>
  14. 14. Script package { import flash.display.*; import flash.events.*; public class MyAnimation extends MovieClip { // Setup the values private var bounce:Number = -0.9; private var gravity:Number = .5; private var oldX:Number; private var oldY:Number; private var N:uint = 3; private var ballArray:Array = new Array(); //Constructor public function MyAnimation() { init(); } //Methods public function init():void { for (var i:uint = 0; i < N; i++){ var thisBall:Ball = new Ball(); ballArray.push(thisBall); ballArray[i].graphics.lineStyle(5,0x000000); ballArray[i].graphics.beginFill(0xCCCCCC); ballArray[i].graphics.drawCircle(0,0,25); ballArray[i].x = Math.floor(Math.random()*500); ballArray[i].y = Math.floor(Math.random()*400); ballArray[i].moveX = Math.floor(Math.random()*20); ballArray[i].moveY = Math.floor(Math.random()*20); addChild(ballArray[i]); } addEventListener(Event.ENTER_FRAME, onMoveCircle); } public function onMoveCircle(pEvent:Event):void { for (var i:int = 0; i < ballArray.length; i++){ ballArray[i].moveY = ballArray[i].moveY + gravity; ballArray[i].x = ballArray[i].x + ballArray[i].moveX; ballArray[i].y = ballArray[i].y + ballArray[i].moveY; if(ballArray[i].x > stage.stageWidth - ballArray[i].width/2 || ballArray[i].x < ballArray[i].width/2) { ballArray[i].moveX = ballArray[i].moveX*bounce; //change direction } if(ballArray[i].y > stage.stageHeight - ballArray[i].height/2 || ballArray[i].y < ballArray[i].height/2) { ballArray[i].moveY = ballArray[i].moveY*bounce; //change direction } } } } }
  15. 15. Homework, due Oct 21 <ul><li>Follow along and complete the Flash Memory Game tutorial </li></ul><ul><li>See you next week! </li></ul>

×