Solved

# Same answer for a variable from two different methods

Posted on 2007-08-08
181 Views
Ok another silly question no doubt.
This is driving around the bend:
I have two methods in a class, one that is placing lots of objects on to a vector, and the second which looks at the objects and gets information from each object on the vector:
Like this:

public void createBackgroundSpritesVector(Point currentSectorIn) {
int tempSpriteNumber;
backgroundSpritesDetails tempSpriteDetails = new backgroundSpritesDetails();
// for each tile in the sector scan whether or not the tile's image is 0
// which means it is a sprite, and if so, add the details to the
// background sprite vector
for (int x=0; x<32; x++) {
for (int y=0; y<23; y++) {
if (gameRef.getMap().sectors[currentSectorIn.x][currentSectorIn.y].tiles[x][y].getImage() == 0) {
// it is a sprite so add the lcoation of the sprite to the vector
Point locTemp = new Point(x,y);
tempSpriteDetails.location = locTemp;
tempSpriteNumber = gameRef.getMap().sectors[currentSectorIn.x][currentSectorIn.y].tiles[x][y].getSprite();
tempSpriteDetails.totalNumber = gameRef.getMap().getSprite(tempSpriteNumber).getNumberOfImages();
System.out.println(tempSpriteDetails.location.x + "," + tempSpriteDetails.location.y);
backgroundSpritesLocationsAndDetails.push(tempSpriteDetails);
tempSpriteDetails.location = new Point(2,2);
}
}
}
}

public void drawBackgroundSprites() {
backgroundSpritesDetails tempSpriteDetails = new backgroundSpritesDetails();
BufferedImage imgTemp;
genericSprite spriteTemp;
int spriteNumberTemp;
// cycle through all the sprites and draw there current images
// to the position on the screen, ie. the tileb
for (int i=(backgroundSpritesLocationsAndDetails.size())-1; i>=0; i--) {
// get the information for the sprites location!
tempSpriteDetails = (backgroundSpritesDetails) backgroundSpritesLocationsAndDetails.pop();
spriteNumberTemp = gameRef.getMap().sectors[gameRef.getCurrentSector().x][gameRef.getCurrentSector().y].tiles[tempSpriteDetails.location.x][tempSpriteDetails.location.y].getSprite();
// draw the sprite
imgTemp = gameRef.getMap().getSprite(spriteNumberTemp).getImage(1);
gTemp = gameRef.getBufferStrategy().getDrawGraphics();
if (!gameRef.getBufferStrategy().contentsLost()) {
gTemp.drawImage(imgTemp, (tempSpriteDetails.location.x*25), (tempSpriteDetails.location.y*25)+25, null); // draw the tile to the screen
}
}
gameRef.getBufferStrategy().show(); // flip the graphics
gTemp.dispose(); // dispose of the buffer to write to it again
}

Now the problem is - when i place the objects onto the Vector the tempSpriteDetails.location (Point) is different for each one which is what i want, but when i pop them back off again, each one has the location (Point) of the last object i placed onto the Vector!

Any ideas?
0
Question by:James_h1023

LVL 14

Accepted Solution

You're going to want to create a new tempSpriteDetails object each time you push it onto the stack. Otherwise, you're pushing the same one each time, and they all get updated when you set the location. Also, this line:

tempSpriteDetails.location = new Point(2,2);

looks like it's setting all the locations to (2,2) instead of (x,y).
0

LVL 4

Author Comment

Excellent thats fixed it - i'm quite sure why though, cause wasn't i overwritin the location in the tempSpriteDetails when i assigned the new location before pushing it?

Ah yeh that line was for some testing i've taken that out!
It works, just confusing, y now?
0

LVL 14

Expert Comment

Yeah, it gets confusing sometimes. The thing to remember is that the push() method doesn't make a copy of the object you pass in, so any later changes you make to that object change the object that's in the stack too, since they're the same. I know I've gone around in circles multiple times for long stretches of time before figuring it out.
0

LVL 4

Author Comment

So it basically just makes like a refernce link to where it is in memory, so that when you access the stack it points to that?!  Thats a bit silly, but i will deffo have to remember that.
0

## Featured Post

### Suggested Solutions

Problem to setup 18 67
What is the compatible hibernate version for spring 4.3.1 10 50
hasOne  challenge 59 65
java set up 1 37
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.