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

Author Topic: How can I show a vector of sprites?  (Read 616 times)

0 Members and 1 Guest are viewing this topic.

FacuRot

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
How can I show a vector of sprites?
« on: May 03, 2017, 01:01:34 am »
Hello.

I'm trying to make a game. The idea is that you control an airplane and you have to shoot to the enemy ships. The problem is that I can't make the enemy ships to appear in the window. the game compile and I don't get any error so I don't know what I'm doing wrong.

This is my code:

class Enemy.h with the class EnemyFactory that handle the texture:

class Enemy {
private:
        sf::Sprite sprite;
       
        float speed = 190.0f ;
       
        Enemy(int _maxX, int _maxY, sf::Texture _texture);
public:
        friend class EnemyFactory;
       
        sf::Sprite render();
        void update(sf::Time deltaTime);
};

class EnemyFactory{
private:
        sf::Texture texture;
public:
        EnemyFactory(){
                texture.loadFromFile("SFML-Game-Development-Book-master/03_World/Media/Textures/Raptor.png");
        }
       
        Enemy create(int _maxX, int _maxY){
                return {_maxX, _maxY, texture};
        }
};

class Enemy.cpp:

Enemy::Enemy(int _maxX, int _maxY, sf::Texture _texture) {
        sprite.setTexture(_texture);
        sprite.rotate(180);
        sprite.setScale(0.8f, 0.8f);
       
       
       
        sprite.setPosition(200,200); //-500
}

sf::Sprite Enemy::render(){
        return sprite;
}


void Enemy::update(sf::Time deltaTime){
        sprite.move(0.0f, speed * deltaTime.asSeconds());
       
        if (sprite.getPosition().y > 480){
                sprite.setPosition(200,200);  //y=-500 x=1+rand()%640
        }
}

class Bullet.h:

class Bullet {
private:

        sf::Sprite sprite;
       
        sf::Vector2f speed;
       
       
        int posX, posY;
       
        Bullet(int _posX, int _posY, sf::Texture &_texture);
       
public:
       
        friend class BulletFactory;
       
        void update(sf::Time deltaTime);
        sf::Sprite render();
};

class BulletFactory{
private:
        sf::Texture texture;
public:
        BulletFactory(){
                texture.loadFromFile("SFML-Game-Development-Book-master/10_Network/Media/Textures/misil2.png");
        }
       
        Bullet create(int _posX, int _posY){
                return {_posX, _posY, texture};
        }
};
 

Bullet.cpp:

Bullet::Bullet(int _posX, int _posY, sf::Texture &_texture) {
       
        sprite.setTexture(_texture);
        sprite.rotate(-90);
        sprite.setScale(0.1f, 0.1f);
       
        posX = _posX;
        posY = _posY;
       
        sprite.setPosition(posX, posY);
       
        speed.y = -500.0f;
}


void Bullet::update(sf::Time deltaTime){
       
        sprite.move(0, speed.y * deltaTime.asSeconds());
}

sf::Sprite Bullet::render(){
        return sprite;
}

class Game.h:

class Game {
private:
        sf::RenderWindow *mWindow;
       
        ScrollingBackground background;
       
        Aircraft aircraft;
       
        std::vector<Enemy> enemies;
        EnemyFactory enemy;
       
        std::vector<Bullet> bullets;
        BulletFactory bullet;
private:
        void proccesEvent();
        void update(sf::Time deltaTime);
        void render();
public:
        Game();
        void run();
};

Game.cpp:

Game::Game(){
        mWindow = new sf::RenderWindow(sf::VideoMode(640,480), "ventana SFML");
        mWindow->setKeyRepeatEnabled(false);
       
        for (int i; i<5; i++){
                enemies.push_back(enemy.create(640,480));
        }      
}

void Game::proccesEvent(){
        sf::Event event;
       
        while(mWindow->pollEvent(event)){
                if(event.type == sf::Event::Closed){
                        mWindow->close();
                }
               
                if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space){
                        bullets.push_back(bullet.create((aircraft.getPosition().x+15), (aircraft.getPosition().y+40)));
                }
               
        }
}


void Game::update(sf::Time deltaTime){
               
        background.update(deltaTime);
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                aircraft.Up();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                aircraft.Down();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                aircraft.Left();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                aircraft.Right();
        }
       
        for (int j=0; j<enemies.size(); j++){
                enemies[j].update(deltaTime);
        }
       
        for (int i=0; i<bullets.size(); i++){
                bullets[i].update(deltaTime);
        }
       
        aircraft.update(deltaTime);
}

void Game::render(){
        mWindow->clear();
        mWindow->draw(background.render());
        for (int j=0; j<enemies.size(); j++){
                mWindow->draw(enemies[j].render());
        }
        for (int i=0; i<bullets.size(); i++){
                mWindow->draw(bullets[i].render());
        }
        mWindow->draw(aircraft.Render());
       
        mWindow->display();
}

void Game::run(){
        sf::Clock clock;
        while(mWindow->isOpen()){
                sf::Time deltaTime = clock.restart();
                proccesEvent();
                update(deltaTime);
                render();
        }
}

Another thing to mention is that at first the enemy ships appeared in the window and worked fine but then I added  the vector of Bullet and it stop working.
Can anyone help me?

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: How can I show a vector of sprites?
« Reply #1 on: May 03, 2017, 07:32:11 am »
In Game.cpp, shouldn't it be "int i = 0" instead of "int i" in the forloop, or was that just a typo?

for (int i; i<5; i++){  
   enemies.push_back(enemy.create(640,480));
}  
 

 

anything