SFML community forums

Help => General => Topic started by: theonekamisama on April 10, 2017, 04:13:54 pm

Title: How does this work?
Post by: theonekamisama on April 10, 2017, 04:13:54 pm
Disclaimer: I am in no way a pro in c++ nor in sfml, Only sucessful game i made was snakes. and that was a bare minimum game...T-T

I wonder if this works....
https://github.com/theonekami/Towerdefence/tree/tower

Ok so i  have here a simple engine for tower defence. It's under construction, and i hope you don't have to read through the entire thing

I have two functions for calling the creeps and adding the towers.

both of them run in the main loop. The creep function updates as the time goes on...

Now the problem here is when i add a tower. It works fine but when the mouse is not moving the creep just stops animating. and when i move the mouse then the creep moves forward

Please any help is appreciated. Honestly i have no idea on how to do this..
Title: Re: How does this work?
Post by: eXpl0it3r on April 10, 2017, 05:21:40 pm
I didn't look at the code, but issues like that usually happen when you put your update code inside the event processing loop. By doing so, your update code only gets executed when there's an event. Moving your mouse triggers an event, thus your animations suddenly seem to work.

Make sure to not have your update code inside the while(window.pollEvent(event)) loop.
Title: Re: How does this work?
Post by: theonekamisama on April 12, 2017, 04:42:38 am
But if i don't have the update function inside the game loop then won't it be executed only once.
Correct me if i'm wrong but the game loop runs infinitly, so if the update function is outside the game loop then it won't run again and again right?

Title: Re: How does this work?
Post by: JayhawkZombie on April 12, 2017, 05:20:25 am
The creep class is being updated in this call on line 68 of main.cpp
c.update(t,w);
and the function
call_creep(c)
is called on line 135 of main.cpp

Yes, you need to call update from within the game loop.  But you can use other functions to do that, like that code does.  It called "call_creep" every frame within the game loop and passes a "creep" by reference. That function in turn calls "update" on the creep object.