Recent

Author Topic: [SOLVED] Range of numbers  (Read 769 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Range of numbers
« on: April 23, 2022, 04:28:23 pm »
Hi I have this code , how can I make a range from 0 to 100.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  2.   WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   if WheelDelta < 0 then
  5.   begin
  6.      FCounter := FCounter -5;
  7.   end else begin
  8.      FCounter := FCounter +5;
  9.   end;
  10.   Label1.Caption := IntToStr(FCounter);
  11. end;
« Last Edit: April 23, 2022, 09:19:14 pm by Pe3s »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Range of numbers
« Reply #1 on: April 23, 2022, 05:03:57 pm »
Code: Pascal  [Select][+][-]
  1.   // ... do some calculation
  2.  
  3.   // ... put these at the end of the calculation
  4.   If WheelDelta < 0 then WheelDelta := 0;
  5.   If WheelDeltea > 100 then WheelDelta := 100;

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Range of numbers
« Reply #2 on: April 23, 2022, 05:11:56 pm »
unfortunately it doesn't work

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Range of numbers
« Reply #3 on: April 23, 2022, 05:27:07 pm »
Basically, there are 3 different range limited types in graphics programming:

1. It won't go beyond a certain limit but stay at the nearest point of the limit

If Position < LowestLimit then Position := LowestLimit;
If Position > HighestLimit then Position := HighestLimit;


2. When it goes beyond a certain limit, it appears at the opposite side

If Position < LowestLimit then Position := HighestLimit;
If Position > HigestLimit then Position := LowestLimit;


3. When reaching a certain limit, it switches to the opposite direction

If (Position <= LowestLimit) or (Position >= HighestLimit) then
  Direction := -Direction;
Position := Position + Direction;



Which one do you want? Perhaps you can show us your whole source code. Why my code not working questions, usually can be solved quickly if we can inspect the whole source code.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Range of numbers
« Reply #4 on: April 23, 2022, 05:52:15 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  2.   var Handled: Boolean);
  3. begin
  4.   case (WheelDelta < 0) of
  5.     True:  Dec(FCounter, 5);
  6.     False: Inc(FCounter, 5);
  7.   end;
  8.   if FCounter < 0 then
  9.     FCounter := 0
  10.   else if FCounter > 100 then
  11.     FCounter := 100;
  12.   Label1.Caption := IntToStr(FCounter);
  13.   Handled := True;
  14. end;

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Range of numbers
« Reply #5 on: April 23, 2022, 09:18:53 pm »
Thanks for your help.

 

TinyPortal © 2005-2018