Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tile Placing  (Read 1019 times)
0 Members and 1 Guest are viewing this topic.
Offline DarkCart

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Posted 2014-06-20 14:18:44 »

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:

1  
2  
3  
4  
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:

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.

}



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

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


Does anyone know what's going on?

Thanks in advance,

-DarkCart


Offline atombrot

Senior Member


Medals: 10
Projects: 1



« Reply #1 - Posted 2014-06-20 14:58:38 »

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

Senior Member


Medals: 6
Projects: 2
Exp: 3 years


I do gamez, yes!


« Reply #2 - Posted 2014-06-20 15:11:19 »

You should have an ArrayList<Integer> storing the coordinates of the skyscrapers.

1  
2  
3  
4  
5  
6  
7  
8  
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 Smiley
Hope this helps Smiley

Edit:
atombrot is right by the way.
All you are doing now is adding two coordinates together.
1  
2  
3  
4  
int i = 8;
int j = 3;

i += j; //i is now equal to 11 (8+3)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2014-06-20 15:14:49 »

Or just have an array list of skyscrapers...

Offline Hermasetas

Senior Member


Medals: 6
Projects: 2
Exp: 3 years


I do gamez, yes!


« Reply #4 - Posted 2014-06-20 15:16:44 »

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 Smiley
Offline DarkCart

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #5 - Posted 2014-06-20 19:16:48 »

You should have an ArrayList<Integer> storing the coordinates of the skyscrapers.

1  
2  
3  
4  
5  
6  
7  
8  
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 Smiley
Hope this helps Smiley

Edit:
atombrot is right by the way.
All you are doing now is adding two coordinates together.
1  
2  
3  
4  
int i = 8;
int j = 3;

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


When I try to run the game, the line
1  
g.drawImage(s.getSkyscraper(), s.getTileX(), s.getTileY())

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

Offline syszee

Junior Member


Medals: 1



« Reply #6 - Posted 2014-06-20 20:50:29 »

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

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #7 - Posted 2014-06-20 21:25:51 »

Example Code?

Offline LiquidNitrogen
« Reply #8 - Posted 2014-06-20 21:57:03 »

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

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #9 - Posted 2014-06-21 17:48:52 »

Alright, now I've got this:

Click to Play


I did that by removing the

1  
2  
tileX += skyX;
tileY += skyY;


part of the code and replacing it with

1  
2  
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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #10 - Posted 2014-06-21 18:09:22 »

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

Offline ForeseenParadox

Senior Newbie


Medals: 1
Exp: 1 year



« Reply #11 - Posted 2014-06-21 18:14:16 »

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.
Offline PocketCrafter7
« Reply #12 - Posted 2014-06-22 08:36:03 »

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.

Nothing is difficult in this world. It is just how you look at it.
Offline Gibbo3771
« Reply #13 - Posted 2014-06-22 09:50:33 »

*Snip*

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
Offline PocketCrafter7
« Reply #14 - Posted 2014-06-22 09:52:34 »

Ummmmm.... Sorry Tongue

Nothing is difficult in this world. It is just how you look at it.
Offline DarkCart

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #15 - Posted 2014-06-22 15:38:14 »

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

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


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

Offline Hermasetas

Senior Member


Medals: 6
Projects: 2
Exp: 3 years


I do gamez, yes!


« Reply #16 - Posted 2014-06-22 15:54:37 »

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 :/
Offline BurntPizza
« Reply #17 - Posted 2014-06-22 16:16:11 »

Break it down to pseudocode (the raw algorithm):

1  
2  
3  
4  
5  
6  
7  
8  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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:

1  
2  
3  
if (keyCode == KeyEvent.VK_SPACE) {
   buildings.add(new Sprite(c.getTileX() + 1, c.getTileY() + 1, getSelectedBuildingType().getImage()));
}
Offline Longarmx
« Reply #18 - Posted 2014-06-22 16:23:30 »

I assume that s is an ArrayList of the skyscrapers?

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

1  
2  
3  
4  
5  
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.

1  
2  
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

Offline DarkCart

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #19 - Posted 2014-06-22 19:40:55 »

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

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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.

1  
2  
3  
private Skyscraper s;
...
s = new Skyscraper();


The paint(Graphics g) method

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


and the KeyListener

1  
2  
3  
4  
5  
6  
7  
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?

Offline Coldstream24
« Reply #20 - Posted 2014-06-22 20:21:20 »

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  
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
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 (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
1  
keyPressed
function. Instead of s.placeSkyscraper, do this:
1  
2  
3  
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.

Yes, I am that same incredibly famous Internet musician.
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #21 - Posted 2014-06-22 20:21:50 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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
Skyscraper s = new Skyscraper()
only makes one Skyscraper. Regardless of how many
Skyscraper s = new Skyscraper()
you call, you will literally only have one "s". So that Skyscraper will move around the map, as in your upper example...

1  
2  
3  
- 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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
//---------------------------------------------------------------------------------------
//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...

Offline DarkCart

JGO Knight


Medals: 15
Projects: 5
Exp: 1 year


Power of the mind over flesh


« Reply #22 - Posted 2014-07-04 12:04:50 »

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

1  
temp.placeSkyscraper(2, 4);


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

Offline tom_mai78101
« Reply #23 - Posted 2014-07-04 15:07:09 »

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


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

Huh
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!