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

2013/8/4

GetOneObjectAtOffset Help...

1
2
danpost danpost

2013/8/4

#
It would be easiest to put the color-changing code in the Yellow class, but, if you must put the code in the Main class, you will need to create a loop to iterate through the nearby Yellow objects and set their colors. The range chosen to create the list of objects to iterate through needs to be somewhat larger than the range at which the color-changing takes place (at least by the largest change in distance between the Main object and a Yellow object in one act cycle). As an alternative, you could use 'getWorld().getObjects(Yellow.class)' to get a list of all Yellow objects in the world. The objects returned in the list will need to be typecast at least to the Actor class to enable changing its image. Then, within the loop, if not outside the color changing distance, which will have to be determined by Maths using 'Math.hypot', set to alternate color; else, set to original color. Putting the code in the Yellow class would be a breeze in comparison. There would be no need for listing of objects, no need for a loop, and no need for typecasting.
TippierNba TippierNba

2013/8/4

#
Okay thanks so much for all this, i am really learning. My question is on line 1, what does (Yellow)rightwall3 mean and do? because i just get a cannot find symbol error.
danpost danpost

2013/8/4

#
Please show the code around which this line is located. Without scope, it is difficult to know how to fix the code.
Make sure you typed "rightWall3", instead of "rightwall3" (your code had it as "rightWall3" before).
danpost danpost

2013/8/4

#
TippierNba wrote...
Okay thanks so much for all this, i am really learning. My question is on line 1, what does (Yellow)rightwall3 mean and do? because i just get a cannot find symbol error.
'rightwall3' (or 'rightWall3') is a field holding a Yellow object; however, is only known to the compiler as being an Object or Actor object (depending on how the object reference was acquired). The '(Yellow)' prefix tells the compiler that the object is indeed a Yellow object. After execution of the typecasting, the compiler will know to look in the Yellow class for methods or fields related to that object (before execution, trying to call a method or access a field in the Yellow class on the object will cause an error of 'cannot field symbol' type).
TippierNba TippierNba

2013/8/4

#
int Width = getImage().getWidth(); int xDis = (int)(Width/2); Actor rightWall = getOneObjectAtOffset(-xDis,0,Green.class); Actor rightWall1 = getOneObjectAtOffset(-xDis,0,Red.class); Actor rightWall2 = getOneObjectAtOffset(-xDis,0,Blue.class); Actor rightWall3 = getOneObjectAtOffset(-xDis,0,Yellow.class); if(rightWall1 ==null)//||rightWall == null||rightWall3 == null||rightWall2==null) { return false; } else { Yellow yellowall = (Yellow)rightWall3; Yellow.Change();// setLocation(getX()+1,getY()); return true; } } Now i get the error non-static method Change() cannot be referenced from a static contex. Also thank you for explaining the previous question.
danpost danpost

2013/8/4

#
What class do the classes Green, Red, Blue and Yellow extend? Sidenote: the error is because you are calling the 'Change' method on a class name and not on an object of that class.
To clarify on what danpost was say, change Yellow.Change() to yellowall.change. Why did you comment out the rest of the if statement?
danpost danpost

2013/8/4

#
@FlyingRabidUnicornPig, I was going to mention that as well as the fact that the conjoiner should be '&&" (not '||'); but the question I asked was intended to deal with that part of the code.
You need to login to post a reply.
1
2