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

2017/11/13

text box showing up when character presses enter

1
2
3
danpost danpost

2017/11/14

#
In my case, a TriggerText object would be like the speech bubble actor. Line 10 of my TriggerText class would be referring to the speech bubble image or for a transparent image if the speech bubble is already in the background image of the world. The TriggerText actor would be created as I had shown above; but, with the appropriate text image set to your TextBox (or whatever you call it) actor.
jacquelinereilly jacquelinereilly

2017/11/14

#
Ok. I think I have it. I forget what it means when the little greenfoot is on the screen - I have that right now.
jacquelinereilly jacquelinereilly

2017/11/14

#
public class TriggerText extends Actor { Actor toShow; public TriggerText (Actor actor) { toShow = actor; GreenfootImage Bg = new GreenfootImage ("MainMenu.jpg"); //image name and file type. the image must be in the project folder created when started scenario } /** * Act - do whatever the TriggerText wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // (toShow.getWorld () !=null ) return; if (! Greenfoot.isKeyDown ("enter") ) return; getWorld ().addObject (toShow, getX (), getY ()); } } this is the trigger text actor class
jacquelinereilly jacquelinereilly

2017/11/14

#
public class TextWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public TextWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1); 
        
         TextForTest textActor = new TextForTest ("Pokemon");
        TriggerText trigger = new TriggerText (textActor);
        addObject (trigger, 286, 80);
        
        // Resizing background picture to fit screen
        GreenfootImage Bg = new GreenfootImage ("pokemon.png"); //image name and file type. the image must be in the project folder created when started scenario
        Bg.scale (getWidth (), getHeight ()); // Can take anyway these 3 lines, if don't want any image/background
        setBackground (Bg);
        
        // Creating and adding the text (an object) to the world
        addObject (new TextForTest ("Pokemon"), 286, 80); //first number L/R: greater moves it to the right. Second Up or down: greater = down
        
        // Creating and adding the button
        PlayButton button = new PlayButton ();
        addObject (button, 500, 400);
        
    }
}
this is the Main world called TextWorld
jacquelinereilly jacquelinereilly

2017/11/14

#
public class TriggerText extends Actor
{
    Actor toShow;
    
    public TriggerText (Actor actor)
    {
        toShow = actor;
        GreenfootImage Bg = new GreenfootImage ("MainMenu.jpg"); //image name and file type. the image must be in the project folder created when started scenario
        
       
    }
        
    /**
     * Act - do whatever the TriggerText wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // (toShow.getWorld () !=null ) return;
        if (! Greenfoot.isKeyDown ("enter") ) return;
        getWorld ().addObject (toShow, getX (), getY ());
        
    }    
}
sorry this is the trigger text
danpost danpost

2017/11/14

#
Some modification to the TriggerText class will be required to allow the text to disappear when 'enter' is pressed again. A field is needed to track the state of the key:
import greenfoot.*;
 
public class TriggerText extends Actor
{
    Actor toShow;
    boolean keyDown;
     
    public TriggerText(Actor actor)
    {
        toShow = actor;
        // set image here or set a default image for the classs
    }
     
    public void act()
    {
        if ( toShow == null ) return; // no actor to show
        if ( ! isTouching(Player.class) ) return; // not touching player
        if (Greenfoot.isKeyDown("enter") != keyDown ) // change in state of 'enter' key
        {
            keyDown = ! keyDown; // new state of key
            if (keyDown) // was key pressed
            {
                if (intersects(toShow)) // is text displayed
                {
                    getWorld().removeObject(toShow); // remove text
                }
                else
                {
                    getWorld.addObject(toShow, getX(), getY()); // show text
                }
            }
        }
        if ( ! isTouching(Player.class) && intersects(toShow)) // did player move away w/o removing text
        {
            getWorld().removeObject(toShow); // remove text
        }
    }
}
danpost danpost

2017/11/14

#
jacquelinereilly wrote...
Ok. I think I have it. I forget what it means when the little greenfoot is on the screen - I have that right now.
The little greenfoot is the default image given to any actor whose class does not have a image set to it nor the actor had an image set to it using 'setImage'.
danpost danpost

2017/11/14

#
I still do not see a player being added into your world. Am I missing something? Remove lines 22 and 23 from the TextWorld class.
jacquelinereilly jacquelinereilly

2017/11/14

#
i do have a player. This is the actor class for it
public class Player extends Actor
{
    public Player ()
    {
        GreenfootImage image = getImage ();
        image.scale (image.getWidth (), image.getHeight ());
        setImage (image);
    }
jacquelinereilly jacquelinereilly

2017/11/14

#
I just added it to the main world! Forgot to show that part
danpost danpost

2017/11/14

#
You do not need to set an image to dimensions it already has. You can remove lines 3 through 8 of the Player class code given above (or 5 through 7, if you wish to explicitly show a constructor for the class).
jacquelinereilly jacquelinereilly

2017/11/14

#
Okay, thank you. I removed those lines and modified the TriggerText class. So once I add this to the other program that my group has, we are making it all one, they have the player moving around. So If i add this stuff to that, it should work? As in the text box will appear when pressing enter?
danpost danpost

2017/11/14

#
jacquelinereilly wrote...
If i add this stuff to that, it should work? As in the text box will appear when pressing enter?
And disappear when 'enter' is pressed again. It should -- provided you set everything up properly as far as the text box actor is concerned; and from what I can tell -- it is.
jacquelinereilly jacquelinereilly

2017/11/14

#
Thank you! I feel like I haven’t created a text box. When I run the program, nothing happens regarding text
jacquelinereilly jacquelinereilly

2017/11/14

#
I don't have any code that moves the player, the other team member does. Now I added the textBox image, so it appears on the screen, but I am still confused how what we just coded is going to trigger the text. It has been clarified that the character will see another player and then press enter for it to appear, and enter again to disappear. Here is updated TriggerText :
public class TriggerText extends Actor
{
    Actor toShow;
    boolean keyDown;
    
    public TriggerText (Actor actor)
    {
        toShow = actor;
        //Declaring image
        GreenfootImage image = new GreenfootImage ("textbox.jpg"); 
        image.scale (image.getWidth ()-110, image.getHeight () -68);
        setImage (image);
       
    }
        
    /**
     * Act - do whatever the TriggerText wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
        public void act()
    {
        if ( toShow == null ) return; // Showing no actor
        if ( ! isTouching(Player.class) ) return; // When not touching the player
        if (Greenfoot.isKeyDown("enter") != keyDown ) // Pressing the enter key
        {
            keyDown = ! keyDown; // State of key after
            //If the key was pressed, show the text, and remove it. 
            if (keyDown) 
            {
                if (intersects(toShow))
                {
                    getWorld().removeObject(toShow); 
                }
                else
                {
                    getWorld().addObject(toShow, getX(), getY()); 
                }
            }
        }
        // If the player moved without reading the text, remove it
        if ( ! isTouching(Player.class) && intersects(toShow)) 
        {
            getWorld().removeObject(toShow); 
        }
    }
}
There are more replies on the next page.
1
2
3