destroy and reinitialize object

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
coolmoon
Posts: 7
Joined: April 7th, 2017, 12:17 am

destroy and reinitialize object

Post by coolmoon » April 19th, 2017, 4:47 am

Hey,
so just starting out and have gone through lesson 12 or so and stopped to try and make a version of pong with what i have learned so far.

I have made a ball class and initialize at the beginning of the file and can draw the file with ball.draw(gfx); bounce it off the walls and paddles ect..

my problem is I can't seem to figure out how to destroy the ball object. I mean I can just stop drawing it but x & y keep updating and I can't seen to create another ball at the original settings.


any ideas?

Thanks for your time

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: destroy and reinitialize object

Post by albinopapa » April 19th, 2017, 5:37 am

1)Keep a static constexpr copy of the starting position of the ball in the ball.h file. When you need to reset the ball position, make ball.x and ball.y equal to Ball::xStart and Ball::yStart.

2)Make a reset function in the Ball class, and when you need to reset the position, you call the ball.Reset() function. In that function, you can assign x = xStart and y = yStart in that function.

3)In the Game class when you want to reset the ball position, you can assign a new ball to the old ball, ball = Ball(Ball::xStart, Ball::yStart );

Hope this gives you some ideas. If you want other things to reset, you would have to also make starting values for those as well and reset them. The last option has a drawback though, if you keep track of number of balls ( lives ) in the ball class, it will be reset to the default starting value. If you go with this option, keep the number of lives outside of the ball class, maybe in the Game class.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

coolmoon
Posts: 7
Joined: April 7th, 2017, 12:17 am

Re: destroy and reinitialize object

Post by coolmoon » April 20th, 2017, 2:21 am

Thanks albinopapa,
been playing with it and think I got a ball.reset() function working.

Post Reply