topical media & game development

talk show tell print

animation-ch09-Distance.ax

animation-ch09-Distance.ax [swf] [flash] flex


  package
  {
          import flash.display.Sprite;
          import flash.events.Event;
          
          public class @ax-animation-ch09-Distance extends Sprite
          {
                  private var ball1:animation_ch09_Ball;
                  private var ball2:animation_ch09_Ball;
                  
                  public function @ax-animation-ch09-Distance()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          ball1 = new animation_ch09_Ball();
                          addChild(ball1);
                          ball1.x = stage.stageWidth / 2;
                          ball1.y = stage.stageHeight / 2;
                          
                          ball2 = new animation_ch09_Ball();
                          addChild(ball2);
                          ball2.startDrag(true);
                          
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);                        
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          var dx:Number = ball2.x - ball1.x;
                          var dy:Number = ball2.y - ball1.y;
                          var dist:Number = Math.sqrt(dx * dx + dy * dy);
                          // default ball diameter is 80 (radius is 40)
                          if(dist < 80)
                          {
                                  trace("hit");
                          }
                  }
          }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.