topical media & game development

talk show tell print

animation-ch14-PlayBall.ax

animation-ch14-PlayBall.ax [swf] [flash] flex


  package {
   import flash.display.Sprite;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import flash.events.Event;
   import flash.geom.Point;
  
   public class @ax-animation-ch14-PlayBall extends Sprite
   {
    private var segments:Array;
    private var numSegments:uint = 6;
    private var gravity:Number = 0.5;
    private var bounce:Number = -0.9;
    private var ball:animation_ch14_Ball;
    
    public function @ax-animation-ch14-PlayBall()
    {
     init();
    }
  

init(s)


    private function init():void
    {
     stage.align = StageAlign.TOP_LEFT;
     stage.scaleMode = StageScaleMode.NO_SCALE;
     ball = new animation_ch14_Ball(20);
     ball.vx = 10;
     addChild(ball);
     segments = new Array();
     for(var i:uint = 0; i < numSegments; i++)
     {
      var segment:animation_ch14_Segment = new animation_ch14_Segment(50, 10);
      addChild(segment);
      segments.push(segment);
     }
     segment.x = stage.stageWidth / 2;
     segment.y = stage.stageHeight;
     addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
   

frame(s)


    private function onEnterFrame(event:Event):void
    {
     moveBall();
     var target:Point = reach(segments[0], ball.x, ball.y);
     for(var i:uint = 1; i < numSegments; i++)
     {
      var segment:animation_ch14_Segment = segments[i];
      target = reach(segment, target.x, target.y);
     }
     for(i = numSegments - 1; i > 0; i--)
     {
      var segmentA:animation_ch14_Segment = segments[i];
      var segmentB:animation_ch14_Segment = segments[i - 1];
      position(segmentB, segmentA);
     }
     checkHit();
    }
   

reach


    private function reach(segment:animation_ch14_Segment, xpos:Number, ypos:Number):Point
    {
     var dx:Number = xpos - segment.x;
     var dy:Number = ypos - segment.y;
     var angle:Number = Math.atan2(dy, dx);
     segment.rotation = angle * 180 / Math.PI;
     
     var w:Number = segment.getPin().x - segment.x;
     var h:Number = segment.getPin().y - segment.y;
     var tx:Number = xpos - w;
     var ty:Number = ypos - h;
     return new Point(tx, ty);
    }
   

position


    private function position(segmentA:animation_ch14_Segment, segmentB:animation_ch14_Segment):void
    {
     segmentA.x = segmentB.getPin().x;
     segmentA.y = segmentB.getPin().y;
    }
  
  

move(s)


    private function moveBall():void
    {
     ball.vy += gravity;
     ball.x += ball.vx;
     ball.y += ball.vy;
     if(ball.x + ball.radius > stage.stageWidth)
     {
      ball.x = stage.stageWidth - ball.radius;
      ball.vx *= bounce;
     }
     else if(ball.x - ball.radius < 0)
     {
      ball.x = ball.radius;
      ball.vx *= bounce;
     }
     if(ball.y + ball.radius > stage.stageHeight)
     {
      ball.y = stage.stageHeight - ball.radius;
      ball.vy *= bounce;
     }
     else if(ball.y - ball.radius < 0)
     {
      ball.y = ball.radius;
      ball.vy *= bounce;
     }
    }
  

hit(s)?


    
    public function checkHit():void
    {
     var segment:animation_ch14_Segment = segments[0];
     var dx:Number = segment.getPin().x - ball.x;
     var dy:Number = segment.getPin().y - ball.y;
     var dist:Number = Math.sqrt(dx * dx + dy * dy);
     if(dist < ball.radius)
     {
      ball.vx += Math.random() * 2 - 1;
      ball.vy -= 1;
     }
    }
   }
  }
  


(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.