A student was having issues with behavior in a program. For all my looking, I could not find the issue. After many hours, I have distilled the problem to the basics, but still need some help explaining why? First, a very basic world:
... an equally simple BadLeaf class
... and a stripped down Wombat class, looking for those bad leaves...
When the code executes, here's what happens...
Huh? Maybe it's just my eyes! Seems like the getObjectAtOffset() is behaving like getObjectsInRange()? But without the line in BadLeaf's addedToWorld(), all is fine?!?!
public WombatWorld() { super(10, 10, 60); setBackground("cell.jpg"); setPaintOrder(Wombat.class, BadLeaf.class); // draw wombat on top of leaf addObject(new Wombat(), 4, 7); addObject(new BadLeaf(), 3, 5); addObject(new BadLeaf(), 2, 9); }
public void act() { } public void addedToWorld(World MyWorld) { int imageCellSize = MyWorld.getCellSize(); GreenfootImage MyImage = getImage(); MyImage.scale(imageCellSize, imageCellSize); // removal of the following line eliminates the problem!?!?!? Actor wombat = getOneObjectAtOffset(0, 0, Wombat.class); }
public boolean facingBadleafMethod() { boolean facingBadleaf = false; int rotation = getRotation(); int xOffset = 0; int yOffset = 0; switch (rotation) { case 0: xOffset = 1; yOffset = 0; break; case 90: xOffset = 0; yOffset = 1; break; case 180: xOffset = -1; yOffset = 0; break; case 270: xOffset = 0; yOffset = -1; break; } Actor Badleaf = getOneObjectAtOffset(xOffset, yOffset, BadLeaf.class); if (Badleaf != null) { System.out.println("I am at " + getX() + "," + getY()); System.out.println("I am facing " + rotation); System.out.println("I am facing a bad leaf right" + Badleaf.toString()); System.out.println("The bad leaf is at " + Badleaf.getX() + "," + Badleaf.getY()); facingBadleaf = true; } else { facingBadleaf = false; } if (facingBadleaf == true) { System.out.println("I am facing a bad leaf \n"); return(true); } else { System.out.println("I am not facing a bad leaf \n"); return(false); } }
[b]I am at 4,7[/b] I am facing 0 I am facing a bad leaf rightBadLeaf@11cdf63 [b]The bad leaf is at 2,9[/b] I am facing a bad leaf