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

2014/3/31

Wrong and Right code

1
2
SullyFish SullyFish

2014/4/29

#
I tried moving the counter to the character "lime" but once again I dont know how to make every chest only raise the count once so I currently have both counters going on and they both dont work like Id like them to. Lime=
     private void findChest()
    {
        if ( canSee(Chest.class) ) 
        {
            find(Chest.class);
            
            counter = counter + 1;
            System.out.println(counter);
            Greenfoot.delay(5);
            if (counter == 8) 
            {
                Greenfoot.stop();
            }
        }
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    public void find(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
    }
}

and the chest
    public void checkOpen()    
{  
    ppen = getOneObjectAtOffset(0,0, Lime.class);  
    if (ppen != null)  
    {  
        if (isSpecial() && getImage() != image2)  
        {  
            setImage(image2);  
            Greenfoot.playSound("ZeldaChest.wav");  
            Greenfoot.stop();  
         }   
        if (!isSpecial() && getImage() != image1)  
        {  
            setImage(image1);  
            counter = counter + 1;    
            Greenfoot.delay(5); 
            counter += fails;
            
            if (counter == 10) Greenfoot.stop();  
        }  
    }   
} 
}
danpost danpost

2014/4/29

#
Change line 9 in the Chest class to 'static int counter;'. This will allow all chest objects to share the same field instead of having separate fields with the same name. Also, in your world subclass, put 'Chest.counter = 0;' in the constructor. This is required because the value is only reset when the project is compiled. Resetting the project does not reset the static field.
SullyFish SullyFish

2014/4/30

#
Okay thank you very much! everything works perfectly!
You need to login to post a reply.
1
2