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

2013/11/23

Help with Container

JasonZhu JasonZhu

2013/11/23

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

/**
 * Write a description of class Container here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Container extends Actor
{
    private int counter;

    /**
     * Act - do whatever the Container wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public Container()
    {
        GreenfootImage img = new GreenfootImage(90,195);
        img.drawRect(0,0,89,199);
        setImage(img);
        counter = 0;     
    }

    public void updateImage()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLACK);        
        img.drawRect(0,0,89,194);
        img.setColor(Color.BLUE);
        img.fillRect(1,195-counter,88,counter);
    }

    public void act() 
    {
        Actor drop = getOneIntersectingObject(Drop.class);
        if(drop!=null){
            counter++;
            getWorld().removeObject(drop);
            updateImage();
        }
    }
}
Is there a way to make it so that my container is just a simple line and have blue stack buy 1 pixel height on top of the line as a my drop touches that line?
bourne bourne

2013/11/23

#
You could do something like this that I believe achieves what you want:
public void act()
{
    Actor drop = getOneIntersectingObject(Drop.class);
    if (drop != null)
    {
        // Check if bottom of the drop reaches “top of container’s contents”
        if (drop.getY() + drop.getImage().getHeight() / 2 > getY() + getImage().getHeight() / 2 - counter)
        {
            counter++;
            getWorld().removeObject(drop);
            updateImage();
        }
    }
}
JasonZhu JasonZhu

2013/11/23

#
That is exactly what i wanted, thanks alot
You need to login to post a reply.