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

2018/8/30

NEED HELP ANIMATION

Mounirmoon Mounirmoon

2018/8/30

#
I wrote a code to move my character I have 9 image png I managed to make an animation but it is too fast I tried to do something with Delay. But it did not work
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Orang here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Orang extends Actor
{
    /**
     * Act - do whatever the Orang wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    GreenfootImage gambar1 = new GreenfootImage("R1.png");
    GreenfootImage gambar2 = new GreenfootImage("R2.png");
    GreenfootImage gambar3 = new GreenfootImage("R3.png");
    GreenfootImage gambar4 = new GreenfootImage("R4.png");
    GreenfootImage gambar5 = new GreenfootImage("R5.png");
    GreenfootImage gambar6 = new GreenfootImage("R6.png");
    GreenfootImage gambar7 = new GreenfootImage("R7.png");
    
    GreenfootImage gambar8 = new GreenfootImage("R8.png");
    GreenfootImage gambar9 = new GreenfootImage("R9.png");
    GreenfootImage gambar10 = new GreenfootImage("L1.png");
    GreenfootImage gambar11 = new GreenfootImage("L2.png");
    GreenfootImage gambar12 = new GreenfootImage("L3.png");
    GreenfootImage gambar13 = new GreenfootImage("standinge.png");
   
    
    public int frame = 1;
    public int waktu = 5;
    public void act() 
    {
      if (Greenfoot.isKeyDown("right"))
      {
        animasi();
        move(3);
    }
      else if (Greenfoot.isKeyDown("left"))
      {
        animasin();
        move(-2);
    }   
      else 
      {
          setImage(gambar13);
        }
}
  
    public void animasi()
    {
        Greenfoot.delay(5);
         if (frame == 1)
            {
            setImage(gambar1);
            frame = 2;
            Greenfoot.delay(2);
            }
         else if (frame == 2)
            {
            setImage(gambar2);
            frame = 3;
            Greenfoot.delay(2);
            }
         else if (frame == 3)
            {
            setImage(gambar3);
            frame = 4;
            Greenfoot.delay(2);
        }
         else if (frame == 4)
         {
             setImage(gambar4);
             frame = 5;
             Greenfoot.delay(2); 
            }
         else if (frame == 5)
         {
             setImage(gambar5);
             frame = 6;
             Greenfoot.delay(2);
            }
            else if (frame == 6)
            {
                setImage(gambar6);
                frame = 7;
                Greenfoot.delay(2);
            }
            else if  (frame == 7 )
            {
                setImage(gambar7);
                frame = 8;
                Greenfoot.delay(2);
                }
             else if (frame == 8 );
             {
                 setImage(gambar9);
                 frame = 1;
                }
             
                
         
               
                
            
            
           
        }
    public void animasin()
    {
         if (frame == 1)
            {
            setImage(gambar10);
            frame = 2;
            }
         else if (frame == 2)
            {
            setImage(gambar11);
            frame = 3;
            }
         else if (frame == 3)
            {
            setImage(gambar12);
            frame = 1;
            }
           
    }
   
}
Super_Hippo Super_Hippo

2018/8/30

#
private GreenfootImage[] images = 
{
    new GreenfootImage("R1.png");
    new GreenfootImage("R2.png");
    new GreenfootImage("R3.png");
    new GreenfootImage("R4.png");
    new GreenfootImage("R5.png");
    new GreenfootImage("R6.png");
    new GreenfootImage("R7.png");
    new GreenfootImage("R8.png");
    new GreenfootImage("R9.png");
    new GreenfootImage("L1.png");
    new GreenfootImage("L2.png");
    new GreenfootImage("L3.png");
    new GreenfootImage("standinge.png");
};


private void animasi()
{
    setImage(images[frames = (frames+1)%9]);
}
}
Mounirmoon Mounirmoon

2018/8/30

#
it doesn't want to work a error appears < class interface or enum inspected >
Super_Hippo Super_Hippo

2018/8/30

#
Umm, sorry, replace all the ';' in lines 3-14 with ',' and remove the one in line 15.
Mounirmoon Mounirmoon

2018/8/30

#
like this ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Orang here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Orang extends Actor
{
    /**
     * Act - do whatever the Orang wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
   
    
    public int frame = 0;
    public int waktu = 5;
    public void act() 
    {
      if (Greenfoot.isKeyDown("right"))
      {
        move(3);
        animasi();
        
    }
     
   
}
public GreenfootImage[] images = 
{
    new GreenfootImage("R1.png"),
    new GreenfootImage("R2.png"),
    new GreenfootImage("R3.png"),
    new GreenfootImage("R4.png"),
    new GreenfootImage("R5.png"),
    new GreenfootImage("R6.png"),
    new GreenfootImage("R7.png"),
    new GreenfootImage("R8.png"),
    new GreenfootImage("R9.png"),
    new GreenfootImage("L1.png"),
    new GreenfootImage("L2.png"),
    new GreenfootImage("L3.png"),
    new GreenfootImage("standinge.png"),
};
 
 
private void animasi()
{
    setImage(images[frame = (frame+1)%9]);
}
                
         
               
                
            
            
           
 }
 
  
Super_Hippo Super_Hippo

2018/8/30

#
Does it work?
danpost danpost

2018/8/30

#
You need to spread out the frame numbers at which your animation changes (like every 3 or 4 frames, maybe). For example:
private void animasi()
{
    frame = (frame+1)%(9*3);
    setImage(images[frame/3]);
}
You can change both 3's to a higher value to slow down the animation more (or lower them to 2's if already too slow). For animasin, you will need to add 9 inside the square brackets on the 4th line and change the 9 in the 3rd line to 3 since there are only 3 left images.
Mounirmoon Mounirmoon

2018/8/31

#
thank you very much super_hippo and danpost it work very well
Mounirmoon Mounirmoon

2018/8/31

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Orang here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Orang extends Actor
{
    /**
     * Act - do whatever the Orang wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
   
    GreenfootImage gambar13 = new GreenfootImage("standinge.png");
    public int frame = 0;
    public int waktu = 5;
    public void act() 
    {
      if (Greenfoot.isKeyDown("right"))
      {
        move(3);
        animasi();
        
      }
      else if (Greenfoot.isKeyDown("left"))
      {
          move(-3);
          animasin();
        }
       
       else
      {
          setImage(gambar13);
        }
            
     
   
}
public GreenfootImage[] imagesR = 
{
    new GreenfootImage("R1.png"),
    new GreenfootImage("R2.png"),
    new GreenfootImage("R3.png"),
    new GreenfootImage("R4.png"),
    new GreenfootImage("R5.png"),
    new GreenfootImage("R6.png"),
    new GreenfootImage("R7.png"),
    new GreenfootImage("R8.png"),
    new GreenfootImage("R9.png"),
   
};
public GreenfootImage[] imagesL = 
{
    new GreenfootImage("L1.png"),
    new GreenfootImage("L2.png"),
    new GreenfootImage("L3.png"),
    new GreenfootImage("L4.png"),
    new GreenfootImage("L5.png"),
    new GreenfootImage("L6.png"),
    new GreenfootImage("L7.png"),
    new GreenfootImage("L8.png"),
    new GreenfootImage("L9.png"),
 
};
 
private void animasi()
{
    frame = (frame+1)%(9*3);
    setImage(imagesR[frame/3]);
}
private void animasin()
{
    frame = (frame+1)%(9*3);
    setImage(imagesL[frame/3]);
}
         
               
                
            
            
           
 }
 
  
You need to login to post a reply.