Now I edited it somewhat.
What it should do is
- play the animations up until the selected LastAnimationName and then exit. (this works okay).
- Then by pushing a button it should start animation from the last animation (which is the current animation) and end with the animation that is declared in the push button procedure.
I cannot get this to work; currently every animation sequence starts from the first animation (standfront).
If I start with NextAnim = FirstAnim (which is 8, the number it should start with (StandBack) it does not work.
I would like some help here. Thanks.
(see attachment for the code and data)
unit GameStateMain;
interface
uses Classes,
CastleUIState, CastleComponentSerialize, CastleUIControls, CastleControls,
CastleKeysMouse, CastleViewport, CastleScene, CastleVectors, StrUtils, Dialogs,
CastleWindow,CastleUtils, CastleGLImages, CastleFilesUtils,
CastleSoundEngine, CastleTimeUtils, CastleColors,
CastleRectangles, CastleFonts, CastleGLUtils, Generics.Defaults,
Generics.Collections, CastleDownload, CastleSceneCore,
CastleCameras, CastleTransform, CastleImages, X3DNodes, X3DFields, CastleURIUtils;
type
TStateMain = class(TUIState)
private
Viewport: TCastleViewport;
AnimList: TStrings;
SequenceList: TStringList;
FirstAnim: Integer;
NextAnim: Integer;
LastAnim: Boolean;
FirstAnimationName, LastAnimationName: String;
PlayForward: Boolean;
procedure StandLeft(Sender: TObject);
procedure StandNE(Sender: TObject);
procedure RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
public
constructor Create(AOwner: TComponent); override;
procedure Start; override;
procedure RunAllAnimations(const Scene: TCastleSceneCore);
procedure RunAnimationList(const Scene: TCastleSceneCore; const AnimNames: array of String);
end;
var
StateMain: TStateMain;
Button1, Button2: TCastleButton;
Label1, Label2: TCastleLabel;
Scene1: TCastleScene;
implementation
uses SysUtils;
{ TStateMain ----------------------------------------------------------------- }
constructor TStateMain.Create(AOwner: TComponent);
begin
inherited;
DesignUrl := 'castle-data:/gamestatemain.castle-user-interface';
end;
procedure TStateMain.Start;
begin
inherited;
Viewport := DesignedComponent ('Viewport') as TCastleViewport;
Button1 := DesignedComponent('Button1') as TCastleButton;
Button2 := DesignedComponent('Button2') as TCastleButton;
Label1 := DesignedComponent('Label1') as TCastleLabel;
Label2 := DesignedComponent('Label2') as TCastleLabel;
Button1.Caption := 'standleft';
Button2.Caption := 'standne';
Button1.OnClick:= @StandLeft;
Button2.OnClick:= @StandNE;
Scene1 := DesignedComponent('Scene') as TCastleScene;
Viewport.Items.Add(Scene1);
Scene1.Translation:= Vector3(800,300,0);
// Scene1.URL:= 'castle-data:/kyley_idle.starling-xml';
Scene1.URL:= 'castle-data:/john_idle.starling-xml';
SequenceList := TStringList.Create();
// RunAllAnimations(Scene1);
// RunAnimationList(Scene1, ['kyleyfront', 'kyleyturnsw', 'kyleysw', 'kyleyturnleft', 'kyleyleft', 'kyleyturnnw', 'kyleynw',
// 'kyleyturnback', 'kyleyback', 'kyleyturnne', 'kyleyne', 'kyleyturnright', 'kyleyright', 'kyleyturnse', 'kyleyse', 'kyleyturnfront', 'kyleyfront']);
FirstAnimationName := 'johnstandfront';
LastAnimationName := 'johnstandback';
PlayForward := True;
RunAnimationList(Scene1, ['johnstandfront', 'johnturnsw', 'johnstandsw', 'johnturnleft', 'johnstandleft', 'johnturnnw', 'johnstandnw', 'johnturnback', 'johnstandback',
'johnturnne', 'johnstandne', 'johnturnright', 'johnstandright', 'johnturnse', 'johnstandse', 'johnturnfront', 'johnstandfront']);
end;
procedure TStateMain.StandLeft(Sender: TObject);
begin
SequenceList := TStringList.Create();
LastAnimationName := 'johnstandleft';
PlayForward := False;
RunAnimationList(Scene1, ['johnstandfront', 'johnturnsw', 'johnstandsw', 'johnturnleft', 'johnstandleft', 'johnturnnw', 'johnstandnw', 'johnturnback', 'johnstandback',
'johnturnne', 'johnstandne', 'johnturnright', 'johnstandright', 'johnturnse', 'johnstandse', 'johnturnfront', 'johnstandfront']);
end;
procedure TStateMain.StandNE(Sender: TObject);
begin
SequenceList := TStringList.Create();
LastAnimationName := 'johnstandne';
PlayForward := True;
RunAnimationList(Scene1, ['johnstandfront', 'johnturnsw', 'johnstandsw', 'johnturnleft', 'johnstandleft', 'johnturnnw', 'johnstandnw', 'johnturnback', 'johnstandback',
'johnturnne', 'johnstandne', 'johnturnright', 'johnstandright', 'johnturnse', 'johnstandse', 'johnturnfront', 'johnstandfront']);
end;
procedure TStateMain.RunAllAnimations(const Scene: TCastleSceneCore);
begin
AnimList := Scene.AnimationsList;
NextAnim := 0;
RunNextAnimation(Scene, nil);
end;
procedure TStateMain.RunAnimationList(const Scene: TCastleSceneCore; const AnimNames: array of String);
var I: integer;
begin
if PlayForward then
begin
For I := 0 to High(AnimNames) do
begin
SequenceList.Add(AnimNames[I]);
end;
end else
begin
For I := High(AnimNames) downto 0 do
begin
SequenceList.Add(AnimNames[I]);
end;
end;
AnimList := SequenceList;
NextAnim := 0;
RunNextAnimation(Scene, nil);
end;
procedure TStateMain.RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
var
NextAnimationName: String;
Params: TPlayAnimationParameters;
begin
{if NextAnim = AnimList.Count then
begin
FreeAndNil(SequenceList);
Exit;
end;}
// if AnimList[NextAnim] = LastAnimationName then Exit;
Label1.Caption:= 'current animation number: ' + IntToStr(NextAnim);
Label2.Caption:= 'First to start animation in next call: ' + IntToStr(FirstAnim);
if LastAnim then
begin
FirstAnim := NextAnim; // remember the index value of LastAnimationName so next call to RunAnimationList will start from this animation
FirstAnimationName := LastAnimationName; // next call to RunNextAnimation should start with this animation
FreeAndNil(SequenceList);
LastAnim := False;
Exit;
end;
NextAnimationName := AnimList[NextAnim];
if NextAnimationName = LastAnimationName then LastAnim := True else
Inc(NextAnim);
Params := TPlayAnimationParameters.Create;
try
Params.Name := NextAnimationName;
Params.Forward:= PlayForward;
Params.StopNotification := @RunNextAnimation;
Scene.PlayAnimation(Params);
finally FreeAndNil(Params) end;
end;
end.
animationsequence4.zip (48.1 MB)