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

2017/10/11

Trying to reset actor back to original after a certain point.

Drew-Hughes Drew-Hughes

2017/10/11

#
I am creating a game where a pig has to catch and "eat" mushrooms as they fall down the screen. There is also an option for the pig to grab a barrel which "collects" the mushrooms and the image grows bigger as the barrel collects more and more. If the pig right clicks on the barrel, the pig will "eat" all that is in the barrel, and the barrel will go back to it's original size and location. I'm having trouble with getting the barrel's image scale back to it's original scale. Here is my code for the world, the barrel, and the pig.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PigWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PigWorld extends World
{

    /**
     * Builds a PigWorld object.
     * 
     */
    public PigWorld()
    {    
        super(600, 400, 1, false); 
        setPaintOrder(Barrel.class, Pig.class, Mushroom.class);
        addObject(new Pig(), 400, 300);
        addObject(new Barrel(), 565, 350);
        
    }
    
    /**
     *  Adds mushrooms at the top of the screen at given intervals.
     */
    public void act() {
        if (Greenfoot.getRandomNumber(100) < 3) {
            addObject(new Mushroom(), Greenfoot.getRandomNumber(550), 0);
        }
        
    }
}
Above is the world "PigWorld". Below is the actor "Barrel".
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Barrel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Barrel extends Actor
{
    private int stored = 0;
    private final double SCALE_FACTOR = 1.05;
    private int size;
    
    public Barrel() {
        setImage(new GreenfootImage("barrel.png"));
        size = 1;
        
    }
    /**
     * Automatically called by Greenfoot whenever a Barrel object
     * is placed in a world. In this assigment, we use it to remember
     * the initial state of this barrel - its (x,y) position and its
     * original image size.
     */
    public void addedToWorld(World world) {
        setLocation(565, 350);
        GreenfootImage img = getImage();
        int width = (int)(img.getWidth());
        int height = (int)(img.getHeight());
        img.scale(width, height);
        
    }
   
    /**
     * Returns how many mushrooms this barrel has stored.
     */
    public int getMushrooms() {
        return stored;
    }
    
    /**
     * Follows mouse drag-and-drop motion. Eats nearby mushrooms when
     * dropped and increases its current image scale by 25%. If this barrel
     * stores more than 10 mushrooms, this barrel has itself removed from
     * this world.
     */
    public void act() {
        
        if(Greenfoot.mouseDragged(this)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
        if(Greenfoot.mouseDragEnded(this)) {
            List<Mushroom> nearby = getObjectsInRange(75,
                                                Mushroom.class);
            for (Mushroom m : nearby) {
                getWorld().removeObject(m);
                ++stored;
                if (stored == 1) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 2) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 3) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 4) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 5) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 6) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 7) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 8) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 9) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored == 10) {
                    GreenfootImage img = getImage();
                    int width = (int)(img.getWidth() * SCALE_FACTOR);
                    int height = (int)(img.getHeight() * SCALE_FACTOR);
                    img.scale(width, height);
                    size = size + 1;
                }
                if (stored > 10) {
                    getWorld().removeObject(this);
                }
            }
            
        }
    }   
    
    /**
     * Returns this barrel to its original (x,y) location and its
     * original image scale.
     */
    public void reset() {
        setLocation(565, 350);
        stored = 0;
        
        

    }
}
Above is the actor "Barrel". Bellow is the actor "Pig".
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pig here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pig extends Actor
{
    /** Keeps track of how many mushrooms this pig has eaten. */
    private int shrooms;
    
    /** 
     * Constructs a Pig object and initializes it as having
     * eaten no mushrooms. 
     */
    public Pig() {
        shrooms = 0;
    }
    
    /**
     * Follows the mouse movement and eats mushrooms on mouse clicks.
     * Stops the scenario once this pig has eaten at least 15 mushrooms.
     */
    public void act() 
    {
       if (Greenfoot.mouseMoved(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
       if (Greenfoot.mouseClicked(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo(); 
            Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class);
            Barrel b = (Barrel) getOneIntersectingObject(Barrel.class);
            
           if (m != null) {
                shrooms++;
                getWorld().removeObject(m);
           }
           if (mouse.getButton() == 3 && b != null) {
                
                int bHas = b.getMushrooms();
                shrooms += bHas;
                b.reset();
                
           }
       }

       if (shrooms >= 30) {
            Greenfoot.stop();
        }
    }    
}
Thank you for any advice you can give me.
danpost danpost

2017/10/12

#
Best would be to retain the original image in a field and make a copy of it to scale when needed. It might make it easier if you also used the stored value as a factor in the scaling:
private GreenfootImage image;
int stored;

public Barrel()
{
    image = new GreenfootImage("barrel.png"};
    setImage(image);
}

private void updateImage()
{
    GreenfootImage img = new GreenfootImage(image);
    img.scale(img.getWidth()+img.getWidth()*stored/20, img.getHeight()+img.getHeight()*stored/20);
    setimage(img);
}
You should be able to replace lines 12 through 34 with this. Along with that the following line of code:
updateImage();
can replace lines 61 through 130 and can also be coded in at line 146.
You need to login to post a reply.