This site requires JavaScript, please enable it in your browser!
Greenfoot back
MrCohen
MrCohen wrote ...

2018/6/13

GetObjects returning two of the same object?!

MrCohen MrCohen

2018/6/13

#
I'm having an issue with a students' project. The student is adding an object to their game. Let's call it a Shooter. However, the students are monitoring object counts using getObjects(Shooter.class).size() For some reason, when a single object is created and added to the World, the getObjects class has a size of 2. I used the following code to try to troubleshoot:
ArrayList<Shooter> shooters = (ArrayList<Shooter>)getObjects(Shooter.class);
for (Shooter s : shooters){
 System.out.println(s.getX() + ", " + s.getY());
}
System.out.println(shooters.get(0) == shooters.get(1));
The x and y coordinates are identical. The second output statement outputs true. There is only one object visibly in the world, and dragging it away does not reveal a second object. I'm very confused. What could result in the getObjects method getting the same object twice like this? (This is one of my students, working on a school computer, on Greenfoot 2.3.0)
danpost danpost

2018/6/14

#
The List object generated in my version of 2.3.0 does not double up on any Actor objects as suggested by your results. Nor does the source for 2.3.0 look like it would create your results (I have a downloaded version of the source for v2.3.0 and reviewed it). Can you duplicate the same results with a simple scenario (adding one object and testing)? If so, post the classes created here for review.
Yehuda Yehuda

2018/6/14

#
...and if these results only occur by this scenario then you should post the codes relevant to the problem.
MrCohen MrCohen

2018/6/14

#
I have worked with getObjects() many times and I have never run into this before. Really not sure how/why it's happening. I will get the source code and post it if I can get the students' permission.
MrCohen MrCohen

2018/6/14

#
Here is a link to the project (with my students' permission): http://www.greenfoot.org/scenarios/21739 The relevant code is in the World (just search for Shooter and you can see where the Shooter gets added to the World). If you start playing the game, and then put down a shooter (any of the 3 types, but choosing one from the bottom and then clicking on a spot to put it) and then pause you can inspect and you'll see a single object, but if you run getObjects(Shooter.class) and examine it's content (using the code I posted above), you'll end up where I was when I wrote the original post here.
danpost danpost

2018/6/14

#
MrCohen wrote...
I'm very confused. What could result in the getObjects method getting the same object twice like this?
Apparently, this behavior is caused by having the class listed multiple times in the setPaintOrder command.
MrCohen MrCohen

2018/6/15

#
dankest wrote...
Apparently, this behavior is caused by having the class listed multiple times in the setPaintOrder command.
I bow to your never-ending wisdom, danpost. You're amazing. Thanks.
You need to login to post a reply.