/ProjetCar/src/apps/markerBall/Ball.as

https://github.com/projetI10/projetI10
ActionScript | 82 lines | 67 code | 13 blank | 2 comment | 9 complexity | cad699377db277eb80c9cf71f03db7d5 MD5 | raw file
  1. package apps.markerBall {
  2. import flash.display.MovieClip;
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.geom.Point;
  6. import flash.geom.Rectangle;
  7. public class Ball extends Sprite {
  8. public static const BALL_MISSED:String = "ballMissed";
  9. public static const BALL_SCORED:String = "ballScored";
  10. private var gfx:MovieClip;
  11. private var _colorId:uint;
  12. private var _speed:Point;
  13. private var bounds:Rectangle;
  14. private var redirected:Boolean = false;
  15. public function Ball (gfx:MovieClip, colorId:uint, speed:Point, bounds:Rectangle) {
  16. this.gfx = gfx;
  17. this.addChild(this.gfx);
  18. this._colorId = colorId;
  19. this._speed = speed;
  20. this.bounds = bounds;
  21. this.gfx.gotoAndStop(this._colorId + 1);
  22. }
  23. public function update () :Boolean {
  24. this.x += this._speed.x;
  25. this.y += this._speed.y;
  26. return this.checkForEdges();
  27. }
  28. public function redirect (ang:Number) :void {
  29. // bounce only once
  30. if (this.redirected) { return; }
  31. this.redirected = true;
  32. this.alpha = 0.5;
  33. var speedLength:Number = this.speed.length;
  34. this.speed.x = speedLength * Math.cos(ang);
  35. this.speed.y = speedLength * Math.sin(ang);
  36. }
  37. public function kill (bScore:Boolean) :void {
  38. this.removeChild(this.gfx);
  39. this.gfx = null;
  40. this.dispatchEvent(new Event(bScore ? BALL_SCORED : BALL_MISSED));
  41. }
  42. public function get colorId () :uint {
  43. return this._colorId;
  44. }
  45. public function get speed () :Point {
  46. return this._speed;
  47. }
  48. private function checkForEdges () :Boolean {
  49. // returns true if still within edges, else returns false
  50. if (this.y < this.bounds.top) {
  51. this.kill(this.colorId == 0);
  52. return false;
  53. }
  54. if (this.x > this.bounds.right) {
  55. this.kill(this.colorId == 1);
  56. return false;
  57. }
  58. if (this.y > this.bounds.bottom) {
  59. this.kill(this.colorId == 2);
  60. return false;
  61. }
  62. if (this.x < this.bounds.left) {
  63. this.kill(this.colorId == 3);
  64. return false;
  65. }
  66. return true;
  67. }
  68. }
  69. }