-3

So im tring to write a section of code that cycles through a linked list which contains different instances of a class, picks out instances with a specific ID, runs a method contained within that instance class and changes a variable which is specific to that instance. However, I cannot seem to access the methods contained within the given object. Any ideas on how to access methods from other classes?

public void tick() {
    for(int i=0; i < handler.object.size() ;i++) {
        GameObject tempObject = handler.object.get(i);
        if (tempObject.getId() == ID.FlashCardCover){
            //run a method in FlashCardCover class on this individual object
        }
    }   
}
Tempelis1
  • 3
  • 2
  • 4
    No one will be in any position to help you unless you post complete code. – hfontanez Jun 10 '19 at 17:33
  • It's not even clear to me what you mean by *"run a method in FlashCardCover class on this individual object"*. What method are you trying to call? Where is it defined? Is it `static` or instance-based? If the latter, do you have an instance of that object? What specifically do you try and how specifically does it fail? If you're just asking how to call a method, well, where do you call this `tick()` method? However you're doing that, that's how you call a method. You're also successfully calling the methods `size()` and `get(int)` and `getId()` in this code. Those are how you call a method. – David Jun 10 '19 at 17:35
  • Does FlashCardCover extend or implement GameObject? Is tempObject an instance of FlashCardCover because is has that ID? If so, then cast tempObject to FlashCardCover and you will be able to call the method. – jalynn2 Jun 10 '19 at 17:38
  • Please refer to this: https://stackoverflow.com/help/minimal-reproducible-example and edit your post accordingly – Harshal Parekh Jun 10 '19 at 19:27

2 Answers2

0

Answer to above comments is important to answer your question correctly, going by your comment in code snippet "run a method in FlashCardCover class on this individual object" looks like GameObject extents or implements FlashCardCover, if that assumption is correct then tempObject.invoke(...) , with all required parameters to this should do the trick assuming this method is visible.

Rai
  • 136
  • 7
0

Your original code:

public void tick() {
    for(int i=0; i < handler.object.size() ;i++) {
        GameObject tempObject = handler.object.get(i);
        if (tempObject.getId() == ID.FlashCardCover){
            //run a method in FlashCardCover class on this individual object
        }
    }   
}

If I interpret this correctly, you have some GameObjects and one of the kinds of GameObject is a FlashCardCover. You determine this by checking the "Id" (which I would call "TypeId" or just "Type")

So as you loop through your GameObjects, you want to call some method when that particular GameObject is in fact a FlashCardCover. You would do that by invoking the method on that object, say like tempObject.uncoverCard() — but of course you can't do that because a GameObject doesn't have an uncoverCard method.
This is where you use a cast

public void tick()
{
    for (int i=0; i<handler.object.size(); i++)
    {
        GameObject tempObject = handler.object.get(i);
        if (tempObject.getId() == ID.FlashCardCover) {
            FlashCardCover c = (FlashCardCover) tempObject;
            c.uncoverCard();
        }
    }   
}

You have cast the tempObject to be a FlashCardCover and that is an object on which you can call your "uncoverCard" method.
The cast doesn't have to be done in two lines (I did that for clarity) -- you can cast and call at the same time, such as ((FlashCardCover) tempObject).uncoverCard()

Note that this all assumes that FlashCardCover is a subclass of GameObject, e.g.

public class FlashCardCover extends GameObject { ... }

If it doesn't you'll get a ClassCastException

Now with all that explained ... you don't need your own type codes to do this sort of casting, you can use instanceof instead.

if (tempObject instanceof FlashCardCover) {
    // do your thing
}
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • Thank you. Despite my poor description of the problem you managed to find a solution to it. Much appreciated – Tempelis1 Jun 11 '19 at 17:35