views:

94

answers:

2

Hi guys my code for collision detection is as follows i need it to loop through 55 pictures on the main form //aliens are named alien1, alien2 ect //image2 is the collison object ( the bullet)

CODE IS TURBO DELPHI

procedure TForm1.TimeralienshotTimer(Sender: TObject)
var
  ax2 : integer;
  bx2 : integer;
  ay2 : integer;
  by2 : integer;
  alienX : array[1..55] of integer;
  alienY : array[1..55] of integer;
  Index: Integer;
begin
  for index := 1 to 55 do
    alienX[index]:= alien([index]).left <--- the problem is this code here !!!!
  ax2 := alienX + 21;
  bx2 := Image2.left + 10;
  ay2 := AlienY + 25;
  by2 := Image2.top + 24;
  if ax2 >= alienX then
    if alienX <= bx2 then
      if ay2 >= image2.top then
        if alienY <= by2 then
        begin
          alien[index].Visible := false;
          Image2.top := 875;
          Image2.left := 648;
          Timershotmovment.Enabled := false;
        end;
end;
A: 

I reformatted your source code.

It looks like a homework assignment, so I'll give some hints to get it solved:

  • make sure you get the right code inside and outside of the loop use a begin ... end block for that (like you did at the inner most if ... then).
  • getting the X or Y coordinates of all aliens does not require a separate array. Just use Alien[Index].Left and Alien[Index].Top
  • do not use hard-coded literals (21, 10, 25, 24, 875, 648). Make those either constants, or get that data from the right place (probably there is something on your form defining those numbers).

--jeroen

Jeroen Pluimers
I suspect he wants to index separate variables alien1...alien54 as array. VB component arrays a like.
Marco van de Voort
+1  A: 
for index := 1 to 55 do
  alienX[index]:= alien([index]).left <--- the problem is this code here !!!!

Two things:

  1. "The problem is this code here" - What problem? You didn't mention one, either in the subject or the question itself.

  2. The typecast you're trying to make where the "problem" apparently occurs can't possibly work. You can't typecast an integer array index as a type alien (which doesn't exist anywhere in your code, btw - you've declared two integer arrays named alienX and alienY, but nothing declared as simply alien).

If your intent was simply to copy alienY's content into the alienX array, you can use this:

for i := 1 to 55 do
  alienX[i] := alienY[i];

Or, better yet in case the limits of the arrays need to change in the future:

for i := Low(alienX) to High(alienX) do
  alienX[i] := alienY[i];

However, since you're trying to access something with a "left" property, that's probably not what you intend. However, without knowing what "alien" is exactly, and what it contains, it's hard to be more specific.

A suggestion for the future? When you say you have a "problem", it helps if you tell people what "problem" is exactly.

Ken White