Why are the enemies not appearing on the screen?

I’m writing a game based on these two tutorials Lazy Foo' Productions - Motion

I managed to get enemies on the screen with

now I’m trying to get the enemies firing back but they are not appearing on the screen. Csn anyone tell me what to do to fix this, I checked all the logic and it seems to match the source code for the tutorial. What have I missed?

$ g++ main.cpp -g -lSDL2_image -lSDL2

Like I wrote in your previous thread - don’t put the code on some dropbox. Create a github repository, put the code there and paste a link to it here.

Also, did you follow the steps that Peter87 suggested?

Hi I put some cout in the functions to see if they are being called and I also spotted a logic error in resetStage which I fixed. now the enemies are appearing but the ship is dissappearing and reappearing and the lasers are not firing regularly and the enemies aren’t firing. Please help, is it something to do with the ship being reset to null? Is the code in main causing any problems? Thanks.

Please show me how to move forward in pinpointing the problem.

The Stage.h/.cpp is missing. Add them and upload to the repo. I will help you with the game.

Thank you! Looking forward to some specialist tuition!

Ok man, I solve the bug.

So, in the FunctionsDefinitions.cpp inside the initPlayer() you miss to setup the player->health to some positive value. As you don’t inited it, probably the compiler put trash value inside it and the value goes negative. So, as in the doFighters() you are not prevendting the int health to go under 0 (but just == 0) and the game logic crash.

Then, just change it to e->health <= 0 inside doFighters() and remember to init the value of player->health to some positive integer. Oh, and also, set the player->next to NULL after you put the firghtersTail->next points to player. The player->next is not inited to NULL automatically!

The code is a little confusiing but enough because you are learning now.

Just some code tips:

1–always use indentation! Here in the forum editor is not good to give a example, but search at the google or some IA and ask “how to indent porperly my code”. There are some patterns as the gnu way, but you are free to invent the yours!

2–all classes in c++ call the constructor after creating the object.

For exemple: class Laser{} can have a constructor called the same name as the class inside it, but it is a function. Like class Laser{…Laser(){//here inside constructor}}

The constructor is always called when you create a variable with the type Laser, but, pointers like Laser *ptr never calls a constructor because is just a pointer, but if you make something like Laser * ptr = new Laser() so the new allocator will trigger the constructor of Laser()

3–c++ classes can have a destructor too! Desctructor is just a function which is called before free the memory of the class. You can use it to create ways of unload resources, etc.

4–study about data structures, that knowledge will give you the mysterious powers of computing! You already the practice now needs some theory and a little more of training in it.

And the game is good! I know is simple, but is something you should pride yourself for make it happens!

If you wanna some help, let me know.

Thanks so much! It works!

1 Like

I notice when the alien bullets are fired sometimes they move to the right, I want to flip the alienbullet with SDL_RenderCopyEx when this happens so its pointing in the right direction. I cant find anywhere in the code the alienBullet being rendered or blited. Where would I put the flip instruction in the code ? Thanks.