Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How does this work?  (Read 2084 times)

0 Members and 1 Guest are viewing this topic.

theonekamisama

  • Newbie
  • *
  • Posts: 8
    • View Profile
How does this work?
« 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..

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How does this work?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

theonekamisama

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How does this work?
« Reply #2 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?


JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How does this work?
« Reply #3 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.