SFML 2.0 c++ projectiles -


i have projectile firing tank. @ moment works fine, unable "re-use" projectiles once have either hit target, or gone off screen. code using @ moment;

//laser shape sf::texture lasertexture; lasertexture.loadfromfile("images/laser.png"); std::vector<sf::sprite>laser(1000, sf::sprite(lasertexture)); 

this if statement when keyboard pressed:

if (event.key.code == sf::keyboard::space)                     {                         if (lasercount==1000)                         {                             lasercount=0;                         }                         /*if (sf::keyboard::iskeypressed(sf::keyboard::space))                         {                          }*/                         laserspeed=4;                         lasercount++;                         laser.play();                         std::cout << "laser count = " << lasercount << std::endl;                     } 

and clock counter firing missile:

static float lasertimer =0.0;                 lasertimer+=clock.getelapsedtime().asseconds();                 if (lasertimer<ldelay)                 {                     lasertimer = 3;                 }                 else {                     lasertimer = 0;                 }                  (int = 0; < lasercount; i++)                 {                     laser[i].move(0, -laserspeed);                  } 

this bad way of doing , poorly optimised, , know this. tried have 50 projectiles in vector, , when reach top of screen or hits target, go tank. didn't work, @ all... if set them relative tank appear @ side of screen , carry on firing.

for (int i=0; i<lasercount; i++)     {     if (laser[i].getposition().y==0)        {         laser[i].setposition(xtank, ytank);         laserspeed=0;        } } 

this put laser @ side of screen (even though tank @ middle of screen). tried actual position (300,200) gave same issue, , other sprites on screen freeze.

i don't want have unnecessary amounts of sprites when frankly aren't needed!

why want reuse particles? can remove them list once have gone off screen or hit target. if want limit amount of particles, shoot timer that. way you're doing it, there 1000 objects, , loaded whether or not use them. isn't efficient.

since more versed in c# , xna in sfml, use c# code, should able apply same concepts.

// global variables  list<particle> particles = new list<particle>(); // empty list  keyboardstate oldkeystate;  float shoottimer = 0.0f; bool justshot = false;  // ... in update function  float elapsed = (float)gametime.elapsedgametime.totalseconds; // delta time keyboardstate curkeystate = keyboard.getstate();  // don't let user hold down space key. has tap it. if (curkeystate.iskeydown(keys.space) && oldkeystate.iskeyup(keys.space)) {  if (!justshot)  {   particles.add(new particle(tank.position));   justshot = true;  } }  if (justshot) {  if (shottimer < shotdelay)  {   shottimer += elapsed; // in seconds  } else { justshot = false; shottimer = 0; } }  (int = 0; < particles.count; i++) {  particles[i].update();  // if (collision or past end of screen)  particles.remove(particles[i]); // 1 way  particles[i].active = false; // way }  oldkeystate = curkeystate; 

this way, using many particles constrained game logic. note is kind of pseudo-code. of course put code in update/main game loop. adapt wish.

edit

remove --> (1000, sf::sprite(lasertexture))

that way have empty vector. whenever need add particles, use push_back.

an example of particle class in c#:

class particle {  public particle(texture2d texture, vector2 position) {    texture = texture;    position = position;  }  public texture2d texture;  public vector2 position;   public void update(gametime gametime) {   // delta time here   position += new vector2(speedx, speedy) * elapsed;  } } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -