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

Author Topic: Help needed: Target an entire Array  (Read 1596 times)

0 Members and 1 Guest are viewing this topic.

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Help needed: Target an entire Array
« on: April 22, 2012, 08:22:56 pm »
So I am trying to make a canon that shoots bullets, the only problem that I'm having is that I don't know how to move/draw the objects sprite efficently. The commented parts are where the problem lies

Here is the code

Declarations.h
Code: [Select]
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

class bullet
{
public:
bullet(){shoot = true;}
sf::Clock clock;
sf::Sprite sprite;
void setShoot(bool setShoot){shoot = setShoot;}
bool getShoot(){return shoot;}
private:
bool shoot;
};

#endif

Main.cpp
Code: [Select]
#include <iostream>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
App.SetFramerateLimit(90);

sf::Image Bullet_Bill;
Bullet_Bill.LoadFromFile("Bullet_Bill.png");

int i = 0;
bullet bulletMember;
bullet myBullet[10];

vector <bullet> bulletlist;

while(App.IsOpened())
    {
sf::Event Event;

while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true && i <= 8)
    {
    cout << "Bang! " << i << endl;
i ++;
    bulletlist.push_back(myBullet[i]);
myBullet[i].sprite.SetImage(Bullet_Bill);
    bulletMember.setShoot(false);
    }
    if (bulletMember.getShoot() == false)
    {
    bulletMember.clock.Reset();
    bulletMember.setShoot(true);
    }
    myBullet[i].sprite.Move(150 * App.GetFrameTime(), 0); // only applies to 1 object at a time :(

    App.Clear(sf::Color(255,255,255,255));
App.Draw(myBullet[i].sprite); // only applies to 1 object at a time :(
App.Display();
}
}

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Help needed: Target an entire Array
« Reply #1 on: April 23, 2012, 12:31:09 am »
This is where a for loop is useful:
App.Clear(sf::Color(255,255,255,255));

for(int i = 0; i < ARRAY_SIZE; ++i) //loop through every value of i
{
   myBullet[i].sprite.Move(150 * App.GetFrameTime(), 0); //now all bullets will be effected
   App.Draw(myBullet[i].sprite);
}

App.Display();
 

I'd recommend you convert myBullet into a vector as well.

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Help needed: Target an entire Array
« Reply #2 on: April 23, 2012, 01:13:15 am »
It's not working for me, still the same problem. Can you please show me how you got it to work?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: Help needed: Target an entire Array
« Reply #3 on: April 23, 2012, 06:14:13 pm »
It's not working for me, still the same problem. Can you please show me how you got it to work?

Then you obviously did something wrong. Because what thePyro_13 wrote is just a loop that itereates through every bullet set the position and draws it.

Maybe you should remove the decleration of int i = 0; in the main() function scope.
And yes you should use a vector for myBullets too.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/