This site requires JavaScript, please enable it in your browser!
Greenfoot back
Etch-a-sketch
Etch-a-sketch wrote ...

2018/6/11

Problems with variables

Etch-a-sketch Etch-a-sketch

2018/6/11

#
I'm relatively new to greenfoot and have been asked to do a falling leaves project for one of my classes. This is the code:
public class Basket extends Actor
{
    int once = 1;
    public void act()
    {
        
       if(once == 1 );
       {
            setLocation(1000,1000);
            int once = 0; 
        }
        
    int dx = 0;
        if (Greenfoot.isKeyDown("right")) dx+=2;
        if (Greenfoot.isKeyDown("left")) dx-=2;
        if (dx!=0) setLocation(getX()+dx, getY());
        if (getX()<5 || getX()>getWorld().getWidth()-6) setLocation(getX()-dx, getY());
    
    }
}
The errors are occuring with the actor not being able to move
danpost danpost

2018/6/11

#
Etch-a-sketch wrote...
I'm relatively new to greenfoot and have been asked to do a falling leaves project for one of my classes. This is the code: << Code Omitted >> The errors are occuring with the actor not being able to move
The once on line 10 is not the same once that you declared on line 3. On line 10, you are declaring a new variable that is local to the method (its scope extends only for the execution of the method). The use of int at the beginning of the line is what defines the declared variable. By removing it, the one declared on line 3 will be, by default, referenced.
You need to login to post a reply.