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

2017/2/24

Help with having 2 Gif's in code

SachaTb SachaTb

2017/2/24

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

/**
 * Write a description of class Shroom here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shroom extends Actor
{
    GifImage gifImage = new GifImage("Shroom-Cropped.gif");
    /**
     * Act - do whatever the Shroom wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setImage(gifImage.getCurrentImage()); 
       int speed = 4;
      
      if(Greenfoot.isKeyDown("left"))
      setLocation(getX() - speed, getY());
      setImage(
      
      if(Greenfoot.isKeyDown("right"));
      setLocation(getX() + speed, getY());
    }
}
i want to put a gif change here
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shroom here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shroom extends Actor
{
    GifImage gifImage = new GifImage("Shroom-Cropped.gif");
    /**
     * Act - do whatever the Shroom wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setImage(gifImage.getCurrentImage()); 
       int speed = 4;
      
      if(Greenfoot.isKeyDown("left"))
      setLocation(getX() - speed, getY());
      "Put a gif Here on keypress"
      
      if(Greenfoot.isKeyDown("right"));
      setLocation(getX() + speed, getY());
    }
}
Please Help!
danpost danpost

2017/2/24

#
Change line 11 to:
GifImage gifRight = new GifImage("Shroom-Cropped.gif");
Insert, immediately after that line, the following two lines:
GifImage gifLeft = new GifImage(<< filename of other gif >>);
GifImage gifImage = gifRight;
Then, place a set of brackets around line 22 through 23 and another set around line 26. Replace line 23 (and insert after 26 inside brackets a similar line for left): with:
gifImage = gifRight;
SachaTb SachaTb

2017/2/27

#
Ok thx dan!
SachaTb SachaTb

2017/2/27

#
@danpost and what if i have about 5 images and i want it to play on a keypress?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Shroom here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shroom extends Actor
{
    /**
     * Act - do whatever the Shroom wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setImage(gifImage.getCurrentImage()); 
       int speed = 4;
       
      if(Greenfoot.isKeyDown("left"))
      setLocation(getX() - speed, getY());
      "Play 5 images in a order"
       
      if(Greenfoot.isKeyDown("right"));
      setLocation(getX() + speed, getY());
    }
}
SachaTb SachaTb

2017/2/27

#
danpost wrote...
"Then, place a set of brackets around line 22 through 23 and another set around line 26. Replace line 23 (and insert after 26 inside brackets a similar line for left): with:"
What do you mean by this?
danpost danpost

2017/2/27

#
SachaTb wrote...
@danpost and what if i have about 5 images and i want it to play on a keypress?
Are you implying that the 5 images are not in 'gif' form and you cannot just change the image set like above?
danpost danpost

2017/2/27

#
SachaTb wrote...
danpost wrote...
"Then, place a set of brackets around line 22 through 23 and another set around line 26. Replace line 23 (and insert after 26 inside brackets a similar line for left): with:"
What do you mean by this?
I mean this:
import greenfoot.*;

public class Shroom extends Actor
{
    private static final GifImage gifRight = new GifImage("Shroom-Cropped.gif");
    private static final GifImage gifLeft = new GifImae("Shroom-Cropped-Left.gif");
    private GifImage gifImage;
    private int speed = 4;
    
    public void act()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - speed, getY());
            gifImage = gifLeft;
        }        
        if(Greenfoot.isKeyDown("right"));
        {
            setLocation(getX() + speed, getY());
            gifImage = gifRight;
        }
        setImage(gifImage.getCurrentImage());       
    }
}
SachaTb SachaTb

2017/2/27

#
It is always moving right now
danpost danpost

2017/2/27

#
Then, try this:
public void act()
{
    setImage("Shroom-Still.png"); // image when not moving
    if(Greenfoot.isKeyDown("left"))
    {
        setLocation(getX() - speed, getY());
        gifImage = gifLeft;
        setImage(gifImage.getCurrentImage());
    }
    if(Greenfoot.isKeyDown("right"))
    {
        setLocation(getX() + speed, getY());
        gifImage = gifRight;
        setImage(gifImage.getCurrentImage());
    }
}
If you have a 'gif' image set for when not moving, add another 'private static final GifImage' field for it and set its current image at line 3.
SachaTb SachaTb

2017/2/27

#
Ok i got it now thx!
You need to login to post a reply.