Tile Placing

Hello JGO,

I have a problem. I am making a game where when you press the space bar, it places a tile where the player is. The problem is, when I try to place more than one tile, it moves the existing tile (see below) instead of creating a new one. The way I place a tile is below:


public void placeSkyscraper(int skyscraperX, skyscraperY) {
tileX += skyscraperX // tileX is the x position of the skyscraper
tileY += skyscraperY // tileY is the y position of the skyscraper
}

The way I call the placeSkyscraper method is below:


if (keyCode == KeyEvent.VK_SPACE) {
s.placeSkyscraper(c.getTileX() + 1, c.getTileY() + 1); // c is the object I use for my player.

}

EDIT:
Also, I have this line that draws the skyscraper graphic:

g.drawImage(s.getSkyscraper, s.getTileX() * 45, s.getTileY() * 45, null);

Does anyone know what’s going on?

Thanks in advance,

-DarkCart

It seems to me that you are not fully understandinging what you are doing. You haven’t posted all relevant code, so i can only guess. I think your problem is that you have only one instance of your skyscraper (the variable s in the second method). You don’t create anything in the methods you posted. You only change the coordinates, which would explain why the existing tile is moved. You have to create an instance for each tile you want to create…

You should have an ArrayList storing the coordinates of the skyscrapers.


ArrayList<Integer> tileX = new ArrayList<Integer>();
ArrayList<Integer> tileY = new ArrayList<Integer>();

public void placeSkyscraper(int skyscraperX, skyscraperY) 
{
       tileX.add(skyscraperX);
       tileY.add(skyscraperY);
}

Look up how to use arrayLists as they are very usefull in gamemaking :slight_smile:
Hope this helps :slight_smile:

Edit:
atombrot is right by the way.
All you are doing now is adding two coordinates together.


int i = 8;
int j = 3;

i += j; //i is now equal to 11 (8+3)

Or just have an array list of skyscrapers…

Yeah but in his case he is simply using integers to store the skyscrapers, so I think he should implement it like that first, and then maybe later expand it :slight_smile:

When I try to run the game, the line

g.drawImage(s.getSkyscraper(), s.getTileX(), s.getTileY())

does not compile. How would you use the ArrayList code in this line?

Okay, I’m going to try and give you an idea on what you should try.
The problem: You aren’t really creating a new one object, just moving that object to the new coordinates.
The Fix (or The Idea): Create a system of LinkedList and Entities and initialize (in your play state) each seperate one when added. Something like “handler.addObject(param);”, and give each different object a unique object ID.

Hopefully, that made sense. It’s what I use.

Example Code?

Take a step back and read up on how to use arrays and array lists… it appears to be limiting you at the moment.

Alright, now I’ve got this:

I did that by removing the


tileX += skyX;
tileY += skyY;

part of the code and replacing it with


tileX = skyX -1;
tileY = skyY -1;

Now, it’s just keeping the skyscrapers in the place that they are placed. Any ideas on how to do that?

Either you have all you tiles have an ID, or you make an array of objects that store their position. :wink:

You need to keep a list of skyscrapers and every time they press space, add a new skyscraper to that position. When you render, iterate through each skyscraper and render it.

EDIT: I had given the whole code on how to do it. But then you won’t learn anything, so I am removing it. I will give you some help instead.

You need to use a wonderful thing called ArrayList.

There is really no point in providing code like this, the op will never learn anything.

ArrayLists are basic Java, they are imo one of the simplest collections to use. Simply add stuff to it and let it go, everything else is handled.

The OP needs to revise a little more. No offense OP, I too had problems like this at first, I took a massive step back and spent a month or so studying Java. I understood how to init and use various Java objects, how to use them in a game was another thing.

I am not quite sure you are past the initial part yet.

Ummmmm… Sorry :stuck_out_tongue:

I understand how ArrayLists work, but when I try to adapt my code for them, it breaks more existing code than it’s worth.
For example, the line


g.drawImage(s.getSkyscraper(), s.getTileX() * 45, s.getTileY() * 45, null); // This works because tileX and tileY are ints.

produces a syntax error because the drawImage() method works like this


g.drawImage(image, int, int, observer);

It may seem n00b-ish using Java2D, but I’d rather not fool around with natives and such.

Do you have a loop that goes through all your scyscrapers in the list?

I don’t truly believe that you understand how they work…

Edit:
Also we have no idea how the rest of your code is working so it is hard to give you a specific answer :confused:

Break it down to pseudocode (the raw algorithm):

Onclick {
    add skyscraper to list of skyscrapers at current position
}

Render {
    loop through list of skyscrapers:
        for each skyscraper, draw it at it's position
}

There are several ways to do it, but the most understandable is probably to create a Sprite type:


class Sprite {
    int tileX, tileY;
    Image image;
    ...

    public Sprite(int tileX, int tileY, Image tex) {
        this.tileX = tileX;
        this.tileY = tileY;
        image = tex;
    }
}

List<Sprite> buildings;

...

if (keyCode == KeyEvent.VK_SPACE) {
    buildings.add(new Sprite(c.getTileX() + 1, c.getTileY() + 1, skyscraperTexture)); // or whatever the location needs to be
}

...

for(Sprite s : buildings)
    g.drawImage(s.image, s.tileX, s.tileY, null);

The “generic” Sprite type also allows you to place any type of building by only changing one line of code:

if (keyCode == KeyEvent.VK_SPACE) {
@@    buildings.add(new Sprite(c.getTileX() + 1, c.getTileY() + 1, getSelectedBuildingType().getImage()));
}

I assume that s is an ArrayList of the skyscrapers?

If so, then you need to loop through each one.

for(int i = 0; i < s.size(); i++)
{
     Skyscraper tmp = s.get(i);
     g.drawImage(tmp.getSkyscraper(), tmp.getTileX() * 45, tmp.getTileY() * 45, null);
}

You can also use a for each loop.

for(Skyscraper tmp : s)
     g.drawImage(tmp.getSkyscraper(), tmp.getTileX() * 45, tmp.getTileY() * 45, null);

We’re going to need some more code if you want any more help

Alright, here’s all the code relating to the Skyscraper.

First of all, here’s the actual Skyscraper class. (Unmodified)


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;
	}

}

Next, here are all the mentions of the above class in the rest of the code.


private Skyscraper s;
...
s = new Skyscraper();

The paint(Graphics g) method


public void paint(Graphics g) {
 // Blah, blah, blah
 g.drawImage(s.getSkyscraper, s.getTileX() * 45, s.getTileY() * 45, null); 
}

and the KeyListener


public void keyPressed(KeyEvent e) {
int KeyPressed = e.getKeyCode();
...
if (keyPressed == KeyEvent.VK_SPACE) {
     s.placeSkyscraper(c.getTileX() + 1, c.getTileY() +1);
    }
}


Does this help at all?