• Post Reply Bookmark Topic Watch Topic
  • New Topic

Buttons in array list moving on tile map  RSS feed

 
Marc Beck
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a space strategy game. The Game should include a tile map with squares for each sector. There should also be horizontal and vertical scrollbars for it is a large map. There should be Icons for Planets and Icons for Space Fleets, and when you click on the Icons, you get information of the planet and of the fleet you click on.

I have tried to make an arrayList of Buttons for all the planets and for each player (1 human, all others computer)one arrayList of Buttons for all fleets. I have only two listeners: one for the Planet Buttons and one for the Fleet Buttons. It all worked fine until I started developing the part where the fleets should move into different sectors (I have a timer and each second they move until they reach their destination coordinates). I have a refresh function that loops through all the fleets and puts their icons at their new position. Now I see only one fleet moving on screen. What happened to the others?

The thing with the scrollbars does not work either. when I try to use them, It "smears" my map and that looks really messy.

Is there any better way to make things work? In VB, you can use any object to click on and something happens, in java you have to make it a JButton and add a listener. Am I right, or is there a different way? I mean, I want to have buttons that move on screen and those buttons would be stored in an arrayList.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,

Welcome to JavaRanch!

You haven't said anything outrageous; nothing you've described is hard to do. It's just a matter of getting the details right. Things you might check: be sure you're really using a separate "Fleet" object for each fleet; if you have multiple variables pointing to the same one, then you'll see this kind of problem. Likewise if your Fleet class has static variables where it should have regular instance variables.

How are you positioning your Buttons in the window? Are you using a layout manager of some kind, or a null layout and absolute positioning? Changing the layout of a visible container can be dicey.
 
Marc Beck
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks for trying to help me out.

I am using null layout and position the Buttons with setBounds. I figured that would work best.

My game has an arrayList named empireList that contains all Empires. Each Empire has an arrayList named fleetList that contains all fleets belonging to that Empire. I have a class named empire and a class named fleet.


This piece of code prints the names of all empires and their respective fleets. I tried that instead of printing the name of each fleet, I added a button with a listener.

A copy of that code, modified that it just positions the buttons at the coordinates of each fleet instead of adding a new button, is called every second, so it updates the position of the buttons when the fleets move.

But that doesn't work. I always have only one Button.I try to avoid static variables whenever I can
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!