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

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

0 Members and 1 Guest are viewing this topic.


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

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();
        Sprite ast;
        m_sprite = ast;
        m_pos.x = rand () % 800 + 30;;
        m_pos.y = -10;
void entidad::actualizar(){
void entidad::dibujar(RenderWindow &w){

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;
        for(size_t i=0;i<m_v.size();i++) {
                Vector2f charaux;
                charaux = m_v[i].verposi();
                if(charaux.y > 900){
                        auto it = next(m_v.begin(),i);
void entidadmanager::dibujar(RenderWindow &w){
        for(size_t i=0;i<m_v.size();i++) {
                entidad aux;
                aux = m_v[i];

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) {
        if(n1.dispara()) vd.push_back(n1.dispGen());
        for(disparo &d : vd) d.actualizar();
        auto it2 = remove_if(vd.begin(),vd.end(),fuera);
void Partida::dibujar(RenderWindow &w, Sprite fondo) {
        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 »


  • SFML Team
  • Hero Member
  • *****
  • Posts: 10194
    • 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
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/