Austin
Austin

Reputation: 3080

Clearing a Double Array (I think I am doing it wrong)

Basically, I am building my own pokemon game, however whenever I go into a new "region" the tiles should reset and refresh, however, instead of clearing the old ones out, they just add onto the existing ones, thus causing a lot of issues. I know that once I can successfully clear the old tiles, the game will refresh nice and neat for me.

I thought all i had to do was

buttonPanels = new JButton[row][col]

to make a new (clean) instance of the button array? The whole board is a just a JButton array.

Two images below, one of before and one of after I go into a new region. You can see how how in the after photo all the new tiles just add onto the existing ones, which causes a lot of problems.

http://i421.photobucket.com/albums/pp296/rskom/before.png http://i421.photobucket.com/albums/pp296/rskom/after.png

THANK YOU!! :) First time at trying a rpg type thing, so don't be too critical of it so far.

Upvotes: 0

Views: 219

Answers (1)

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

Without seeing more of your code it is hard to answer your problem, but I think I know what the problem is. You show code where you do create a new array of JButton references. However, this does not destroy or remove the buttons you had in the previous array. If the buttons were visible somewhere (usually the case for having buttons) then they should first be removed from that container. This is probably the sequence you want if won want to replace all buttons.

For each button that is referenced from your array, remove it from the container where it is visible. Then you can replace your array, fill it with new buttons and finally add the buttons to the container where they should be visible again.

But then the real question is if you really do need to create a new array with new buttons. Can you not just reuse your existing one?

Upvotes: 1

Related Questions