my works like this i shot airplanes and if the shot toughed an airplane it change there positions(airplane and the shot) than it will delete them. if i toughed an airplane the airplane position gets changed. than it gets deleted than my health reduces. well that works well expect that it breaks when i have about 2-4 airplanes left and i toughed one of them and shot one of them my game breaks it pops a box says vector out of range... i have had something like this problem before i fixed it because i new where was the problem but the box doesn't give me any information where it is the problem. here my full code
Thanks in Advance
note: i don't think i can go any lowwer with my code i have put new one
Well I just had a quick look at that monstrosity, and I'm not going to look too far into it but right off the bat I see a problem.
I don't know if this is related to this particular problem you are asking about, but this type of thing isn't going to work the way you think:
for(long index=0; index < (long)RegularShots.vshots.size(); ++index)
{
RegularShots.vshots[index].y-=2;
// Delete Shots
if(RegularShots.vshots[index].y<-16)
{
RegularShots.vshots.erase(RegularShots.vshots.begin()+index);
}
}
When you erase an item from the vector, the next item in the vector is moved down one, so every time you erase an item, you are skipping over the next item and not performing your test on it.
A quick fix here would be to do an index-- after the erase call.
But based on what I've seen, I would seriously recommend that you do some reading on STL containers and iterators in particular. And then read some books on good coding style ;)
Learn to 'refactor': for example, code like line 241 looks like it would be better as a subroutine.
Anyway, to find where this particular problem is, do "Debug/Break All" with the debugger while the error box is being displayed; and then look at the debugger's "Call stack" window, to see what code is being executed which triggers the popping of the box.
精彩评论