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

2020/4/14

Only use an Actor if at a certain score

sarhas22 sarhas22

2020/4/14

#
Hi, I'm trying to make my code use a certain actor only when the user is at a certain level. Basically, this actor is kind of like the a "shooting" device. In Level 1, the user is not allowed to use a shooter, but they should only be allowed to in levels 2 and 3. I am getting an error on line 3 of the code below (where the if-statements are), the compiler cannot find the symbol of ".level" even though it is in my world-class. Is there something syntactically wrong I am doing here? public void act() { if(getWorld().level == 2 || getWorld().level == 3) { setLocation(getX() + speed, getY()); if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); } else { Actor virus = getOneIntersectingObject(Virus.class); if(virus !=null) { getWorld().removeObject(virus); getWorld().removeObject(this); } } } }
danpost danpost

2020/4/14

#
sarhas22 wrote...
Is there something syntactically wrong I am doing here? << Code Omitted >>]
Well, yes. The getWorld method returns a World object which may or may not be a MyWorld object. The compiler is not going to look in all (or any of) your subclasses of World hoping to find level. If it did, it would certainly be a problem if more than one subclass had a level field. You must let the compiler know what type world you want level from:
if (((MyWorld)getWorld()).level > 1) {
You need to login to post a reply.