Animated Title

This demo shows an image bouncing and rotating under the control of a timer. Rotation is measured in degrees and is changed by 0.5 each time (and not restricted to an integer). Rotation about the x, y and/or z axis is possible.

The Code

unit Form1;

interface

uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Sprite3D, SmartCL.Time;

type
  TTitle= class(tw3sprite)
  private
    mDown:  Boolean;
  protected
    procedure InitializeObject; override;
  public
    procedure UpdateBehavior(const TimeId: integer); override;
  end;

  TForm1 = class(TW3form)
  private
    FTitle: TTitle;
    FTimer: TW3Timer;
    FCounter: integer;
    {$I 'Form1:intf'}
  protected
    procedure InitializeObject; override;
    procedure FinalizeObject; override;
    procedure Resize; override;
    procedure HandleUpdate(Sender: TObject);
  end;

implementation

{ TTitle}

procedure TTitle.InitializeObject
begin
  inherited;
  SetTransformFlags(CNT_USE_POS or CNT_USE_ROTY);
  Width := 422;
  Height := 57;
  mDown := True;
  Update3d;
  Transparent := True;
  Background.fromURL('res/parallax.png');
end;

procedure TTitle.UpdateBehavior(const TimeId: integer);
begin
  if (TimeId mod 3) = 0 then
  begin
    case mDown of
    True:
      begin
        if Y < 20 then
          MoveY(0.5)
        else
          mDown := False;
      end;
    False:
      begin
        if Y > 10 then
          MoveY(-0.5)
        else
          mDown := True;
      end;
    end;
    Update3d;
  end
  else
  begin
    RotateY(-1);
    Update3d;
  end;
end;

{ TForm1}

procedure TForm1.HandleUpdate(Sender: TObject);
begin
  Inc(FCounter);
  FTitle.UpdateBehavior(FCounter);
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  StyleClass := '';
  FTitle := TTitle.Create(self);
  FTimer := TW3Timer.Create;
  FTimer.Delay := 1;
  FTimer.onTime := HandleUpdate;
  FTimer.Enabled := True;
end;

procedure TForm1.FinalizeObject;
begin
  FTitle.Free;
  inherited;
end;

procedure TForm1.Resize;
var
  dx : integer;
begin
  inherited;
  dx := (Width div 2) - (FTitle.Width div 2);
  FTitle.SetBounds(dx, 10, FTitle.Width, FTitle.Height);
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.    

The Demo

If Spartacus1 does not run in your current browser, please try another (such as Chrome). If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page.

Spartacus1
Programming - a skill for life!

Different ways of using sprites in Smart Mobile Studio