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

2018/4/18

Text

1
2
student1 student1

2018/4/18

#
i'm making a game and i'm curious to know if i can make it so that when the player is touching an actor it will display text on the screen and if it is possible how would i do this.
danpost danpost

2018/4/18

#
student1 wrote...
i'm making a game and i'm curious to know if i can make it so that when the player is touching an actor it will display text on the screen and if it is possible how would i do this.
You would use the isTouching or intersects method to determine contact in an if condition and add the text into the world when found true. You could create the text object on the fly or have it already created and given a reference name (use a field to retain it). You will probably need to retain it at least while it is in the world so that you can remove it if the two conditions, it is in world and player is not touching actor, are true, remove it.
student1 student1

2018/4/18

#
how would i code that i've got code for the door and text, they are separate actors though
// the door actor
public void touching()
    {
        if(isTouching(Player.class))
        {
            setImage("door-open.png");
            if(Greenfoot.isKeyDown("enter"))
            {
                Greenfoot.setWorld(new Level1());
            }
        }else
        {
            setImage("door.png");
        }
    }

// the text actor
public void Text()
    {
        GreenfootImage resetText = new GreenfootImage("Level 1", 48, Color.RED, new Color(0, 0, 0, 0));
        setImage(resetText);
    } 
danpost danpost

2018/4/18

#
You probably should not have "void" in line 18.
student1 student1

2018/4/18

#
i'v got the method text being called in the act method so it works fine but i don't know what the code would look like to make it appear and disappear when the player is either touching the door or not
danpost danpost

2018/4/18

#
student1 wrote...
i'v got the method text being called in the act method so it works fine but i don't know what the code would look like to make it appear and disappear when the player is either touching the door or not
Have the Door object create the Text object and add it and remove it when it changes its image, since they happen at the same times.
student1 student1

2018/4/19

#
i don't know if i was clear enough so i'm going to explain what it is that i want exactly. i'm wanting it so that when the player touches the door the door will change it's image to show that it is open (i have this part) and (need help with the following) it will also add text (level 1) to the world (just above the centre) and then when the player is not touching the door it will be removed (i.e. the text is only visible when touching the door class) my world is 850 by 590
danpost danpost

2018/4/19

#
student1 wrote...
i don't know if i was clear enough so i'm going to explain what it is that i want exactly. << Clarification Omitted >>
You were clear enough before. See what you can do. HINT: It would only take 3 lines of code in your Door class -- one to create the Text object and retain it in an Object reference field; one to add it into the world; and one to remove it from the world.
student1 student1

2018/4/19

#
so it should not be this
if(isTouching(Player.class))
        {
            GreenfootImage resetText = new GreenfootImage("Level 1", 48, Color.RED, new Color(0, 0, 0, 0));
            setImage(resetText);
        }
danpost danpost

2018/4/19

#
student1 wrote...
so it should not be this << Code Omitted >>
That is neither creating, adding or removing a Text object. In fact, you would ruin your Door image with that code. Answer this question: What code do you use to create an object (say a Text object), in general?
student1 student1

2018/4/19

#
isn't it add object
danpost danpost

2018/4/19

#
student1 wrote...
isn't it add object
The addObject method of the World class will be used to add an already created object into the world. What code would you use to actually create the object before it can be added into a world? As an example, show code that creates an instance of the Text class.
student1 student1

2018/4/19

#
i was thinking while coming up to college if this would work but unfortunately i can't check just now
if(isTouching(Player.class))
{
Greenfoot.drawImage L1("Level 1", color.RED);
getWorld().addObject(L1,450,450);
}else
{
Greenfoot.removeObject(L1);
}
danpost danpost

2018/4/19

#
student1 wrote...
i was thinking while coming up to college if this would work but unfortunately i can't check just now << Code Omitted >>
Line 3 is nonsense. I am not asking about any image right now; nor am I interested in the whole block of code. I am not even asking for a complete line of code (although, I guess it could be, depending on the type of object being created. All I am asking for is what code you would use to create a simple Text object. Alright, let us say you had a class as follows:
import greenfoot.*;

public class DummyClass extends Actor
{
    
}
Give me what code you would use to create an object of type DummyClass (or, a DummyClass object).
student1 student1

2018/4/19

#
i thought it could be this since it is creating an object image called L1 but i am probably wrong
GreenfootImage L1 = new GreenfootImage("Level 1", 48, Color.RED, new Color(0, 0, 0, 0));
There are more replies on the next page.
1
2