ARTDM 170, Week 11: User Interaction Gilbert Guerrero [email_address] gilbertguerrero.com/blog/artdm-170
Turn in Homework Please put your files in a folder with your last name and first initial Put the folder in my dropbox Example: smith-h Memory-Game.swf
Collision Detection
hitTestObject When the bounding box of one object overlaps with the bounding box of another object, a collision is detected Use an if() statement to do something with the collision if( sprite1.hitTestObject(sprite2) ){    // collision happened   }   sprite1 sprite2
hitTestPoint Check if the edge of a shape is near a point Cannot be used with two shapes, only a shape and a point Can be used with the mouse, using mouseX and mouseY sprite1 if( sprite1.hitTestPoint(x1, y1, true) ){   // collision happened   }
Collision based on distance Detect when two objects are near each other Collide when the distance between their registration points is less than half their combined sizes sprite1 sprite2 sprite1 sprite2
Pythagorean Theorem To find the distance between two points we need the Pythagorean Theorem A 2 +  B 2 =  C 2 A B C
Pythagorean Theorem In ActionScript: dx = x2 - x1; dy = y2 - y1; dist = Math.sqrt(dx*dx + dy*dy); (x1,y1) dist (x2,y2) (x1,y2) dx dy
Collision based on distance var sizeBetween =    sprite1.width/2 + sprite2.width/2; if(dist < sizeBetween) {   // collision detected } sprite1 sprite2
Point.distance There’s a function for this!  var sizeBetween =    sprite1.width/2 + sprite2.width/2;   if(  Point.distance (   new Point (sprite1.x, sprite1.y),   new Point (sprite2.x, sprite2.y)   ) < sizeBetween ) {   // collision detected } You must import the flash.geom.Point class at the top of your ActionScript after the package declaration.
Hit the Brick example
Pop the Balloon example
User Interaction keys and spacebar
Listen for Keyboard  stage.addEventListener(   KeyboardEvent.KEY_DOWN,    keyDownFunction); stage.addEventListener(   KeyboardEvent.KEY_UP,    keyUpFunction);
Checking which key // key pressed down public function keyDownFunction(event:KeyboardEvent) {   if( event.keyCode == 37 ) {   leftArrow = true;   } else if( event.keyCode == 39 ) {   rightArrow = true;   } else if( event.keyCode == 32 ) {   jumpUp = true;   } }
Checking which key // key lifted up public function keyUpFunction(event:KeyboardEvent){   if( event.keyCode == 37 ){   leftArrow = false;   }else if( event.keyCode == 39 ){   rightArrow = false;   } }
Key codes Spacebar 32 Left arrow 37 Up arrow 38 Right arrow 39 Down arrow 40 More  key codes  and  constants  on Adobe’s site
Moving an object // move to the left if (leftArrow) {   myBrick.x -=  speed ; } This would allow the user to move the object to the left at a certain  speed  every  frame .
Time-based animation Using time is more accurate than frames  We can capture how much time has passed and multiply it by the speed  var timePassed:int =    getTimer() - lastTime; lastTime += timePassed; if (leftArrow) {   myBrick.x -=  speed*timePassed/1000 ; }
Jumping script if( jumpUp ) {   jumpVelocity =    jumpVelocity - gravity*timePassed/1000;   myBrick.y -= jumpVelocity;   if( myBrick.y >= oldBrickY ) {   jumpVelocity = 40;   myBrick.y = oldBrickY;   jumpUp = false;   } }
Homework, due Nov 4 Create the  Start  and  Game Over  screens in Flash for your final project Make an encapsulated movie Start screen must have a start button that takes the user to the second frame Second frame must have some trigger that takes the user to the Game Over screen

ARTDM 170, Week 11: User Interaction

  • 1.
    ARTDM 170, Week11: User Interaction Gilbert Guerrero [email_address] gilbertguerrero.com/blog/artdm-170
  • 2.
    Turn in HomeworkPlease put your files in a folder with your last name and first initial Put the folder in my dropbox Example: smith-h Memory-Game.swf
  • 3.
  • 4.
    hitTestObject When thebounding box of one object overlaps with the bounding box of another object, a collision is detected Use an if() statement to do something with the collision if( sprite1.hitTestObject(sprite2) ){ // collision happened } sprite1 sprite2
  • 5.
    hitTestPoint Check ifthe edge of a shape is near a point Cannot be used with two shapes, only a shape and a point Can be used with the mouse, using mouseX and mouseY sprite1 if( sprite1.hitTestPoint(x1, y1, true) ){ // collision happened }
  • 6.
    Collision based ondistance Detect when two objects are near each other Collide when the distance between their registration points is less than half their combined sizes sprite1 sprite2 sprite1 sprite2
  • 7.
    Pythagorean Theorem Tofind the distance between two points we need the Pythagorean Theorem A 2 + B 2 = C 2 A B C
  • 8.
    Pythagorean Theorem InActionScript: dx = x2 - x1; dy = y2 - y1; dist = Math.sqrt(dx*dx + dy*dy); (x1,y1) dist (x2,y2) (x1,y2) dx dy
  • 9.
    Collision based ondistance var sizeBetween = sprite1.width/2 + sprite2.width/2; if(dist < sizeBetween) { // collision detected } sprite1 sprite2
  • 10.
    Point.distance There’s afunction for this! var sizeBetween = sprite1.width/2 + sprite2.width/2; if( Point.distance ( new Point (sprite1.x, sprite1.y), new Point (sprite2.x, sprite2.y) ) < sizeBetween ) { // collision detected } You must import the flash.geom.Point class at the top of your ActionScript after the package declaration.
  • 11.
  • 12.
  • 13.
  • 14.
    Listen for Keyboard stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownFunction); stage.addEventListener( KeyboardEvent.KEY_UP, keyUpFunction);
  • 15.
    Checking which key// key pressed down public function keyDownFunction(event:KeyboardEvent) { if( event.keyCode == 37 ) { leftArrow = true; } else if( event.keyCode == 39 ) { rightArrow = true; } else if( event.keyCode == 32 ) { jumpUp = true; } }
  • 16.
    Checking which key// key lifted up public function keyUpFunction(event:KeyboardEvent){ if( event.keyCode == 37 ){ leftArrow = false; }else if( event.keyCode == 39 ){ rightArrow = false; } }
  • 17.
    Key codes Spacebar32 Left arrow 37 Up arrow 38 Right arrow 39 Down arrow 40 More key codes and constants on Adobe’s site
  • 18.
    Moving an object// move to the left if (leftArrow) { myBrick.x -= speed ; } This would allow the user to move the object to the left at a certain speed every frame .
  • 19.
    Time-based animation Usingtime is more accurate than frames We can capture how much time has passed and multiply it by the speed var timePassed:int = getTimer() - lastTime; lastTime += timePassed; if (leftArrow) { myBrick.x -= speed*timePassed/1000 ; }
  • 20.
    Jumping script if(jumpUp ) { jumpVelocity = jumpVelocity - gravity*timePassed/1000; myBrick.y -= jumpVelocity; if( myBrick.y >= oldBrickY ) { jumpVelocity = 40; myBrick.y = oldBrickY; jumpUp = false; } }
  • 21.
    Homework, due Nov4 Create the Start and Game Over screens in Flash for your final project Make an encapsulated movie Start screen must have a start button that takes the user to the second frame Second frame must have some trigger that takes the user to the Game Over screen