Hello

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Hello

Post by Empirean » September 24th, 2017, 3:37 am

hello guys, im new here and im new to c++. i wanna say chili i appreciate your tutorials man.

i have a question, i am planning to make a space adventure game wherein you have a spaceship (rectangle for now) and youre gonna dodge things(other rectangles for now) that are coming at you. im on the planning stage right now and i was wondering if do i need destructors for my enemy class since there will be a lot of it that will go to the edge of the screen and wont be displayed anymore as the game progress.

im planning to implement something like ill have a loop that will go through every obstacle just like in the poo game and when they go to the edge of the screen ill destroy the obstacles and reuse the index of the destroyed obstacles .

Code: Select all

for (int i = 0; i < maxObstacle; i++)
{
     if (obstacle[i].isDestroyed())
     {
           // really destroy it
           // then reindex
           obstacle[i] = obstacle[maxObstacle];
           maxObstacle--;
           i--;
     }
     else
     {
           // draw and other logic
     }

}
to recap, how do i destroy classes? do i really need destructors for this case? or ill just overwrite the previous values and ignore? i appreciate the help. thank you in advance.

Edited by albinopapa:
Just put code in

Code: Select all

[code]
[/code]]tags

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hello

Post by chili » September 24th, 2017, 6:10 am

Welcome to the forum bro. I'm not sure how far you are in the tutorials, but destructors are generally required when you have some resource (like dynamic memory) that you need to dispose of when an object is deleted.

Unless you have some dynamic memory that the obstacle owns, you shouldn't need to implement dtor for it.

What you're doing there is a very common pattern in C++ by the way, often used to remove elements from a std::vector.
Chili

Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Re: Hello

Post by Empirean » September 24th, 2017, 6:49 am

thank you for the response man. i still dont know about vectors. im still in the array part of the first series. good to know that im doing it right. thanks again for the resource.

Edit: its not working as expected using arrays. looks like vector is the way to go.

Post Reply