• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Resetting an Object?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic