Learn how to a build a cloud-first strategyRegister Now


Same answer for a variable from two different methods

Posted on 2007-08-08
Medium Priority
Last Modified: 2013-11-05
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);
                   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?
Question by:James_h1023
  • 2
  • 2
LVL 14

Accepted Solution

CPColin earned 500 total points
ID: 19658350
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).

Author Comment

ID: 19658364
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?
LVL 14

Expert Comment

ID: 19658393
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.

Author Comment

ID: 19658421
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.
Thanks for your help.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question