GoTo / Wait until ...

Started by
5 comments, last by Flogo 20 years, 3 months ago
Hi There, I''m trying to make an 2D-Point-and-Klick Adventure (Monkey Island Style) with Delphi and DelphiX. My Animation (The Maincharakter) has a Point-Var: "Target" The Timer sets the Position of the Animation one Step in the direction of target everytime it is called, so that the charakter walks to his "target". My Problem is a command like: go to door when you are there ... open the door and load new level or: Set Target (Door) Wait until (Target = Position) open the door and load new level I tryed to do this "waiting" with repeat application.processmessages until (Target=Position) but by this the Timer is not called and the Animation not moved. I hope someone can help me here cu Flogo PS: please excuse my poor English Life is just a bad Adventure but Graphics are cool
Life is just a bad Adventure but Graphics are cool
Advertisement
The best thing would be to assign your main game function to Application.OnIdle and in that set Done to false.
See the help for the function definition (TIdleEvent).
This will make it loop and use 100% CPU, so you should put a sleep(5) command in there.
From there you can do all your game logic.
This also ensures all messages are processed as usual.
Thanx for answering this fast!
...
So i dont need a Timer anymore?
and bout that sleep thing: doesnt this block the entire game for 5 Milliseks? (so 100% cpu are used anyway?)
I will try this now! thanx again
Flogo
Life is just a bad Adventure but Graphics are cool
sleep(5) passes control to other programs for 5 milliseconds. If nothing else is using a lot of CPU you''ll be running at 200 loops/second, that should be fast enough.
You can still use timers, for instance if you do anything like shoot a monster you can start a timer for 10 seconds, then when it fires its code creates a new monster.
Ok I tried it but nothing happend (except for the 100% CPU). Maybe you can see my Error:

procedure IdleProc(Sender: TObject; var Done: Boolean);
begin
if An <> nil then
An.DoMove;
Application.ProcessMessages;
sleep(5);
Done := false;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
An.target := Point(x,y);
repeat
if (An.Target.X <> X) or (An.Target.Y <> Y) then showmessage(''target changed'');
Application.ProcessMessages;
until (An.X = X) and (An.Y = Y);
showmessage(''target reached'')
end;

I think its the same mistake as earlier:
DoMove is not called in the repeat-until block
Life is just a bad Adventure but Graphics are cool
You don''t have to call processmessages yourself, it will be done for you. It exits the idleproc every time and handles messages, then enters it again immediately.

If you release the mouse you''ll be loopin in the formmouseup.
What you can do it when the formmouseup happens, you set the target. The idleproc calls your game logic, which moves the player and tests if it''s there. You should exit the formmouseup without waiting, then the idleproc loop continues and will call domove until it reaches its target.
But thats what my old code did. (klick sets the Target, Timer moves the charakter there)
But my Problem was exactly this waiting.

Goto(199,274);
say(''im there'');

The Klickproc should do this as i have ~50 Levels and I cant write that code all in my Mainloop.
Isnt there any way to wait until a Var has a certain value that doesnt stop the Program?
Life is just a bad Adventure but Graphics are cool

This topic is closed to new replies.

Advertisement