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
jacquelinereilly jacquelinereilly

2017/11/13

#
I am trying to have the player walk up to a certain point, press enter, and a text box will appear. I know how to do the text class and how to display the text
danpost danpost

2017/11/13

#
jacquelinereilly wrote...
I am trying to have the player walk up to a certain point, press enter, and a text box will appear. I know how to do the text class and how to display the text
Try creating an Actor subclass for an object that will trigger the Text object. Show attempt and ask questions if struggling.
jacquelinereilly jacquelinereilly

2017/11/13

#
Do I make the actor subclass for the text object?
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); 
        
        // 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);
        
    }
}
jacquelinereilly jacquelinereilly

2017/11/13

#
That is my world for the text, I have an actor too called TextForTest which is where I am doing the text settings
danpost danpost

2017/11/13

#
I am a bit confused. I thought you wanted the player to go to a certain point before the text was displayed. It appears you have the text already displayed when the world is created. Maybe you can explain in detail, step by step, using references to the specific objects you are using, what is supposed to happen (as best as possible -- knowing that you are having a bit of an issue with it). Actually, I do not even see a player being added to the world (looks like your title screen with a start button which has nothing to do with what you were asking help on).
jacquelinereilly jacquelinereilly

2017/11/13

#
I am working in a group which is why it is a bit fuzzy - Sorry!! So I made the text class so that I could show my group members how to do it. My next step is to have a player (Which the other member has created ) so I am adding onto another program. -- is to have the player approach the text box and press enter for it to appear
jacquelinereilly jacquelinereilly

2017/11/13

#
So I have a player I just am in the midst of going through the other program. I have to add onto it
danpost danpost

2017/11/13

#
Okay. I would not yet add the text actor into the world. However, I would create it and give it to another Actor object (whose class you will need to create). This object should detect the player and add the text into the world at the appropriate time (when touches player). So, in world, you might have this:
TextForTest textActor = new TextForTest ("Pokemon");
TriggerText trigger = new TriggerText(textActor);
addObject(trigger, 286, 80);
The TriggerText class would be something like this:
import greenfoot.*;

public class TriggerText extends Actor
{
    Actor toShowl;
    
    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 ( toShow.getWorld() != null ) return; // actor already in world
        if ( ! isTouching(Player.class) ) return; // not touching player
        if ( ! Greenfoot.isKeyDown("enter") ) return; // 'enter' key not pressed
        getWorld().addObject(toShow, getX(), getY());
        getWorld().removeObject(this); // optional
    }
}
jacquelinereilly jacquelinereilly

2017/11/13

#
Thank you so much! I am going to try it now.
danpost danpost

2017/11/13

#
Oops, I have an extra (unwanted) ell ( 'l' ) near the end of line 5.
jacquelinereilly jacquelinereilly

2017/11/14

#
Yes I caught that, thank you
jacquelinereilly jacquelinereilly

2017/11/14

#
public class TriggerText extends Actor
{
    Actor toShow;
    
    public TriggerText (Actor actor)
    {
        toShow = actor;
        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);
    }
        
    /**
     * 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 ());
        
    }    
}
jacquelinereilly jacquelinereilly

2017/11/14

#
This is the trigger text, not sure why there is an error on lines 9 and 10. it says cannot find symbol
danpost danpost

2017/11/14

#
Let me get things straight, here. Apparently the whole background is to be occupied by this triggered text. But: 1) What exactly is the player to touch? that is, what is visible that the player is to move to before the text appears? and, is it part of the background image or an image set to an actor? 2) Is the text that is to appears permanent? or is it to change or be removed at some point? 3) If not permanent, then what about the thing the player touched to begin with? is it to be removed when the text is triggered? Once these are answered, I should be able to provide some helpful tips.
jacquelinereilly jacquelinereilly

2017/11/14

#
OH! That makes sense - so I can remove the lines about the background. okay, so The player is to touch a speech bubble or another character, press enter, and then the text box will appear. The player then presses enter again and the text box will go away. So the player will see the box, or see another character who is going to say something, press enter, text box with text in it appears, user reads it, and then presses enter for the text box to go away, and the player continues. Sorry for all the confusion
There are more replies on the next page.
1
2
3