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:

1 2 3 4

publicvoidplaceSkyscraper(intskyscraperX, skyscraperY) {tileX += skyscraperX// tileX is the x position of the skyscrapertileY += skyscraperY// tileY is the y position of the skyscraper}

The way I call the placeSkyscraper method is below:

1 2 3 4

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

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...

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.

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.

"This code works flawlessly first time and exactly how I wanted it"Said no programmer ever

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

1

Skyscrapers

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

1

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.

1

s.placeSkyscraper()

is only ever going to move

1

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

1

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

1

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:

1 2 3

for (Skyscrapersky : 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

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

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

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

//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

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.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org