Recent

Author Topic: Memory game  (Read 2074 times)

zerowin

  • New member
  • *
  • Posts: 8
Memory game
« on: April 02, 2019, 12:32:06 am »
Hello, I have a question about what I do to stock 2 SpeedButtons in 2 variables and next, compare this 2.

                              The error --->    "unit1.pas(61,32) Error: Incompatible types: got "Int64" expected "TSpeedButton"


If anyone has a suggestion that might support the project, even though it has nothing to do with what was asked, please say.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Olá, eu tenho uma dúvida sobre como eu posso fazer para armazenar 2 speedbuttons em variáveis para poder compará-los.
                          Vou postar o código a seguir, mas o erro é o falado acima.
Se alguém tiver uma sugestão que possa me ajudar no projeto, mesmo que não tenha nada a ver com o que eu perguntei, pode falar.
Desde já agradeço!



This is my code

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Buttons;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     ImageList1: TImageList;
  16.     SpeedButton1: TSpeedButton;
  17.     SpeedButton2: TSpeedButton;
  18.     SpeedButton3: TSpeedButton;
  19.     SpeedButton4: TSpeedButton;
  20.     SpeedButton5: TSpeedButton;
  21.     SpeedButton6: TSpeedButton;
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure SpeedButton1Click(Sender: TObject);
  24.     procedure SpeedButton2Click(Sender: TObject);
  25.     procedure SpeedButton3Click(Sender: TObject);
  26.     procedure SpeedButton4Click(Sender: TObject);
  27.     procedure SpeedButton5Click(Sender: TObject);
  28.     procedure SpeedButton6Click(Sender: TObject);
  29.   private
  30.  
  31.   public
  32.  
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.   b1,b2: TSpeedButton;
  38.  
  39. implementation
  40.  
  41. {$R *.lfm}
  42.  
  43. { TForm1 }
  44.  
  45. procedure TForm1.SpeedButton1Click(Sender: TObject);
  46. begin
  47.   ImageList1.GetBitmap(0, SpeedButton1.Glyph);
  48.     end;
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. begin
  52.          SpeedButton1.tag := 1;
  53.          SpeedButton2.tag := 2;
  54.          SpeedButton3.tag := 2;
  55.          SpeedButton4.tag := 3;
  56.          SpeedButton5.tag := 1;
  57.          SpeedButton6.tag := 3;
  58.  
  59.  
  60.       if (b1 = nil) then
  61.      b1 := TSpeedButton(sender).tag;
  62.  
  63.      else
  64.      b2 := TSpeedButton(sender).tag;
  65.   end;
  66.       if b1=b2 then
  67.       begin
  68.     exit;
  69.           end;
  70.  
  71.  
  72. end;
  73.  
  74. procedure TForm1.SpeedButton2Click(Sender: TObject);
  75. begin
  76.   ImageList1.GetBitmap(1, SpeedButton2.Glyph);
  77. end;
  78.  
  79. procedure TForm1.SpeedButton3Click(Sender: TObject);
  80. begin
  81.   ImageList1.GetBitmap(1, SpeedButton3.Glyph);
  82. end;
  83.  
  84. procedure TForm1.SpeedButton4Click(Sender: TObject);
  85. begin
  86.   ImageList1.GetBitmap(2, SpeedButton4.Glyph);
  87. end;
  88.  
  89. procedure TForm1.SpeedButton5Click(Sender: TObject);
  90. begin
  91.   ImageList1.GetBitmap(0, SpeedButton5.Glyph);
  92. end;
  93.  
  94. procedure TForm1.SpeedButton6Click(Sender: TObject);
  95. begin
  96.   ImageList1.GetBitmap(2, SpeedButton6.Glyph);
  97. end;
  98.  
  99. end.                                  
« Last Edit: April 02, 2019, 12:35:47 am by zerowin »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Memory game
« Reply #1 on: April 02, 2019, 12:58:55 am »
The error message is telling you what's happening:
Quote
"unit1.pas(61,32) Error: Incompatible types: got "Int64" expected "TSpeedButton"

You are declaring your variables as TSpeedButton but you're trying to store an Int64 (the button's Tag property) into them.

Just declare them as Int64:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4.  
  5. [... strip ...]
  6.  
  7. var
  8.   Form1: TForm1;
  9.   b1: Int64 = 0;
  10.   b2: Int64 = 0;
  11.  
  12. implementation
  13.  
  14. [... strip ...]
  15.  
  16. procedure TForm1.FormCreate(Sender: TObject);
  17. begin
  18.   SpeedButton1.tag := 1;
  19.   SpeedButton2.tag := 2;
  20.   SpeedButton3.tag := 2;
  21.   SpeedButton4.tag := 3;
  22.   SpeedButton5.tag := 1;
  23.   SpeedButton6.tag := 3;
  24.  
  25.   if (b1 = 0) then
  26.     b1 := TSpeedButton(Sender).Tag
  27.   else
  28.     b2 := TSpeedButton(Sender).Tag
  29.   end;
  30.  
  31.   if b1 = b2 then
  32.     exit;
  33. end;
  34. [... etc. ...]

Do note that there is a very serious error in your code: if FormCreate is the handler for the Form's OnCreate event then Sender will always be the form itself and never a TSpeedButton.

I can't see what it is you're trying to do so I don't know how to repair that. In fact, it really looks like some parts of your code are missing.
« Last Edit: April 02, 2019, 01:05:48 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

zerowin

  • New member
  • *
  • Posts: 8
Re: Memory game
« Reply #2 on: April 02, 2019, 01:36:53 am »
sorry, in this part:

if (b1 = nil) then        -----------> If b1 didn´t receive nothing
     b1 := TSpeedButton(sender).tag;  -----> b1 receive the tag of the first button the user clicked
     else
     b2 := TSpeedButton(sender).tag; -----> if b1 received the first click, "else" is activated and b2 will receive the second click
  end;
      if b1=b2 then  -------> if the tag of b1 and b2 is the same, so
      begin
    exit;   -----> in this part, i think the code will stop with the function of "turn pieces". If b1=b2, it means that the user hit one combination of pieces, so this 2 pieces will be turned (showing the image I put in them).
          end;


Sorry if it is confused.

zerowin

  • New member
  • *
  • Posts: 8
Re: Memory game
« Reply #3 on: April 02, 2019, 01:45:16 am »
Other question, it´s possible to randomize the index of images on ImageList every time I start the project? ( with a different sequence in every time I start)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Memory game
« Reply #4 on: April 02, 2019, 11:06:45 am »
You're mixing types.  If you want to use integers you can't use pointers/references.  You need to redesign your algorithm. Pascal doesn't has a NaN value as other languages (PHP) have.  To know if b1 doesn't receive a value you should use a non used value instead.  For example, if button tags are [0..3], use -1;  or if tags are [1...4] use 0.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

TinyPortal © 2005-2018