PDA

View Full Version : Showing message on screen for fixed time period



Wizard
11-10-2007, 11:36 AM
OS: WINXP
LIB: DELPHIX

Hi everyone. I would like to display a message on the screen indicating that a bonus life is awarded. At the moment the message is displayed as per the code below, problem is that points can be scored faster or slower and the time that the message is displayed is directly connected to scoring. I would like the message to be displayed for 3 or 5 seconds independent of the scores. ie If points reaches 25000, 50 000 etc the message must be shown for 5 seconds.

Thanks!

if ((points >= 25000) and (points <= 27000)) then
begin
dxFontBonus.TextOut(dxDrawGame.Surface,300,330,'Bo nus Life');
end;

jasonf
11-10-2007, 11:45 AM
you need something independent of the scoring.

I would create a sprite class for this.
Create it when the bonus is achieved and add a new instance to the sprite engine.

The TBonusSprite instance would count down a fixed number of ticks (or it could be time based, your call) and then die... possibly even setting it's alpha value on the way so you get a nice fade out. The text would be provided by an image... or you could useTextOut, but you can make it very dramatic by using an image.

You can also make it play a sound ("BONUS!!!!" in a deep echoing voice) and also maybe create a particle effect.. all of this by creating one object when a bonus is received.

Your call, but that's how I would do it.

Brainer
11-10-2007, 11:49 AM
Place a TTimer component on your form, set its Interval on 5000, its Enabled on False and paste this in its OnTimer event:


dxFontBonus.TextOut&#40;dxDrawGame.Surface,300,330,'Bo nus Life'&#41;;


An then somewhere:


if &#40;points >= 25000&#41; and &#40;points <= 27000&#41; then
Timer1.Enabled &#58;= True;


Hope that helps :!: :D

Huehnerschaender
11-10-2007, 12:11 PM
Place a TTimer component on your form, set its Interval on 5000, its Enabled on False and paste this in its OnTimer event:


dxFontBonus.TextOut&#40;dxDrawGame.Surface,300,330,'Bo nus Life'&#41;;


An then somewhere:


if &#40;points >= 25000&#41; and &#40;points <= 27000&#41; then
Timer1.Enabled &#58;= True;


Hope that helps :!: :D

This will not help because it will show the text for hours if the player stops scoring after reaching 25000 points.

In addition to that, if I read your code right, it will let the text blink every 5 seconds for a wink of an eye :)

Wizard
11-10-2007, 12:16 PM
Brainer, from what I've read it's not wise to have TTimer with the DelphiX timer.

What about TimeGetTime?

Something like:

procedure TFormGame.BonusLife;
var BonusTime : single;
begin
BonusTime := timegettime + 3000;
if timegettime <BonusTime>= 5000) then
begin
dxFontBonus.TextOut(dxDrawGame.Surface,300,330,'Bo nus Life');
end;
end;
doesn't work -- any suggestions?

Huehnerschaender
11-10-2007, 12:22 PM
I would do it like that:



type TMessage = record
NeededPoints &#58; integer;
Text &#58; string;
StartTime &#58; cardinal;
Duration &#58; cardinal;
end;

var BonusMsg &#58; TMessage;
.
.
.

// init the Record
with BonusMsg do
Text &#58;= 'Bonus life!';
NeededPoints &#58;= 25000;
StartTime &#58;= 0;
Duration &#58;= 5000;
end;
.
.
.

// Show the message
if &#40;score >= BonusMsg.NeededPoints&#41; then
begin
with BonusMsg do
begin
StartTime &#58;= GetTickCount;
inc&#40;NeededPoints, 25000&#41;;
end;
end;

.
.
.

// rendering in your render routine
if GetTickCount - BonusMsg.StartTime < BonusMsg.Duration then
dxFontBonus.TextOut&#40;dxDrawGame.Surface,300,330,Bon usMsg.Text&#41;;




Code written straight outta my head. No guarantee for compilable code, but it should show the idea...

Away from that, personally I would use a simple particle to show the text!


EDIT: I edited the code so that it should spawn the text each 25000 points!

Wizard
11-10-2007, 01:12 PM
Thanks Huehnerschaender!!

I've compiled your code but the message appears when it should but it doesn't go away?? Am I doing something wrong?


procedure TFormGame.BonusLife;
begin
with BonusMsg do
begin
Text &#58;= 'Bonus life!';
NeededPoints &#58;= 25000;
StartTime &#58;= 0;
Duration&#58;= 5000;
end;
if &#40;points >= BonusMsg.NeededPoints&#41; then
begin
with BonusMsg do
begin
StartTime &#58;= GetTickCount;
inc&#40;NeededPoints, 25000&#41;;
end;
end;

if GetTickCount - BonusMsg.StartTime < BonusMsg.Duration then
dxFontBonus.TextOut&#40;dxDrawGame.Surface,300,330,Bon usMsg.Text&#41;;
end;

procedure bonus life is called in my playing statemachine.

NecroDOME
11-10-2007, 02:24 PM
I think a easy way would be to set a message string and timer (integer or single). When you reached a certain score, you can set the message to what ever you want and set the timer to x seconds (or x millisecond).
Next you can render the message if Timer > 0 in the game main render loop. In the update loop (can be same as render loop) you decrease the your timer by the time that is passed until it reaches 0.

(This is the theory)

Huehnerschaender
11-10-2007, 02:25 PM
You should NOT initialize BonusMsg IN the Bonuslife procedure! You always set the needed points back to 25000 that way which is of course false.

Init it in gameinit or formcreate or something like that.

Wizard
12-10-2007, 05:36 AM
It works!!! :-) :-) :-) Thanks Huehnerschaender !!!

The BonusMsg is now initialized in my Starting Playing state and it works like a charm :!: I wasn't thinking, of course the values of BonusMsg must be initialized first!! This is the only procedure in the game, except for the doMoves, the rest is object orientated :wink:

Thanks very much to all you guys, I'm really learning valuable techniques 8)

All the small detail in my game "Eons" is now sorted and I will post it soon for feedback from you guys :idea: