Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

changing one array index changes whole array  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class Level, which has a 2d array of objects Tiles and the Tile class contains a tileImage (which extends my own Image class). Image has an int[] pixels and a function setPixels which looks like this;


Now the problem is, when I try (for example)

it sets that tile image for every tile of the level. I have looked at this for 2 days but I can not seem to figure out why it does that. It's driving me crazy!

If you need to see any additional code I'll post it.
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like all your array elements refer to the same object instead of different objects. Show us your code that populates the array of Tiles.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I populate the array using binary deserialisation


if, after the deserialising, I change the tileImage with another tileImage object it works fine.
so like this;
 
Marshal
Posts: 63843
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's binary deserialisation?

Yoiur lines 3 and 4 look peculiar. You have all those dots, which tells me you have non‑private fields. That is usually poor design. We would however need to see a lot more code before we can see what is wrong please. Another thing i can see is that you are alling fields on a class name: Level.levels.... That makes me suspect that you have static members of the Level class. That means you only have one instance of the field, so that brings me back to what Junilu said: always the same object.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to help people out a little. That code shows everything but an array element being set. Remember, we don't know what you know about your program. You may think what you posted obviously shows how that tiles array is set but to someone who is not familiar with your code, it's still a total mystery.

Nobody here knows what this: Level.level.get(i) actually does. We can guess that Level.level is probably a reference to a list of some sort, maybe. We can guess that level.set(i, levelToRead) is some kind of method you've written but we can't be sure how that works under the covers. But then again, those would all be wild-a** guesses.

Show us the code where a tile array element is actually set.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Level class has a static list that containts all Levels to I can for example display them all in my level editor using a for loop. Here is the Level class;


the Image Class;


the ImageTile class;


and the code that gets executed at the start of the program (only the code that relates to this topic beceuase it's a long function);


what I eventually wind up with is the I image I attached.
Schermafbeelding-2019-04-02-om-10.48.31.png
[Thumbnail for Schermafbeelding-2019-04-02-om-10.48.31.png]
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luc Drenth wrote:The Level class has a static list that containts all Levels  . . .

Even without looking at the code, I can see that as a mistake. Don't make the field public, otherwise any code can change it without your knowing. Don't make it static because, as I suggested earlier, there will only be one instance of that list anywhere. As Junilu said, any changes always affect the same object and are reflected elsewhere: where you don't want to see them. Don't make that field static because static fields don't constitute part of the object and are therefore omitted in serialisation.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback. I now moved the list of Levels to the GameManager class and made it non-static and private. It did not solve the problem though.
(I did the same for the list in the ImageTile class)
 
Rancher
Posts: 3989
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is your issue.
Every tile is the same ImageTile.
You never create a new one.
So when you modify the pixel array you are modifying the only pixel array...which all the Tile references in the tiles array are looking at.
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want something like a method to set a particular part of the display. If you are setting one pixel, you would want something like public void setPixel(int x, int y, Color colour), There are doubtless more elegant ways to do it, partcular if you are setting a large part of the display.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much guys! Problem solved (you were right Dave Tolls), code is a little bit cleaner and learned some things!
 
Do not set lab on fire. Or this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!