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

2017/9/7

Call overloaded subclass method from getOneObjectAtOffset

Comwp Comwp

2017/9/7

#
I honestly don't know exactly how to word my problem, but here it goes: I have a sort of universal selector class (just a fancy square with a translucent center) that is designed to jump around my game's menu by detecting buttons nearby it and setting it's location to the button it detects in any given direction. the buttons are all subclasses of a universal 'btn_' class, which is what the selector targets and attempts to invoke methods on. On to the actual problem: I get an error that It cannot find the method even though I have made sure it was in the btn_ class and overloaded in all subclasses with their respective individual button functions.. made sure the method was public and everything. Help?
if(Greenfoot.isKeyDown("ENTER")){ //this code is in the selector class.
            if(getOneObjectAtOffset(0,0,btn_.class)!=null){
                getOneObjectAtOffset(0,0,btn_.class).press(); // cannot find symbol - method press()
            }
        }
Comwp Comwp

2017/9/7

#
Actually i just found out how to fix it. for some reason
if(Greenfoot.isKeyDown("ENTER")){
            if(getOneObjectAtOffset(0,0,btn_.class)!=null){
                btn_ currentButton = (btn_)getOneObjectAtOffset(0,0,btn_.class);
                currentButton.press();
            }
        }
works just fine. maybe it was casting it? No idea, i am all ears at alternative or rather, quicker ways though.
danpost danpost

2017/9/7

#
Comwp wrote...
Actually i just found out how to fix it < Code Omitted > works just fine. maybe it was casting it? No idea, i am all ears at alternative or rather, quicker ways though.
Actually, casting it is what fixed it. The compiler did not know to look in the btn_ class as the reference in your first post was to an Actor type object (the return type of 'getOneObjectAtOffset') -- not a btn_ type object.
You need to login to post a reply.