Tile Placing

Listen… I’ve just read through this thread and it seems you guys are having a bit of a hard time communicating, so I’ll give it a go.

You have a Skyscraper

Skyscraper s

declared as a variable inside your main class. This is also the one that you are rendering, by getting an image stored inside that ONE instance of Skyscraper.

When you are testing whether

VK_SPACE

has been pressed is where your little problem occurs. You want to place a new skyscraper, I’m guessing? The problem is you aren’t creating another instance of Skyscraper and are instead moving the existing one.

s.placeSkyscraper()

is only ever going to move

s

. This means that you’re going to need a new way to store, create and render your skyscrapers.

Let’s start with storing.
Many, many people have recommended an

ArrayList

in this thread, and I will as well because they are incredibly easy to use. So, we need to declare one in the same place you’ve declared

s

. Next, you need to update your render loop to accommodate for the fact that you now need to iterate through a list to get your skyscrapers:


for (Skyscraper sky : muhskyscrapers){
 //use sky here as you would have used s before…
}

So we have ourselves a brand new ArrayList. The problem is we haven’t added anything to it. This can be fixed in your

keyPressed

function. Instead of s.placeSkyscraper, do this:

Skyscraper s = new Skyscraper();
s.placeSkyscraper(x, y);
muhskyscrapers.add(s);

I’d also recommend adding two parameters to the Skyscraper constructor to take an x and y value, just for convenience.

:’( A thread… about placing tiles. Alright, I’ll work with it.

This part is good, you need to store the TileX and TileY for each tile.


package com.darkcart.game.cb;

import java.awt.Image;
import javax.swing.ImageIcon;

public class Skyscraper {

   private Image skyscraper;
   private int tileX, tileY;

   public Skyscraper() {

      ImageIcon img = new ImageIcon();
      img = new ImageIcon("res/skyscraper.png");
      skyscraper = img.getImage();

      tileX = 4;
      tileY = 0;
   }

   public Image getSkyscraper() {
      return skyscraper;
   }

   public int getTileX() {
      return tileX;
   }

   public int getTileY() {
      return tileY;
   }

   public void placeSkyscraper(int skyX, int skyY) {
      tileX = skyX - 1;
      tileY = skyY - 1;
   }

}

Okay, the next step is actually storing each Skyscraper. You see, in Java, you have to make a new Object for every item that you have so doing something like [icode]Skyscraper s = new Skyscraper()[/icode] only makes one Skyscraper. Regardless of how many [icode]Skyscraper s = new Skyscraper()[/icode] you call, you will literally only have one “s”. So that Skyscraper will move around the map, as in your upper example…


- Skyscraper s = new Skyscraper();

+ ArrayList<Skyscraper> listOfSkyscrapers = new ArrayList<Skyscraper>();

You want to use a list of skyscrapers in order to solve your problem. It will allow you to make multiple Skyscrapers under the same roof that are all different entities.



//---------------------------------------------------------------------------------------
//CREATE FIRST SKYSCRAPER
//---------------------------------------------------------------------------------------

//To add a new skyscraper to the list
//Skyscraper (Index 0)
listOfSkyscrapers.add(new Skyscraper());

//---------------------------------------------------------------------------------------
//TO EDIT SKYSCRAPERS
//---------------------------------------------------------------------------------------

//To edit that skyscraper
Skyscraper temp = listOfSkyscrapers.get(0);

//To edit variables
temp.placeSkyscraper(4,5);

//To place skyscraper back
listOfSkyscrapers.set(0, temp);

//---------------------------------------------------------------------------------------
//TO MAKE ANOTHER NEW SKYSCRAPER (Index 1)
//---------------------------------------------------------------------------------------
//Skyscraper (Index 1)
listOfSkyscrapers.add(new Skyscraper());

//---------------------------------------------------------------------------------------
//TO EDIT SKYSCRAPER (Index 1)
//---------------------------------------------------------------------------------------

//To edit that skyscraper
//temp was declared in above statement....
temp = listOfSkyscrapers.get(1);

//To edit variables
temp.placeSkyscraper(2,3);

//To place skyscraper back
listOfSkyscrapers.set(1, temp);

//---------------------------------------------------------------------------------------
//Etc.
//---------------------------------------------------------------------------------------

//Repeat "To Make Another Skyscraper" Again, adding one to index each time...

//---------------------------------------------------------------------------------------
//TO DRAW ALL SKYSCRAPERS IN LIST
//---------------------------------------------------------------------------------------
//To draw all contained in the list

public void paint(Graphics g) 
{
     for (int i = 0; i < listOfSkyscrapers.size(); i++)
     {
	    g.drawImage(listOfSkyscrapers.get(i).getSkyscraper(), 
                              listOfSkyscrapers.get(i).getTileX() * 45, listOfSkyscrapers.get(i).getTileY() * 45, 
                              null); 
     }
}


This should hopefully be more than needed to get the ball rolling. Arrays exist to make sure you can do copy and pasting easier within code. Learning how to use them (and functions) is critical if you don’t want to type out different variables for each item you want on your map.

Edit: ninja’d… but just as well, now he has two examples…

Ok, so now that I’ve got the ArrayList code under my belt (thanks, all), I now need the line


temp.placeSkyscraper(2, 4);

to place the skyscraper at the cursor’s position. How can I do that?


//Some code...
ArrayList<Skyscraper> skyscraperList = new ArrayList<Skyscraper>();
//Some code...


temp = new Skyscraper();
temp.placeSkyscraper(cursor.getX(), cursor.getY());
skyscraperList.add(temp);

???