Motion tween

Submitted by GROL on 22 September, 2010 - 21:33
Main motion tween demo

Sample of creating frame-by-frame animation using Motion features. Both creation of separate samples with adjustment of all the parameters and auto-creation of main options are possible. 
 

Source archive: motion_tween.zip (0.93Mb)

 

Sample works

Some sample source: 
procedure TForm1.bByOptionsClick(Sender: TObject);
var
  Movie: TFlashMovie;
  MotionDuration, il: integer;
  P0, P1, P2, P3: TPoint;
 
begin
  MotionDuration := udDuration.Position;
  P0 := Point(50, 300);
  P3 := Point(550, 300);
  Movie := TFlashMovie.Create(0, 0, 600, 500, udFPS.Position, scPix);
  Movie.Version := SWFVer8;
  Movie.BackgroundColor.RGB := SWFRGB(clWhite);
  Movie.AddCircle(0, 0, 25).SetRadialGradient(cswfLtGray, cswfGray80, 40, 40);
 
  Movie.AddLine(P0.X, P0.Y, P3.X, P3.Y).SetLineStyle(2, cswfNavy);
  Movie.PlaceObject(Movie.Shapes.Last, 2);
 
  with Movie.PlaceMotion(Movie.Shapes[0], 1) do
  begin
    StartState.SetPosition(P0.X, P0.Y);
    AddSomeKeyFrame(udFPS.Position div 2);
    with AddKeyFrame(MotionDuration + udFPS.Position div 2) do
    begin
      PlaceObject.SetPosition(P3.X, P3.Y);
 
      DefaultEase.EaseType := TFlashEaseType(cbEaseType.ItemIndex);
      DefaultEase.EaseMethodType := TFlashEaseMethodType(cbEaseMethod.ItemIndex);
      DefaultEase.MiddlePoint := TrackbarValue(easeMiddlePoint);
      case TFlashEaseMethodType(cbEaseMethod.ItemIndex) of
        meLinear, meCircular,
        meSin: {no parameters};
        meSinAnticipate:
          DefaultEase.Params := TrackbarValue(easeAnticipate);
        meExponential:
          DefaultEase.Params := easePower.Position;
        meExponentialAnticipate:
          DefaultEase.Params := MakeEaseExponentialAnticipateParam(easePower.Position, TrackbarValue(easeAnticipate));
        meBounce, meElastic:
          DefaultEase.Params := MakeEaseBoundParam(easeBounds.Position, TrackbarValue(easeReduction), easePower.Position);
      end;
 
      case cbTrajectory.ItemIndex of
      0: {linear}
        begin
          // empty
        end;
      1: {Sinusoid}
        begin
          MotionMoveY.MotionMethod := mmSin;
          MotionMoveY.MotionParam := MakeMotionSinParam(TrackbarValue(sinAmount),
                          sinExtream.Position, TrackbarValue(sinOffset));
        end;
      2: {Curves (Bezier)}
        begin
          P1 := Point(StrToInt(curvX1.Text), StrToInt(curvY1.Text));
          P2 := Point(StrToInt(curvX2.Text), StrToInt(curvY2.Text));
          MotionMoveX.MotionMethod := mmBezier;
          MotionMoveX.MotionParam := MakeMotionBezierParam(P0, P1, P2, P3, true);
          MotionMoveY.MotionMethod := mmBezierLoop;
          MotionMoveY.MotionParam := MakeMotionBezierParam(P0, P1, P2, P3, false);
          Movie.AddCubicBezier(P0, P1, P2, P3).SetLineStyle(1, AddAlphaValue(cswfNavy, -100));
          Movie.PlaceObject(Movie.Shapes.Last, 3);
        end;
      3: { Circle }
        begin
          MotionMoveX.MotionMethod := mmCircle;
          MotionMoveY.MotionMethod := mmCircle;
          MotionMoveY.MotionParam := MakeMotionCircleParam(TrackbarValue(circlYRadius) *
              (-1 + 2 * byte(cbCirclDirect.Checked)), 0);
 
          with AddKeyFrame(2 * MotionDuration + udFPS.Position div 2) do
          begin
            DefaultEase.EaseType := TFlashEaseType(cbEaseType.ItemIndex);
            PlaceObject.SetPosition(P0.X, P0.Y);
            MotionMoveX.MotionMethod := mmCircle;
            MotionMoveY.MotionMethod := mmCircle;
            MotionMoveY.MotionParam := MakeMotionCircleParam(TrackbarValue(circlYRadius) *
                (-1 + 2 * byte(cbCirclDirect.Checked)), 0);
          end;
 
          Movie.ShowFrame(MotionDuration);
        end;
      4: { ZigZag }
        begin
          MotionMoveY.MotionMethod := mmZigZag;
          MotionMoveY.MotionParam := MakeMotionZigZagParam(TrackbarValue(zigzagAmount),
              zigzagPeaks.Position, TrackbarValue(zigzagOffset));
        end;
      end;
    end;
 
    with Movie.AddShape() do
    begin
      Movie.SystemCoord := scTwips;
      Edges.MoveTo(0, 2000);
      for il := 1 to 100 do
        Edges.LineTo(il * TWIPS, 2000 - Round(2000 * KeyFrames[KeyFramesCount - 1].CalcEase(il /100, mopDefault)));
      Movie.SystemCoord := scPix;
      SetLineStyle(2, cswfBlue);
      StyleChangeMode := scmLast;
      Edges.MoveTo(0, 0);
      Edges.LineTo(0, 100);
      Edges.LineTo(100, 100);
      SetLineStyle(1, cswfGray70);
      CalcBounds;
    end;
    Movie.PlaceObject(Movie.Shapes.Last, 10).SetPosition(10, 10);
    Movie.AddFont('Tahoma', true, false, 14, true);
    Movie.AddText('e', cswfGray80, Movie.Fonts.Last, Point(0, 10));
    Movie.PlaceObject(Movie.Texts.Last, 11);
    Movie.AddText('t', cswfGray80, Movie.Fonts.Last, Point(100, 110));
    Movie.PlaceObject(Movie.Texts.Last, 12);
  end;
 
  Movie.ShowFrame(MotionDuration + udFPS.Position);
 
  Movie.MakeStream;
  if not cbExternal.Checked then
  begin
    Mem.Clear;
    Movie.SaveToStream(Mem);
    Mem.Position := 0;
    Player.LoadMovieFromStream(Mem);
    Player.Play;
  end else
    Movie.SaveToFile('motionTween.swf');
  Movie.Free;
 
  if cbExternal.Checked then
    ShellExecute(0, 'open', 'motionTween.swf', nil, nil, SW_NORMAL);
end;