开发者

c++ vectors causing break in my game [closed]

开发者 https://www.devze.com 2023-01-14 21:34 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维百科 Closed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消