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

2016/11/22

Having issues with the wait() command...

DukeFutzwillus DukeFutzwillus

2016/11/22

#
Thought this looked right but keeps giving me the error, " must be caught or declared to be thrown". This is the code for the whole class. The wait is under the moveImage method. Any help I am grateful for, thanks.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CavernTank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankBoss extends SmoothMovement
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    int imgFrame = 0;   //This keeps track of which image frame it is currently showing

     public TankBoss()
     {
         image1 = new GreenfootImage("CavernTank_0.png");
         image2 = new GreenfootImage("CavernTank_1.png");
         image3 = new GreenfootImage("CavernTank_2.png");
     }
    public void act()  
    {  
        moveImage();
        
    }  
      
    
    public void moveImage() //This would be a loop going constantly to animate it  
    {  
        imgFrame++;
        
        if (imgFrame == 4)
        {
            imgFrame = 1;
            
        }
        if (imgFrame == 1)
        {
          setImage(image2);
          wait(5);
        }
        
        if (imgFrame == 2)
        {
         setImage(image3);
          
        }
        
        if (imgFrame == 3)
        {
         setImage(image1);
         
        }
        
        
        

        
        }
    }
Nosson1459 Nosson1459

2016/11/22

#
I don't think that is the way the method wait() works, maybe try this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class CavernTank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankBoss extends SmoothMovement
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    int imgFrame = 0;   //This keeps track of which image frame it is currently showing
 
     public TankBoss()
     {
         image1 = new GreenfootImage("CavernTank_0.png");
         image2 = new GreenfootImage("CavernTank_1.png");
         image3 = new GreenfootImage("CavernTank_2.png");
     }
    public void act()  
    {  
        moveImage();
         
    }  
       
     
    public void moveImage() //This would be a loop going constantly to animate it  
    {  
        int delay=0;
        imgFrame++;
         
        if (imgFrame == 4)
        {
            imgFrame = 1;
             
        }
        if (imgFrame == 1)
        {
          setImage(image2);
        }
         
        if (imgFrame == 2 && delay==5)
        {
         setImage(image3);
         delay=0;  
        }
         
        if (imgFrame == 3)
        {
         setImage(image1);
          
        }
        delay++;
         
         
         
 
         
        }
    }
I put the && delay==5 in the next if so that the delay will be before the changing to image3 which will be the same as you put it with wait(5), but I don't know how much delay this will be so you can change the delay== for a different amount of delay.
danpost danpost

2016/11/22

#
When using 'wait(int)' or 'Greenfoot.delay(int)', the entire scenario will be on hold for the allotted time. Neither is what you will want to use. You need to increase the counter to some multiple of 3 and change the image each time that multiple is reached. For example, at 10, 20 and 30 (or 0). You can use the modulus operation to determine when to change the image:
if (imgFrame%10 == 0)
Nosson1459 Nosson1459

2016/11/22

#
What will happen if he tries my coding will it work or not? So wait(int) is a blocking method?
danpost danpost

2016/11/22

#
Nosson1459 wrote...
What will happen if he tries my coding will it work or not? So wait(int) is a blocking method?
The 'wait' and 'delay' methods are no-op (no operations are performed) loops that prevent the constant flow of act cycles from executing until the loop is exited. Not even keyboard or mouse actions can be detected while they are "looping".
Nosson1459 Nosson1459

2016/11/22

#
oh
You need to login to post a reply.