* * *

Author Topic: Problems with logic problems  (Read 386 times)

krull

  • New member
  • *
  • Posts: 12
Problems with logic problems
« on: April 27, 2017, 06:34:07 pm »
Hello,

I'm still practising in writing a vertical shoot 'em up with Spaceship. The current progress is that tried to get this ship firing more than one shot. But the Problem is as soon as it hits the boundaries an error occure.
The strange thing is that I" free shot1" in line 130 and the "assigned(shot1)" in line 133 gives back a "true". I also tried "destroy" same effect.
So if somebody have a bit time and an idea thanks.
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows ,Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, BGRABitmap, BGRABitmapTypes;
  9.  
  10. type
  11.   TPlayer = class
  12.     playerPosTimer : TTimer;
  13.     shotTimer: TTimer;
  14.     constructor create();
  15.     procedure playerPosTimerTimer(Sender: TObject);
  16.     procedure playerPaint(p:Point;pOld:Point);
  17.     procedure shotTimerTimer(Sender: TObject);
  18.     procedure shotPaint(shot:TBGRABitmap; p:Point);
  19.   private
  20.  
  21.   public
  22.    playerShip : TBGRABitmap;
  23.    shot1, shot2, shot3 : TBGRABitmap;
  24.    pPlayer : Point;
  25.    pFormer: Point;
  26.    shotSpeed : Integer;
  27.    pShot1, pShot2, pShot3 : Point;
  28.    leftClick, x : boolean;
  29.  
  30.   end;
  31.  
  32.   { TForm1 }
  33.  
  34.   TForm1 = class(TForm)
  35.     procedure FormClick(Sender: TObject);
  36.     procedure FormCreate(Sender: TObject);
  37.  
  38.   private
  39.     { private declarations }
  40.   public
  41.     { public declarations }
  42.   end;
  43.  
  44. var
  45.   Form1: TForm1;
  46.   Player1 : TPlayer;
  47.  
  48. implementation
  49.  
  50. {$R *.lfm}
  51.  
  52. { TForm1 }
  53.  
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. begin
  56.  Player1:=TPlayer.create;
  57. end;
  58.  
  59. procedure TForm1.FormClick(Sender: TObject);
  60. begin
  61.   Player1.leftClick:=true;
  62. end;
  63.  
  64. constructor TPlayer.Create();
  65. begin
  66.   playerShip := TBGRABitmap.Create('1.png');
  67.   pPlayer:= Mouse.CursorPos;
  68.   pPlayer:= Form1.ScreenToClient(pPlayer);
  69.   shotSpeed:=10;
  70.   leftClick:= false;
  71.   pShot1.y:=1; pShot2.y:=1; pShot3.y:=1;
  72.   playerPosTimer := TTimer.Create(nil);
  73.   shotTimer := TTimer.create(nil);
  74.   playerPosTimer.Interval := 25;
  75.   shotTimer.Interval := 25;
  76.   playerPosTimer.OnTimer := @playerPosTimerTimer;
  77.   shotTimer.onTimer := @shotTimerTimer;
  78.   playerPosTimer.Enabled := true;
  79.   shotTimer.Enabled := true;
  80. end;
  81.  
  82. procedure TPlayer.playerPosTimerTimer(Sender: TObject);
  83. begin
  84.   pFormer:= pPlayer;
  85.   pPlayer:= Mouse.CursorPos;
  86.   pPlayer:= Form1.ScreenToClient(pPlayer);
  87.   playerPaint(PPlayer,pFormer);
  88. end;
  89.  
  90.  
  91.  
  92. procedure TPlayer.playerPaint(p:Point;pOld:Point);
  93. begin
  94.   Form1.Canvas.FillRect(pOld.x-round(0.5*playerShip.Width),pOld.y-round(0.5*playerShip.Height),pOld.x+playerShip.Width-round(0.5*playerShip.Width),pOld.y+playerShip.Height-round(0.5*playerShip.Height));
  95.   playerShip.Draw(Form1.Canvas, p.x-round(0.5*playerShip.Width),p.y-round(0.5*playerShip.Height),false);
  96. end;
  97.  
  98. procedure TPlayer.shotTimerTimer(Sender: TObject);
  99. begin
  100.   if (leftClick = true) and not assigned(shot1) then
  101.   begin
  102.     shot1:= TBGRABitmap.Create('Beam1.png');
  103.     pShot1.x:=pPlayer.x- round(0.5*shot1.width);
  104.     pShot1.y:=pPlayer.y- round(0.5*playerShip.Height)-shot1.Height;
  105.     shotPaint(shot1,pShot1);
  106.     leftClick:=false;
  107.   end;
  108.   if (leftClick = true) and not assigned(shot2) and assigned(shot1) then
  109.   begin
  110.     shot2:= TBGRABitmap.Create('Beam1.png');
  111.     pShot2.x:=pPlayer.x- round(0.5*shot2.width);
  112.     pShot2.y:=pPlayer.y- round(0.5*playerShip.Height)-shot2.Height;
  113.     shotPaint(shot2,pShot2);
  114.     leftClick:=false;
  115.   end;
  116.   if (leftClick = true) and not assigned(shot3) and assigned(shot2) and assigned(shot1)then
  117.   begin
  118.     shot3:= TBGRABitmap.Create('Beam1.png');
  119.     pShot3.x:=pPlayer.x- round(0.5*shot3.width);
  120.     pShot3.y:=pPlayer.y- round(0.5*playerShip.Height)-shot3.Height;
  121.     shotPaint(shot3,pShot3);
  122.     leftClick:=false;
  123.   end;
  124.  
  125.   if assigned(shot1) or assigned(shot2) or assigned(shot3) then
  126.   begin
  127.     if pShot1.y<=0 then
  128.     begin
  129.       Form1.canvas.FillRect(pShot1.x, pShot1.y, pShot1.x+shot1.Width, pShot1.y+shot1.Height);
  130.       shot1.free;
  131.       pShot1.y:=1;
  132.     end
  133.     else if assigned(shot1) then
  134.     begin
  135.       pShot1.y:= pShot1.y-shotSpeed;
  136.       shotPaint(shot1,pShot1);
  137.     end;
  138.     if (pShot2.y <= 0) then
  139.     begin
  140.       Form1.canvas.FillRect(pShot2.x, pShot2.y, pShot2.x+shot2.Width, pShot2.y+shot2.Height);
  141.       shot2.free;
  142.       pShot2.y:=1;
  143.     end
  144.     else if assigned(shot2) then
  145.     begin
  146.       pShot2.y:= pShot2.y-shotSpeed;
  147.       shotPaint(shot2,pShot2);
  148.     end;
  149.     if (pShot3.y <= 0) then
  150.     begin
  151.       Form1.canvas.FillRect(pShot3.x, pShot3.y, pShot3.x+shot3.Width, pShot3.y+shot3.Height);
  152.       shot3.free;
  153.       pShot3.y:=1;
  154.     end
  155.     else if assigned(shot3) then
  156.     begin
  157.       pShot3.y:= pShot3.y-shotSpeed;
  158.       shotPaint(shot3,pShot3);
  159.     end;
  160.   end;
  161.  
  162. end;
  163.  
  164. procedure TPlayer.shotPaint(shot:TBGRABitmap; p:Point);
  165. begin
  166.  Form1.canvas.FillRect(p.x, p.y+shotSpeed, p.x+shot.Width, p.y+shot.Height+shotSpeed);
  167.  shot.draw(Form1.Canvas,p.x,p.y,true);
  168. end;
  169.  
  170. end.
  171.  

Handoko

  • Hero Member
  • *****
  • Posts: 1327
  • My goal: build my own game engine using Lazarus
Re: Problems with logic problems
« Reply #1 on: April 27, 2017, 07:20:58 pm »
I've tried your code.

Nice.  :D
When you finished it, don't forget to post it on:
http://forum.lazarus.freepascal.org/index.php/topic,35313.msg235850.html

Now back to your question. To fix your issue, change the shot1.free with FreeAndNil(shot1). Do to same also on shot2 and shot3.

Free and FreeAndNil are similar, but in your case, you should use FreeAndNil.
« Last Edit: April 27, 2017, 07:34:32 pm by Handoko »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus