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

2021/12/29

help idk what the mistake is

mariq_rasyid29 mariq_rasyid29

2021/12/29

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

/**
 * Write a description of class METAL_WOLF_ANTHRO here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class METAL_WOLF_ANTHRO extends NPC
{
    /**
     * Act - do whatever the MGX wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {((Dialogue)getWorld().getObjects(Dialogue.class).get(0)) this.class.setImage("Char/MGX/MGX 1.png");
     ((Dialogue)getWorld().getObjects(Dialogue.class).get(2)) this.class.setImage("Char/MGX/MGX 2.png");
     ((Dialogue)getWorld().getObjects(Dialogue.class).get(7)) this.class.setImage("Char/MGX/MGX 2.png");
}
   
  
}

the Dialogue class there in HowToplay World here the world code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HowToplay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HowToplay extends World
{public String[] dialogue5 = 
    {"Oh hi !",
"My name is MWA MK CCCVIX or you can call me Metal Wolf Anthro MK CCCVIX.",
"I will explain how the mechanism of this game The Wolf,",
"Press A for move left,",
"Press D for move right,",
"Press S for move backward,",
"Press W for move forward,",
"Let's help Blizzard to complete his mission!"
    };

    /**
     * Constructor for objects of class HowToplay.
     * 
     */
    public HowToplay()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1024, 768, 1); 
        prepare();
        METAL_WOLF_ANTHRO mETAL_WOLF_ANTHRO = new METAL_WOLF_ANTHRO();
        addObject(mETAL_WOLF_ANTHRO,204,419);
        adddialog();
    }
    
    public void adddialog()
    { Dialogue dialogue = new Dialogue(dialogue5);
        addObject(dialogue, 600,26);
    }
    
public void act()
    {if (!getObjects(Dialogue.class).isEmpty() && "z".equals(Greenfoot.getKey()))
        {
            ((Dialogue)getObjects(Dialogue.class).get(0)).nextText();
        }
    }
    
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        
    }
}
my question is how to change the picture in METAL_WOLF_ANTHRO.class if in dialogue length at 0,2, and 7?
danpost danpost

2021/12/29

#
mariq_rasyid29 wrote...
here the code << Code Omitted >>
"this.class" is a problem. Maybe you mean just "this".
the Dialogue class there in HowToplay World here the world code << Code Omitted >> my question is how to change the picture in METAL_WOLF_ANTHRO.class if in dialogue length at 0,2, and 7?
Please provide Dialogue class codes.
mariq_rasyid29 mariq_rasyid29

2021/12/30

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

/**
 * Write a description of class Dialogue here.
 * 
 * @author (Muhamad Ariq Rasyid) 
 * @version (1.0 Feb 19 2021)
 */
public class Dialogue extends Text
{private String[] dialogue;
private int index;
 
public Dialogue(String[] dialogue)
{
    this.dialogue = dialogue;
    index = 0;
    setText();
}
 
private void setText()
{
    setImage(new GreenfootImage(dialogue[index], 28, Color.BLACK, Color.WHITE));
}
 
public void nextText()
{
    if (++index < dialogue.length) setText();
    else
    if(Greenfoot.isKeyDown("z"))getWorld().removeObject(this);
}
  

}
danpost danpost

2021/12/30

#
Both the dialogue and the index are private in the Dialogue class and there are no public "getter" methods to gain access, so no information about the dialogue is accessible from outside the Dialogue class. However, best is to have the HowToPlay world control what goes on in it. The world is the link between all actors in it. So, when the "z" key is detected, it can tell the dialogue to go to next line AND, if required, have the image of the other actor change. The world does not need to keep a reference to the dialogue itself (the array). What the world needs references to are the Dialogue object and the METAL_WOLF_ANTHRO object. It would be as such: The world class:
import greenfoot.*;

public class HowToPlay extends World
{
    Dialogue dialogue;
    METAL_WOLF_ANTHRO wolf;
    
    public HowToPlay()
    {
        super(1024, 768, 1);
        String[] dialog =
        {
            "Oh hi !",
            "My name is MWA MK CCCVIX or you can call me Metal Wolf Anthro MK CCCVIX.",
            "I will explain how the mechanism of this game The Wolf,",
            "Press A for move left,",
            "Press D for move right,",
            "Press S for move backward,",
            "Press W for move forward,",
            "Let's help Blizzard to complete his mission!"
        };
        dialogue = new Dialogue(dialog);
        addObject(dialogue, 600, 26);
        wolf = new METAL_WOLF_ANTHRO();
        addObject(wolf, 204, 419);
        wolf.setImage("Char/MGX/MGX 1.png");
    }
    
    public void act()
    {
        if (dialogue.getWorld() == this && "z".equals(Greenfoot.getKey()))
        {
            switch (dialogue.next())
            {
                case 2:  wolf.setImage("Char/MGX/MGX 2.png"); break;
                case 7:  wolf.setImage("Char/MGX/MGX 2.png"); break;
                case 8:  removeObject(dialogue); break;
            }
        }
    }
}
The dialogue class:
import greenfoot.*;

public class Dialogue extends Text
{
    String[] dialog;
    int index;
    
    public Dialogue(String[] lines)
    {
        dialog = lines;
        setText();
    }
    
    private void setText()
    {
        setImage(new GreenfootImage(dialog[index], 28, Color.BLACK, Color.WHITE));
    }
    
    public int next()
    {
        if (++index < dialog.length) setText();
        return index;
    }
}
The wolf class:
public class METAL_WOLF_ANTHRO extends NPC {}
Yes -- that one line is all that is required in the class. Not sure why, but you have the same image being set to the wolf when the index is both 2 and 7 for the dialogue. Maybe there is a 3rd image that is not currently used here. Also not sure if it was intended, but "CCCVIX" is not a valid Roman numeral.
mariq_rasyid29 mariq_rasyid29

2021/12/30

#
ok thx sir for help
You need to login to post a reply.