Advertisement

collision trouble...

Started by December 27, 1999 01:47 PM
2 comments, last by ViPeR007 24 years, 11 months ago
try changing this code:

laser[index].state = LASER_STATE_OFF;


to this:

laser[index].attr = laser[index].attr & ~BOB_ATTR_VISIBLE;


Does this work???

You might try setting a flag like beenhit=TRUE; ,and not accept anymore hits for n amount of milliseconds,or seconds.
Advertisement
I am implementing a system in my game where it takes 3 hits from a level 1 laser to destroy an enemy. I already had the collision code for one hit working so I though I would just add a counter to count each time a laser would hit and when that counter would hit 3 (3 hits) then it would destroy the enemy. Well I found out that my collision code wasn't quite working because once the laser collides with the enemy it acts like it keeps colliding. Ill explain how I found this out but here is the collision code:

if ( bot.attr & BOB_ATTR_VISIBLE )
{
for (index=0; index {
if ( (laser[index].attr & BOB_ATTR_VISIBLE) && (Collision_BOBS(⊥, &laser[index])) )
{
laser[index].state = LASER_STATE_OFF;
Start_Burst(laser[index].x, laser[index].y,
68+rand()%12,54+rand()%10,
bot.xv>>1, bot.yv>>1);
laser_hit++;

if (laser_hit == 36000)
{
Hide_BOB(⊥);
boton = 2;
score = score + 300;
laser_hit = 0;
}

}
}
}

(I know this code wouldn't destroy the enemy in three hits because I test the counter for 36000 but I did this because if I put it to 3 it counts up so fast that you barely can see it)This code is in Move_Laser(); which is runned in the Game_Main();. Now I figured out that the laser just kept hitting it because I displayed the counter variable on the screen. Now it should have gone from 0 to 1 when my laser beam hit the enemy instead it went to 1 and then just kept on counting up which means that even though the laser gets turned off (LASER_STATE_OFF) and it goes invisible somehow its still hitting the enemy. Even if the laser is hidden it shouldn't be hitting it because I make sure that its not invisible by putting if ( (laser[index].attr & BOB_ATTR_VISIBLE)... Does anyone see an error in the code above that would be causing this?

Thanx,
ViPeR

BTW, Im using the GPDUMB engine from one of Andre Lamothes books.

[This message has been edited by ViPeR007 (edited December 26, 1999).]

Ok Enix's code worked. Now it when it collides it will count to one. Then I shoot another and it will count to two. Then I can't shoot anymore. Now this only happens when I make the lasers collide with the enemy, otherwise if I just shoot around it Ill be able to shoot as much as I want. I added && LASER_STATE_OFF to the end of laser[index].attr = laser[index].attr & ~ BOB_ATTR_VISIBLE because when I check for the boundries of the screen (when to delete the laser) it has to be on for it to check so by setting it off it just won't have to check it. Here is the function I use to fire the laser:

void Fire_Laser(int x,int y, int vel)
{

for (int index=0; index {

if (laser[index].state == LASER_STATE_OFF)
{
// start this one up
laser[index].x = x;
laser[index].y = y;
laser[index].yv = -vel;
laser[index].curr_frame = 0;
laser[index].state = LASER_STATE_ON;

// start sound up
for (int sound_index=0; sound_index < MAX_FIRE_SOUNDS; sound_index++)
{
// test if this sound is playing
if (Status_Sound(fire_ids[sound_index])==0)
{
Play_Sound(fire_ids[sound_index]);
break;
} // end if

} // end for sound_index


return;

} // end if

} // end for

}

I have no idea why it will only let me take two shots because my MAX_LASER is set to 50. When I put it back to the way it was (broken) it can fire as many shots as you want it to. I know for a fact (through testing) that all my functions dealing with the laser are executing but for some reason its not being drawn to the screen and its not dectecting a collision with the enemy. Does anyone know why this one line can make my whole laser go away (after 2 shots that collide)?

Thanx,
ViPeR

This topic is closed to new replies.

Advertisement