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

Author Topic: How can i slow down the rate my projectiles fire.  (Read 1462 times)

0 Members and 1 Guest are viewing this topic.

Darkpony

  • Newbie
  • *
  • Posts: 7
    • View Profile
How can i slow down the rate my projectiles fire.
« on: April 23, 2017, 11:20:52 pm »
I have a function that draws and moves a projectile automatically based on if a certian monsters are in range or not. The problem I am having is that when the monsters in range it draws it so many times its almost like a line. I have been at this for so many hours im about to throw in the towel. I have tried using the clock but its just not working out.

This function checks if the monster is in range and pushes back a vector of bullets.

 for (int i = 0; i < mymonsters.size(); i++)
        {
                for (int k = 0; k < myicetower.size(); k++)
                {
                        if (mymonsters[i].checkCollision(myicetower[k].getRadius(), myicetower[k].getPositionX(), myicetower[k].getPositionY()))
                        {

                                //      BulletClass bulletshot;
                                bulletshot.setPosition(myicetower[k].getPositionX(), myicetower[k].getPositionY());
                                bullet.push_back(bulletshot);
                        }
                }
        }

 

This function draws it to the window. But it draw it so many times its ridiculous

sf::Vector2f fire(1.f, 1.f);
        for (int i = 0; i < bullet.size(); i++)
        {
                //if(function.checkCollision(myicetower, mymonsters))
                {
                        bullet[i].fire(fire);
                        bullet[i].draw(window);
                }
        }
       
        window.display();
}

Any help would be great.
« Last Edit: April 24, 2017, 05:54:06 am by Darkpony »

takatalvi

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How can i slow down the rate my projectiles fire.
« Reply #1 on: May 03, 2017, 09:32:19 am »
Analysing solely the code you posted. Here's how it reads to me.

For every monster, check every tower to see if they're in range. If they are in range, Create a bullet at so and so position.

I think therein lies your issue. You're creating a new bullet every time you check collision(presumably every frame).

In my opinion, I would create a shoot() method in your tower class. If a monster is in range, call this method, but dont spawn a bullet right away. Have a local clock, and check to see that it's past a certain time before actually spawning a new bullet. Most important, when you spawn a new bullet, you need to ::reset() the clock, or it'll auto fire exactly like I think you're seeing now.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: How can i slow down the rate my projectiles fire.
« Reply #2 on: May 05, 2017, 04:40:31 pm »
well, why not use CoolDown thing...

 

anything