This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Resetting an Object?  RSS feed

 
Jordan Rosenberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm learning Java, coming from a C++ background and I've been reading Head First Java.

I've finished a complete simple Pong game, but I was stuck on figuring out how to "restart" the game. I would press the button R on the keyboard and it would then run a method to restart the game.

In order for the game to restart, the objects' data from the classes Paddle and Ball would need to be reset.

So when the game starts, the objects are created and they interact with each others data. But if I want to restart the game, I was thinking of just re-linking the references to new objects with the fresh new data. Doing so, the old objects are null and deleted right?

Thanks so much in advance!


//Game Starts...
player_1 = new Paddle(1):
player_2 = new Paddle(2);
ball = new Ball();

//method to Restart the Game
player_1 = new Paddle(1):
player_2 = new Paddle(2);
ball = new Ball();
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Unused objects are garbage-colleced, so yes, they will be deleted.
 
Jordan Rosenberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!