clock.Reset() setting clock to weird time

Im trying to make a target launcer fire a target every 5 seconds. The problem is that the clock starts acting funny after the first target is launched. Here is the log from the console:
1
2
3
4
5
6
1	clock:4.61451 amountofT: 0
2	clock:4.8168 amountofT: 0
3	degree: 14   force: 14
4	clock:2.54222e-005 amountofT: 1
5	clock:0.191573 amountofT: 1
6	clock:0.394454 amountofT: 1


amountofT is the amount of targets that are currently in the game. degree and force are cout'ed right before the target is fired. My question is why do i get that "clock:2.54222e-005"? This problem is also causing targets to be fired prematurely and then i get some kind "list iterator not incrementable" Debug Assertion Failed message.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
01	//Launch A Target Every 10 Seconds
02	if(clock.GetElapsedTime() >= 5){
03	    //Launch target
04	    srand(time(NULL));
05	    int degree = rand() % 15;
06	    int force = rand() % 20+5;
07	    Target *target = new Target(xPos, yPos, degree, force);
08	    targets.push_back(target);
09	 
10	    std::cout<<"degree: "<<degree<<"   force: "<<force<<std::endl;
11	 
12	    //Reset clock
13	    clock.Reset();
14	    }
15	 
16	         std::cout<<"clock:"<<clock.GetElapsedTime()<<" amountofT: "<<         targets.size()<< std::endl;

"clock:2.54222e-005"

Seems pretty reasonable to me. You reset the clock, and then you ask it how much time has elapsed since it was reset, and it says 25 micro-seconds.
Last edited on
Ahh okay the problem seems to be with removing the target once it has crashed. When the target collides i want it to be removed, but instead i get a "list iterator not incrementable" Debug Assertion Failed message. Here is the relevant code:

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
//In TargetLauncher::Update()
	//Launch A Target Every 10 Seconds
	if(clock.GetElapsedTime() >= 5){
		//Launch target
		srand(time(NULL));
		int degree = rand() % 15;
		int force = rand() % 20+5;
		Target *target = new Target(xPos, yPos, degree, force);
		targets.push_back(target);

		std::cout<<"degree: "<<degree<<"   force: "<<force<<std::endl;

		//Reset clock
		clock.Reset();
	}

	//Update each target
	if(targets.size() > 0)
		for( std::list<Target*>::iterator i = targets.begin(); i != targets.end(); ++i)
		{
			if( (*i)->Update(window) == REMOVE )
				targets.erase(i);
		}

	std::cout<<"clock:"<<clock.GetElapsedTime()<<" amountofT: "<< targets.size() << std::endl;

	return KEEP;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//In Target::Update()
Target::State Target::Update(sf::RenderWindow& window)
{
	//Move the target
	CollideWithMap(window);
	xPos += xVel;
	yPos += yVel;

	yVel += GRAVITATION;

	sprite.SetPosition(xPos - scroll_X, yPos - scroll_Y);

	//If the target collides with something, return REMOVE
	if(broken) return REMOVE;

	return KEEP;
}


CollideWithMap sets 'broken' to true if it collides with the map. Remove is returned. Then i get "Debug Assertion Failed!...list iterator not incrementable" because the for loop continues for some reason.
Let's say there are 5 elements in the list, and they contain letters a to e, like this: a,b,c,d,e.

You're at the 4th element; that is, list[i] where i=3. You decide to delete it. Now, the list looks like this:

a,b,c,e

You're still at list[3], which is now the element with the e in it.

Now you increment the iterator. Oh no! You've just incremented the iterator to position 4 in the list, but that doesn't exist! You've incremented an iterator when you shouldn't, because you're not allowed to increment an iterator beyond the end of the list, and it crashes.

Last edited on
Shouldn't it stop looping since i now equals list.end()?

1
2
3
4
5
	for( std::list<Target*>::iterator i = targets.begin(); i != targets.end();++i;)
	{
		if( (*i)->Update(window) == REMOVE )
				targets.erase(i);
	}


How do i fix this?
Shouldn't it stop looping since i now equals list.end()?

When is that checked? Before starting each new loop. What happens at the end of the current loop, even though we're now at list.end()? i++.

Just don't increment in a loop where you erase.
Last edited on
Okay i changed it to
1
2
3
4
5
6
7
for( std::list<Target*>::iterator i = targets.begin(); i != targets.end();)
	{
		if( (*i)->Update(window) == REMOVE )
				targets.erase(i);
		else
			i++;
	}


And now i get "Debug Assertion Failed!...list iterators incompatible" upon removal.
Aha i found the answer. I needed to do i= targets.erase(i) instead of just targets.erase(i)
Topic archived. No new replies allowed.