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

Author Topic: Problem spawning multiple enemies  (Read 1029 times)

0 Members and 2 Guests are viewing this topic.

Baro

  • Newbie
  • *
  • Posts: 6
    • View Profile
Problem spawning multiple enemies
« on: February 22, 2022, 10:08:55 pm »
Hello!

I have a problem whit a game that I am developing, i want to spawn random enemies across time, but only one of them appears in screen. Someone can help me? :D

I made this classes, entidad(entity), entidadmanager(entitymanager) and partida.
entidad::entidad() {
       
        static Texture *tex = 0;
        if(tex == 0){
                tex = new Texture();
                tex->loadFromFile("meteorito.png");
        }
        Sprite ast;
        ast.setTexture(*tex);
        m_sprite = ast;
        m_sprite.setScale(0.03f,0.03f);
        m_pos.x = rand () % 800 + 30;;
        m_pos.y = -10;
        m_sprite.setPosition(m_pos.x,m_pos.y);
}
void entidad::actualizar(){
        m_sprite.move(0,0.3);
}
void entidad::dibujar(RenderWindow &w){
        w.draw(m_sprite);
}

Vector2f entidad::verposi(){
        Vector2f charpos = m_sprite.getPosition();
        return charpos;
}



entidadmanager::entidadmanager() {
        vector<entidad> v;
        m_v = v;
        Clock reloj;
        m_reloj = reloj;
}
void entidadmanager::actualizar(){
       
        if(m_reloj.getElapsedTime().asMilliseconds() >= 1000){
                entidad aux;
                m_v.push_back(aux);
        }
        for(size_t i=0;i<m_v.size();i++) {
                m_v[i].actualizar();
                Vector2f charaux;
                charaux = m_v[i].verposi();
                if(charaux.y > 900){
                        auto it = next(m_v.begin(),i);
                        m_v.erase(it);
                }
        }
        m_reloj.restart();
}
void entidadmanager::dibujar(RenderWindow &w){
        for(size_t i=0;i<m_v.size();i++) {
                entidad aux;
                aux = m_v[i];
                aux.dibujar(w);
        }


Partida::Partida() {
        nave n1;
        entidadmanager met;
       
}
bool fuera(disparo &d){
        Vector2f p = d.verPosicion();
        if(p.x<0 or p.x>1600) return true;
        if(p.y<0 or p.y>900) return true;
        return false;

}
void Partida::actualizar(juego &j) {
        n1.actualizar();
        met.actualizar();
        if(n1.dispara()) vd.push_back(n1.dispGen());
        for(disparo &d : vd) d.actualizar();
        auto it2 = remove_if(vd.begin(),vd.end(),fuera);
        vd.erase(it2,vd.end());
}
void Partida::dibujar(RenderWindow &w, Sprite fondo) {
        w.clear(Color(220,220,180,255));
        w.draw(fondo);
        n1.dibujar(w);
        met.dibujar(w);
        for(disparo &d : vd) d.dibujar(w);
}


I guess that the problem is in entidadmanager::actualizar, but I can`t find it.
« Last Edit: February 23, 2022, 10:00:34 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Problem spawning multiple enemies
« Reply #1 on: February 23, 2022, 11:16:30 am »
I suggest you try to narrow it down a bit more on your own first.
It's rather difficult to jump into someone else's code base, especially when you only have half of the code.
Much easier for you to run the application in a debugger and step through it line by line to see what's going on.
Alternatively you can also print some debug information to the console, like the number of entities in existence or where they are located, that way you should easily see why they don't seem to be where you expect them.

        static Texture *tex = 0;
        if(tex == 0){ ... }

You should really use nullptr (or at least NULL for pre-C++11 code) instead of 0.
Using a static variable here, is much more of a workaround than an actual solution. I recommend using something like a ResourceHolder to manage your resources or at least manage the texture outside of your entity.

Generally highly recommend to use verbose variable names, instead of things like "v", "n1", "met", "j", etc.
Makes for a much easier code reading, especially for others unfamiliar with your code base.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/