# checking for hit?

• 02-25-2006, 02:23 PM
slicer4ever
checking for hit?
how do i make it so it checks to see if say enemy hits player?
like:

if enemyx==playerx and enemyy==playery then
enemyxmove = math.random(4)
enemyy = math.random(272)
enemyx = 450
lives = lives-1

that doesn't seem to work so is there some special way to say and or hittest or what?
• 02-25-2006, 04:03 PM
SG57
well, there are plenty of collision detection engines out there, but mine is in C, so you wouldnt understand it, but i could translate it to LUA if you want?
• 02-25-2006, 05:18 PM
fchaos

Anyways, what you want is a collision detection function.

Code:

```function collision(x1, y1, w1, h1, x2, y2, w2, h2)         if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then                 if x2 >= x1 and x1 + w1 >= x2 then                 return true                 elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then                 return true                 elseif x1 >= x2 and x2 + w2 >= x1 then                 return true                 elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then                 return true                 end         end return false end```
x1, y1, w1, h1 are the x coor, y coor, width and height of the first object and x2, y2, w2, h2 are the x coor, y coor, width and height of the second object. The function will return either true if their is a collision or false if their isn't.

Oh, and I certainly hope your collision detection "engine" isn't the one I provided for you, SG57. http://forums.qj.net/developers-dungeon/37068-box-barrier-help.html
• 02-25-2006, 06:16 PM
slicer4ever
well thanks although i figured out for now a semi hit test that seems to work ok for now=-) perhaps later on, i'll use this also i was wondering if you could do something like thos in lua:

if play==false then
if somehting then
play=true
end
end
if play==true then
if something then
do sometin
end
end

would that work or not?
• 02-25-2006, 08:35 PM
slicer4ever
k i figured out you could so new question:can you check to see what image is currently being displayed?
• 02-25-2006, 10:03 PM
SG57
Quote:

Originally Posted by slicer4ever
k i figured out you could so new question:can you check to see what image is currently being displayed?

Yes, very easily, but I put mine into a loop:

for(i=1; i>0; i++) {
screen:blit whatever
blitting w/e image = true
}

if blitting w/e image = true then
what ever you want here
end

My LUA is bad since starting a new language, so this is a combination of them sorry, but this will make a loop as long as 1 is greater than 0 and it is, so it goes up to ensure an infinitacy (?), and it will display the image thing for ever and make make a flag called 'blitting w/e image' become true. Then just call an if statement saying if blitting w/e image is true, then dom w.e.

My LUA is very bad since starting a new languaage.
• 02-25-2006, 10:30 PM
slicer4ever
yes i prgram in flash also it's hard to forget not to put _root. and that stuff o but flash felt so much easier...
• 02-26-2006, 10:43 AM
slicer4ever
arg it doesn't work, i'm trying to make my menu but that method does not work=-(