Hello,
I watched a tutorial. There I saw that a TCastleScene object is created with DesignedComponent. Should I do the same? But without it, the paddle works well. Maybe TCastleScene doesn’t have everything from PaddleScene, as it was explained in another forum.
In any case, I’ll show you what I’ve done below.
{ Main view, where most of the application logic takes place.
Feel free to use this code as a starting point for your own projects.
This template code is in public domain, unlike most other CGE code which
is covered by BSD or LGPL (see License | Manual | Castle Game Engine). }
unit GameViewMain;
interface
uses Classes, CastleScene, CastleTransform,
CastleVectors, CastleComponentSerialize,
CastleUIControls, CastleControls, CastleKeysMouse;
type
{ Main view, where most of the application logic takes place. }
TViewMain = class(TCastleView)
published
{ Components designed using CGE editor.
These fields will be automatically initialized at Start. }
LabelFps: TCastleLabel;
PaddleScene: TCastleScene;
public
constructor Create(AOwner: TComponent); override;
procedure Start; override;
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
function Press(const Event: TInputPressRelease): Boolean; override;
end;
TPaddleRec = record
Size: Single;
Left: Real;
Right: Real;
end;
TPaddleSize = (Small, Normal, Large);
var
ViewMain: TViewMain;
PaddleArr: array[TPaddleSize] of TPaddleRec;
PaddleSize: TPaddleSize;
const
PaddleY = -448;
implementation
uses SysUtils;
procedure InitPaddle;
begin
with PaddleArr[Small] do
begin
Size := 0.25;
Left := -833;
Right := 833;
end;
with PaddleArr[Normal] do
begin
Size := 0.5;
Left := -798;
Right := 798;
end;
with PaddleArr[Large] do
begin
Size := 0.75;
Left := -765;
Right := 765;
end;
end;
{ TViewMain ----------------------------------------------------------------- }
constructor TViewMain.Create(AOwner: TComponent);
begin
inherited;
DesignUrl := ‘castle-data:/gameviewmain.castle-user-interface’;
//Container.MousePosition := Vector2(0, PaddleY);
InitPaddle;
PaddleSize := Normal;
PaddleScene.Scale := Vector3(PaddleArr[PaddleSize].Size, 0.75, 1);
//PaddleScene.Scale := Vector3(0.50, 0.75, 1.00);
//PaddleScene.Setup2D;
//PaddleScene.Scale := Vector3(0.50, 0.75, 1.00);
end;
procedure TViewMain.Start;
begin
inherited;
end;
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
const
MoveSpeed = 500;
var
PlayerPos: TVector3;
begin
inherited;
{ This virtual method is executed every frame (many times per second). }
Assert(LabelFps <> nil, ‘If you remove LabelFps from the design, remember to remove also the assignment “LabelFps.Caption := …” from code’);
LabelFps.Caption := 'FPS: ’ + Container.Fps.ToString + ’ X: ’ + FloatToStr(PaddleScene.Translation.X);
//PlayerPos := PaddleScene.Translation;
PlayerPos := Vector3(Container.MousePosition.X - PaddleArr[PaddleSize].Right, PaddleY, 0);
// NEW CODE WE ADD:
{if Container.Pressed[keyArrowLeft] then
PlayerPos := PlayerPos + Vector3(-MoveSpeed * SecondsPassed, 0, 0);
if Container.Pressed[keyArrowRight] then
PlayerPos := PlayerPos + Vector3(MoveSpeed * SecondsPassed, 0, 0);}
{if Container.Pressed[keyArrowDown] then
PlayerPos := PlayerPosition + Vector2(0, -MoveSpeed * SecondsPassed);
if Container.Pressed[keyArrowUp] then
PlayerPos := PlayerPosition + Vector2(0, MoveSpeed * SecondsPassed);}
if PlayerPos.X <= PaddleArr[PaddleSize].Left then
PlayerPos := Vector3(PaddleArr[PaddleSize].Left, PaddleY, 0);
if PlayerPos.X >= PaddleArr[PaddleSize].Right then
PlayerPos := Vector3(PaddleArr[PaddleSize].Right, PaddleY, 0);
PaddleScene.Translation := PlayerPos;
end;
function TViewMain.Press(const Event: TInputPressRelease): Boolean;
begin
Result := inherited;
if Result then Exit; // allow the ancestor to handle keys
{ This virtual method is executed when user presses
a key, a mouse button, or touches a touch-screen.
Note that each UI control has also events like OnPress and OnClick.
These events can be used to handle the "press", if it should do something
specific when used in that UI control.
The TViewMain.Press method should be used to handle keys
not handled in children controls.
}
// Use this to handle keys:
{
if Event.IsKey(keyXxx) then
begin
// DoSomething;
Exit(true); // key was handled
end;
}
end;
end.