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

2020/3/11

Getting value from an object of same class

jay4221 jay4221

2020/3/11

#
I'm new to java and greenfoot and i'm trying to make the game 2048. I'm trying to get the merging of two objects to work right now and I'm not sure how. I need to retrieve the integer value held by an object at a specific location and return that value. If anyone needs to see my code to try and fix it I can add it or if I'm not being clear enough.
footpickle footpickle

2020/3/11

#
mAYBE uR JuST STOopID hAHaHahA loLOlOlOlOLOlOlooLol XD
Yehuda Yehuda

2020/3/11

#
What are you trying to do, and what went wrong with what you tried?
danpost danpost

2020/3/11

#
Normally, it would look something like this:
Actor actor = getOneObjectAtOffset(dx, dy, NumberBlock.class);
if (actor != null)
{
    int otherValue = ((NumberBlock)actor).getValue();
    // etc.
where NumberBlock represents the Actor subclass that displays the numbers (the name of the class this code is in) and getValue is a method in the class that returns the current value assigned to an instance of the class. The dx and dy are the offsets from where this actor is to where the possible actor is whose value it wants to get (usually plus or minus one for one and zero for the other).
You need to login to post a reply.