Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Image Collision (Part 2)

Posted on 1999-07-23
Medium Priority
189 Views
The answer to my Question Q.10185289 Image Collision works.
I can check for collision with other Controls now before moving another control.

But now there is another problem.

The code is:
//Dummy is a TRect
//imgMain is the control I'm going to move.

For i := 0 to Form1.ControlCount - 1 do
If IntersectRect(Dummy, imgMain.BoundsRect, Form1.Controls[i].BoundsRect) = False then
imgMain.Left := imgMain.Left + 5; //No collision, so move it!

The problem is:

Let's say there are 5 controls on the form.
When I use the above code, the Control I want to move will move 5 times the amount I want it to move!
The For - Loop will be implemented on the "Move Control Code"!
That's not what I want!

Is there a way I can move the Control by 5, no matter how many controls the form already has?
0
Question by:jackjoker
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3

LVL 10

Expert Comment

ID: 1390700
Hi Joker,

sorry, but I don't your question. If you want to move your image independent of the control count of the form then just move it. What has the control count to with the move?

Ciao, Mike
0

Accepted Solution

Byteback earned 40 total points
ID: 1390701
i := 0;
intersect := false;
while (i < ControlCount) and (intersect = false) do
begin
If IntersectRect(Dummy, imgMain.BoundsRect, Form1.Controls[i].BoundsRect) = true then
begin
intersect := true;
end;
Inc(i);
end;
if intersect = false then
begin
imgMain.Left := imgMain.Left + 5;
end;

0

LVL 10

Expert Comment

ID: 1390702
ByteBack, now I understand the question :-)) A Break after Intersect := True would also prevent to loop unnecessarily through further controls...

Ciao, Mike
0

Expert Comment

ID: 1390703
your move code (imgMain) gets executed everytime the if evaluates to true which is not what you want so if you change it to the above code it should be solved.

0

Expert Comment

ID: 1390704
Lischke,

true... almost never use the break statement myself...

0

Author Comment

ID: 1390705
I'm sorry but it did not work. Let me try to explain the problem.

I'm using the code with in the KeyDown Event of the Form.

i := 0;
intersect := false;
while (i < ControlCount) and (intersect = false) do
begin
If IntersectRect(Dummy, imgMain.BoundsRect, Form1.Controls[i].BoundsRect) = true then
begin
intersect := true;
end;
Inc(i);
end;
if intersect = false then
begin
imgMain.Left := imgMain.Left + 5;
end;

==========
4 lines above here, is the "imgMain.Left := imgMain.Left + 5;" right? So whatever code put in this place will not be affected by the For-Loop right? I've tried a way similar to yours before. Get the For-Loop to provide a "True or False" value first, THEN clarify it AFTER the For-Loop.
I thought it will work, but sadly, it still doesn't work.
Maybe a REPEAT-UNTIL or TRY-EXCEPT loop will do?

For your information, I'm making a little game for my brother. It's kinda like a car moving around. I want to prevent this little car from going through the RoadBlocks(which are actually Image controls.)

I'll increase the points to 30 if you can help me.
0

LVL 10

Expert Comment

ID: 1390706
Joker,

you should be more precise in your description. WHAT doesn't work? Does "intersect" never become True or does the image still move depending on control count?

Ciao, Mike
0

Expert Comment

ID: 1390707
If i understood your question correctley the above code should work. And yes you can place whatever code is nesseccary instead of "imgMain.Left := imgMain.Left + 5;"
This code checks if the current position of imgMain is not overlapping any of the current controls and if not moves imgMain 5 pixels to the left.
If you're trying to prevent the car from moving through controls you should check if the new position of imgMain  is empty.

something like this:

i := 0;
// get rectangle of imgMain
rect := imgMain.BoundsRect;
// move rectangle to new position
rect.left := rect.left + 5;
// determine if rect is empty
intersect := false;
while (i < ControlCount) and (intersect = false) do
begin
If IntersectRect(Dummy, rect, Form1.Controls[i].BoundsRect) = true then
begin
intersect := true;
end;
Inc(i);
end;
// if rect is empty move imgMain
if intersect = false then
begin
imgMain.Left := imgMain.Left + 5;
end;

p.s. I used a while so you don't have to move through all controls if a control is found which overlaps with the rectangle. You could also use a repeat-until or a for loop in combination with a break just as Lischke said.
try-except is something completely different. You use this for error handling.

0

Expert Comment

ID: 1390708
oops, forgot something...
if you loop through all controls of the form you'll always find a collision as imgMain is a control too.. so you should skip imgMain in the loop...

like this:

if (Form1.Controls[i] <> imgMain) and Intersect(....) then

Maybe you have other controls on your form which also should be skipped... like a background image or panel or something..
you should only check thoses controls which the car is not allowed to enter.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGridâ€¦
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vulnâ€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll