I made a simple java game last week. At that time, one class extended Jframe and held the data (which comprised three other class, through composition)
Now, I'm trying to make the same game, but with several JPanels. One panel will be the game graphics (basically the old JFrame shrunk into one panel). And there will be buttons and text fields on other panels.
But I'm confused about how the program's data sharing/accessing will work. I now have one JFrame extending class with 3 subpanels. One of those subpanels is the game graphics, which currently has all the Player data.
1) Is there someway I c开发者_如何转开发an leave the data in that panel and just access it with getters/setters? (I've tried but not been able to make anything work.)
2) How are games usually laid out? Should I, for instance, move the Player data to the JFrame class which holds all the panels, and add getter/setters there. (Ideally, the two player arraylists will be used/edited by all panels.)
I would recommend you move the Player to a Player class, and provide each panel a reference to it, and following that pattern for most of your data.
精彩评论