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

2016/11/14

i need help slowing down the transition between two images to create an animation

nathen nathen

2016/11/14

#
here is my code
public class bug3 extends Actor
{
    private GreenfootImage image1 = new GreenfootImage ("bug3.png"); 
    private GreenfootImage image2 = new GreenfootImage ("bug3flap.png");
           private int speed = 2;
    private int leftTurn = 100;
    private int rightTurn = 480;
   
    /**
     * Move in the direction we are currently moving in. Turn if we reach a turning point.
     */
    public void act()
    {
        
        
        setLocation ( getX() + speed, getY() );
        
        Actor actor = getOneIntersectingObject(null);
        if(actor != null) {
            actor.setLocation ( actor.getX() + speed, actor.getY() );
        }
        
        if (atTurningPoint()) {
            speed = -speed;
        }
        
        
        
       
        
        
        
        if (getImage().equals(image1)){
            setImage (image2);
        }
        else{
            
            
                setImage (image1);
        
       }
       
    }
    
    
        /**
        * Test if we are at one of the turning points.
        */
      
       public boolean atTurningPoint()
       {
        return (getX() <= leftTurn || getX() >= rightTurn);
       }
    
    }    

danpost danpost

2016/11/14

#
Add an int field to clock the display time of each image:
private int imageTimer;
Then, replace lines 33 through 43 with this:
imgTimer = ++imgTimer%10; // adjust '10' as needed
if (imgTimer == 0)
{
    if (getImage() == image2) setImage(image1); else setImage(image2);
}
nathen nathen

2016/11/14

#
thank you!
You need to login to post a reply.