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

2016/4/6

my counter aint increasing

Nischaya Nischaya

2016/4/6

#
import greenfoot.*; public class Nischaya extends Actor { public void act() { // Add your action code here if(Greenfoot.isKeyDown("up")) move(5); if(Greenfoot.isKeyDown("down")) move(-5); if(Greenfoot.isKeyDown("right")) turn(1); if(Greenfoot.isKeyDown("left")) turn(-1); Actor pizza = getOneObjectAtOffset(0,0,food.class); int pizzas=0; if(pizza != null) { pizzas++; getWorld().removeObject(pizza); } Actor obstruction = getOneObjectAtOffset(0,0,Obstructions.class); if(obstruction != null) { getWorld().removeObject(this); System.out.println("GAME OVER"); System.out.println("Pizzas eaten = "+pizzas); Greenfoot.stop(); } } } This is my code and the variable pizzas is not increasing. Help required.....
danpost danpost

2016/4/6

#
You have 'int pizzas = 0;' inside the act method as a variable whose scope (existence) is maintained while the method is executing. Each time the method executes, a new 'pizzas' variable is created and when the method is done executing, the variable is gone for good. To maintain the variable for a longer period of time (for the life of the Nischaya object), that line needs to be placed outside of the method.
You need to login to post a reply.