Game project

Hi!

I'm currently making a game using HGE and C++. I've gotten a really weird bug though and I can't seem to be able to solve it on my own, so I've came here to get some help.

This specific bug makes the game crash and it only happens sometimes when 2 enemy ships overlap and their rectangles overlap eachother. When the bullet hits, it's supposed to remove it, then swap the last element to that specific spot and then put the bullet count to x-1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void GameEntity :: bulletCollision()
{

	for (int x = 0; x < bCount; x++)
	{

		//GETS CURRENT BULLETS RECTANGLE
		pShot[x]->getBoundingBox(&bRect);

		for (int i = 0; i < capacity; i++)
		{

			bool bulletHit = false;

			//GET ENEM SHIPS RECTANGLE
			eShip[i]->getBoundingBox(&eRect);

			//CHECK IF TWO RECTANGLES COLLIDES
			if (bRect.Intersect(&eRect))
			{

				bulletHit = true;

				if (bulletHit == true)
				{

					pShip->setPlayerScore();

					posY = -80.0f;
					posX = hge->Random_Float(0, 
						mScreenMaxHeight - 70.0f);
					speed = (hge->Random_Float(0.5, 2.5));

					delete eShip[i];
					eShip[i] = new Enemy(eTexture, 
						posX, speed);

					delete pShot[x];
					pShot[x] = pShot[bCount - 1];
					bCount--;

				}

			}

		}

	}

}


The line that causes the program to crash is pShot[x] = pShot[bCount - 1]. If anyone could give me a pointer on how to solve it, it'd be awesome.
most likely the culprit is line 38. That would mean you delete an invalid object (maybe already deleted?)

Look at line 22: you set bulletHit to true and on line 24 you check whether bulletHit is true.
So line 24 is suspiciously useless.
I guess you wanted something different there?
You seem to be onto something here. Maybe I have to make a check to see whether or not 2 enemies has collided with the same bullet and if that is true, only remove the bullet once. Only problem I have is how will I do that count? :P


Unhandled exception at 0x20746F6E in ProjectTemplate.exe: 0xC0000005: Access violation executing location 0x20746F6E.


and (This is the next statement to execute when this thread returns from the current function) pShot[x] = pShot[bCount - 1]
Last edited on
This is the next statement to execute when this thread returns from the current function) pShot[x] = pShot[bCount - 1]

Are you certain that x and bCount - 1 are always valid index values for that array? That they never go out of bounds?
The array has the count of as many ships as there are on the screen (7, 10). The count bCount can never go any higher than capacity. So yes, I'm certain. This code works, it only fails when 2 ships collide with the same bullet at the same time. the x value, as stated in the code, can never go higher than the current bCount so that shouldn't be the problem.

However:
I noticed that it tries to take bCount - 1 twice (which has no element after the first one is executed). Why it behaves this way is because of the if-function that checks if it collides with the enemy ship. So basically; I need to have another if-function that checks if there is two ships that has collided with the same bullet and then only run it once.

How I am suppose to do this, I have no idea.

E: Here's a picture of it. Ignore the text above the arrow, it's only to describe the example of the crash.
http://i.imgur.com/iRSew9l.png
Last edited on
Solved it with an if-function under the i's for-loop and put it inside x's. Thanks anyways.

1
2
3
4
5
6
7
8
	if (bulletHit == true)
	{

		delete pShot[x];
		pShot[x] = pShot[bCount - 1];
		bCount--;

	}
Topic archived. No new replies allowed.