Błąd Access Violation

0

Gdy naciskam F1 lub F2 to wyskakuje błąd Access Violation at adress... Ale dlaczego?

Oto kod

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Menus, StdCtrls, Math;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);

  private
   procedure ShiftLeft;
   procedure ShiftRight;
   procedure Fire;
   procedure Action(wKey : Word; sShift : TShiftState);
    { Private declarations }
  public
    { Public declarations }
  end;

  TProcedure = procedure of object;

{ rekord, ktory przechowuje informacje o skrocie klawiaturowym i o procedurze
  ktora bedzie wykonywana w nastepstwie nacisniecia takowego skrotu }
  TShortCutArray = packed record
    sShortCut: TShortCut;
    pAction : TProcedure;
  end;

var
  ShortCutArray : array[0..20] of TShortCutArray; // tablica rekordow
  Form1: TForm1;
  zly: TImage;
  imgBron: TImage;
  img : TImage;
  Fileimg: string;
implementation

{$R *.dfm}
procedure TForm1.Action(wKey: Word; sShift: TShiftState);
var
  i : Integer;
  pKey : Word;
  pShift : TShiftState;
begin
  for I := 0 to High(ShortCutArray) do
  begin
  { rozbicie skrotu z tablicy }
    ShortCutToKey(ShortCutArray[i].sShortCut, pKey, pShift);
    { porownanie zmiennych }
    if ((pKey = wKey) and (sShift = pShift)) then
      ShortCutArray[i].pAction; // wywolanie odpowiedniej procedury
  end;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
{ wywolaj z parametrami procedure Action }
Action(Key, Shift);
if (key=VK_CONTROL) then
Image2.Picture.LoadFromFile('D:\Delphi\gra\ludzik_ukl.bmp');
if (key=VK_F1) then begin
Fileimg:='D:\Delphi\gra\naboj-do-ak47.bmp';
img.Top:=160;
img.Left:=88+Image2.Left;
imgBron := TImage.Create(self);
imgBron.Picture.LoadFromFile('D:\Delphi\gra\AK-47.bmp');
insertcontrol(imgBron);
imgBron.Proportional:=true;
imgBron.Width:=81;
imgBron.Top:=176;
imgBron.Left:=img.Left+8;
end;
if (key=VK_F2) then begin
imgBron.Destroy;
Fileimg:='D:\Delphi\gra\pif.bmp';
img.AutoSize:=true;
img.Top:=160;
img.Left:=7+Image2.Width+Image2.Left;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Fileimg:='D:\Delphi\gra\pif.bmp';
//skróty klawiszowe
Shortcutarray[0].sShortCut := TextToShortCut('Left');
Shortcutarray[0].pAction := ShiftLeft;
Shortcutarray[1].sShortCut := TextToShortCut('Right');
Shortcutarray[1].pAction := ShiftRight;
Shortcutarray[2].sShortCut := TextToShortCut('S');
Shortcutarray[2].pAction :=Fire;


Randomize;
zly:=TImage.Create(self);
zly.Picture.LoadFromFile('D:\Delphi\gra\zly.bmp');
zly.AutoSize:=true;
zly.Top:=147;
zly.Left:=RandomRange(50,700);
zly.Visible:=true;
insertcontrol(zly);

end;

procedure TForm1.ShiftLeft;
begin
if (Image2.Left <> 0) then
Image2.Left:=Image2.Left-5;
end;
procedure TForm1.ShiftRight;
begin
if (Image2.Left <> 700) then
Image2.Left:=Image2.Left+5;
end;
procedure TForm1.Fire;
begin
img := TImage.Create(self);
img.Picture.LoadFromFile(Fileimg);
insertcontrol(img);
img.AutoSize:=true;
img.Top:=160;
img.Left:=7+Image2.Width+Image2.Left;
img.Visible:=true;
while img.Left <> 700 do begin
Application.ProcessMessages;
Sleep(15);
img.Left:=img.Left+5;
  if img.Left >= zly.Left then begin
  img.Left:=7+Image2.Width+Image2.Left;
  zly.Picture.LoadFromFile('D:\Delphi\gra\zly_dead.bmp');
  zly.Top:=178;
  img.Visible:=false;
  break;
  end;
end;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (key=VK_CONTROL) then
Image2.Picture.LoadFromFile('D:\Delphi\gra\ludzik.bmp');
end;

end.
0

Próbowałeś debugować?
Ale z tego, co widzę, to nie tworzysz img.

1 użytkowników online, w tym zalogowanych: 0, gości: 1