The moose likes Game Development and the fly likes Buttons in array list moving on tile map Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Game Development
Reply Bookmark "Buttons in array list moving on tile map" Watch "Buttons in array list moving on tile map" New topic
Author

Buttons in array list moving on tile map

Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
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
Marshal

Joined: Jul 08, 2003
Posts: 23395

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.


[Jess in Action][AskingGoodQuestions]
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
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
 
 
subject: Buttons in array list moving on tile map
 
Threads others viewed
Isometric assistance needed
Need help with screen offset
What should a 2d tile-based map basically have???
Need help with centering screen
Developing Games in Java does anyone know if this book is any good?
IntelliJ Java IDE