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

2015/3/15

getActorAtOffset method?

Holladerwaldelf Holladerwaldelf

2015/3/15

#
hi guys, is there an easy way to write something like the getOneObjectAtOffset-method for a specific actor-class? Say that i have a class called "monster"
public class Monster extends Actor
and another actor-class-object wants to get and use a monster-object next to itself with a method like this:
public Monster getMonsterInFront()
    {Monster b;
     b = getOneObjectAtOffset(xOffset,yOffset,Monster.class);
     return b;}
if i try it like this, the compiler complains about "incompatible types" because the getOneObjectAtOffset-method doesn't return a monster-class-object but some other class (which one?). can anybody help?
danpost danpost

2015/3/15

#
The method is declared to return a reference of type 'Actor' -- not of type 'Monster'. The compiler cannot set the variable 'b', which is declared to hold an object of type 'Monster', with an object that is only known by it to be of type Actor. You must inform the compiler that the object returned is indeed of type 'Monster' before setting 'b' with it.
b = (Monster)getOneObjectAtOffset(xOffset, yOffset, Monster.class);
Placing '(Monster)' in front of the value allows the compiler to look at the value as that type (if the value does not conform to the type an error will ensue during runtime -- I think an InvalidClassCastException would be thrown).
Holladerwaldelf Holladerwaldelf

2015/3/15

#
thanks a lot! now it works.
You need to login to post a reply.