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

2015/7/19

Regarding story telling in greenfoot

sujan_08 sujan_08

2015/7/19

#
Hi, Can we show in world communication b/w two Actors using message like story telling, i searched in greenfoot but i didnt get anything like that, could you let me know if such features is available in greenfoot ? Thanks Sujan
danpost danpost

2015/7/19

#
It is not a "feature" that would be available. It is something you would program or code your project to do -- and of course it is possible. Just create a class to run a dialog. Pass the specific dialog to the class when you create it and use the 'addedToWorld' method to control the dialog. You can use a 'delay' with 'getKey' to trap keypesses to get through the dialog and remove the actor when done. The following basic class code should be close to working. An array of GreenfootImage or an array of String objects can be passed to it as the dialog:
import greenfoot.*;

public class Dialog extends Actor
{
    Object[] frames;
    int index;

    public Dialog(Object[] array)
    {
        frames = array;
    }

    protected void addedToWorld(World world)
    {
        do
        {
            GreenfootImage image = null;
            if (frames[index] instanceof String) image = getFameImage();
            if (frames[index] instanceof GreenfootImage) image = (GreenfootImage)frames[index];
            setImage(image);
            while (Greenfoot.getKey() == null) Greenfoot.delay(1);
            index++;
        }
        while (index < frames.length);
        world.removeObject(this);
    }

    private GreenfootImage getFrameImage()
    {
        return new GreenfootImage((String)frames[index], 24, null, java.awt.Color.yellow);
    }        
}
danpost danpost

2015/7/19

#
I have found with the class above that the program will freeze if the 'Pause' button is clicked on during the dialog. To avoid that, the act method will need to be used and all action of all other actors must be put on hold while a Dialog object is in the world.
import greenfoot.*;

public class Dialog extends Actor
{
    Object[] frames;
    int index;
 
    public Dialog(Object[] array)
    {
        frames = array;
        setFrame();
    }
 
    public void act()
    {
        if (Greenfoot.getKey() != null)
        {
            index++;
            if (index == frames.length)
                getWorld().removeObject(this);
            else
                setFrame();
        }
    }
 
    private void setFrame()
    {
        GreenfootImage image = null;
        if (frames[index] instanceof String)
            image = new GreenfootImage((String)frames[index], 24, null, java.awt.Color.yellow);
        if (frames[index] instanceof GreenfootImage)
            image = (GreenfootImage)frames[index];
        setImage(image);
    }        
}
sujan_08 sujan_08

2015/7/22

#
Hi, Thanks for the reply, i did as per your suggestion, but when i am trying to create an object of Dialog class, its not allowing, its not getting highlighted " new Dialog (Object array" so i cant create the object. I am not able to paste the image .
danpost danpost

2015/7/22

#
sujan_08 wrote...
.I am not able to paste the image .
An image is not necessary. Just post the code of the class where you have 'new Dialogg'.
You need to login to post a reply.