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

2012/10/26

Cannot find symbol - variable __________

solokeh solokeh

2012/10/26

#
I am having a problem where I do everything exactly as the tutorial that teaches you how to make a crab eat worms, says, (except for a couple name changes) n=but it gives me a "cannot find symbol- variable spider. here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class blue_car here. * * @author (your name) * @version (a version number or a date) */ public class blue_car extends Actor { /** * Act - do whatever the blue_car wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { { if(Greenfoot.isKeyDown("d")) turn(1); } if(Greenfoot.isKeyDown("a")) { turn(-1); } if(Greenfoot.isKeyDown("w")) { move(1); } if(Greenfoot.isKeyDown("s")) { move(-1); } { if(Greenfoot.isKeyDown("right")) { turn(1); } if(Greenfoot.isKeyDown("left")) { turn(-1); } if(Greenfoot.isKeyDown("up")) { move(1); } if(Greenfoot.isKeyDown("down")) { move(-1); } /** * Eat a spider. */ { Actor leaf = getOneObjectAtOffset(0, 0, spider.class); if(spider != null) { getWorld().removeObject(spider); spidersEaten = spidersEaten + 1; } } } } }
erdelf erdelf

2012/10/26

#
You call the variable spider here two times, but you never made one called spider.
if(spider != null) { getWorld().removeObject(spider); spidersEaten = spidersEaten + 1; }
Gevater_Tod4711 Gevater_Tod4711

2012/10/27

#
to fix the problem you should first add the variable spidersEaten in the top of your class (above the act method) and you should change the code erdelf already sad like this:
Actor spider = getOneObjectAtOffset(0, 0, spider.class);
if(spider != null) {
    getWorld().removeObject(spider);
    spidersEaten++; 
}
solokeh solokeh

2012/10/27

#
thank you, problem fixed.
You need to login to post a reply.