Your SlideShare is downloading. ×
Artdm170 Week10 Arrays Math
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Artdm170 Week10 Arrays Math

354
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
354
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ARTDM 170, Week 10: Arrays and Math
    • Gilbert Guerrero [email_address] gilbertguerrero.com/blog/artdm-170
  • 2. Arrays
  • 3. What is an Array?
    • Arrays group data together in a single element
    • Can be thought of as a variable that stores variables var arrayName :Array = new Array();
  • 4. Storing and Retrieving
    • Arrays can be used to store data var fruits :Array = new Array(); fruits = [“apples”,“oranges”,“bananas”];
    • 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( " I like to eat "+ fruits[0] +" and "+ fruits[1] +" . " );
  • 5. Arrays and Objects
    • 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];
  • 6. Arrays and Objects
    • 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;
  • 7. for() loops
    • for() statement allows you to repeat an operation a certain number of times
      • for(var i:int=0; i < N; i++) { trace(i); }
    • If N=3, then output would display:
      • 0 1 2
    • The script runs while i is less than N, but not greater than or equal to it
  • 8. Using arrays and for loops
    • In the case of our ballArray, we could use a for() statement instead of typing out every line
    • for(var i:int=0; i < 3; i++) { ballArray[ i ].x = 100*( i +1); ballArray[ i ].y = 100; }
    • The above code would set all the y values to 100. The x values would be 100, 200, and 300.
  • 9. Creating multiples with arrays
    • We can use a for() loop to create and add the balls to an array
      • 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; }
    • In this case we can create hundreds with a few lines of code
  • 10. Randomization
    • Generate a random number between 0 and .999999
    • Math.random()
    • Round down using Math.floor and multiply Math.random by a number to get a random integer between zero and your number
    • //generates integers from 0 to 29
    • Math.floor(Math.random()*30)
  • 11. Randomizing location and velocity
    • Combining for() loops, arrays, and random numbers we can create hundreds of objects and give each one a different location and velocity
    • 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); }
  • 12. Array Functions
    • Add an item to the end of the array fruits.push(“strawberries”);
    • Remove an item from the middle of an array fruits.slice(5,1);
    • More array functions: Chapter 5 in AS 3.0 Cookbook
  • 13. Open Flash
    • Create a new ActionScript 3.0 document: MyAnimation.fla
    • Create a new ActionScript file: MyAnimation.as
  • 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. Homework, due Oct 21
    • Follow along and complete the Flash Memory Game tutorial
    • See you next week!