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

2021/2/1

I need to display Hint type text

XApple15 XApple15

2021/2/1

#
I have to create Hint type text, along with background for it. There will be almost the same text for both players, but it will appear when needed and dissapear when task is done, but i have tried something which is not working. Can someone help me?
danpost danpost

2021/2/1

#
XApple15 wrote...
I have to create Hint type text, along with background for it. There will be almost the same text for both players, but it will appear when needed and dissapear when task is done, but i have tried something which is not working. Can someone help me?
You will need to show what you tried and explain (a) how the resulting behavior is not what you want; and (b) what you actually want.
XApple15 XApple15

2021/2/1

#
There`s the Actor which triggers the Hints to appear or dissapear
private void DisplayText()   
    {
        getWorld().addObject(new HoldCtoCut(100,100, Player), 100,100);
        // getWorld().addObject(new GoToVent(), 100,100);
        getWorld().addObject(new AfterCuttingPressE(100,100),100,100);
    }
and here is the actor where the string will be generated along with the box which will be placed first
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HoldCtoCut here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HoldCtoCut extends TextBox
{
    String text = " Hold E to start cutting the vents ";
    
    public void HoldCtoCut( int x,int y, int Player)
    {
        if( Player == 1 ) 
        {
         getWorld().addObject(new Box( x, y), 100,500);
        }
        else 
        {
            getWorld().addObject(new Box( x, y), 800,500);
        }
        GreenfootImage HoldCtoCut = new GreenfootImage(100,30);
        HoldCtoCut.drawString(text, 2, 20);
        setImage(HoldCtoCut);
    }
    public void act() 
    {
        // Add your action code here.
    }    
}

here is the box actor
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Jobs
{
    public void Box( int x,int y)
    {
        GreenfootImage image = new  GreenfootImage(x, y);
        image.setColor(Color.RED);
        image.fill();
        setImage(image);
    }
    public void act() 
    {
        // Add your action code here.
    }    
}
What I am trying to do is on the first code mentioned i will pass the location in first 2 parameters for the Box ( which is the background for the text ) along with the Player parameter which is 1 or 2 ( 1 for player 1 and 2 for player 2 ; with this i know on which side of the screen to display those boxes) In HoldCtoCut i create the Box with the location gathered from the first code and after that the string =. For passing the parameters on new Box( x,y) and for new HoldCtoCut( 100,100,Player) i get an compilation error that i cannot do this type of call. Is there any other solution to fix this?
danpost danpost

2021/2/1

#
XApple15 wrote...
for new HoldCtoCut( 100,100,Player) i get an compilation error that i cannot do this type of call. Is there any other solution to fix this?
In HoldCtoCut class, change line 13 to:
public HoldCtoCut()
and remove lines 15 thru 22. Use the following for DisplayText method:
public void DisplayText()
{
    addObject(new Box(100, 100), Player*700-600, 100);
    addObject(new HoldCtoCut(), 100, 100);
    addObject(new AfterCuttingPressE(100, 100), 100, 100);
}
(although, I do not know why the last two actors are placed at the same location)
XApple15 XApple15

2021/2/1

#
In HoldCtoCut class, change line 13 to: ? 1 public HoldCtoCut() and remove lines 15 thru 22.
And what should i replace lines 15 to 22 with ?
XApple15 XApple15

2021/2/1

#
So, i want to display some text with a box behind it at some coordinates , those will be the hints. The problem is that i could create a class for every Hint, but i need this code to be efficient. Any ideas?
danpost danpost

2021/2/1

#
XApple15 wrote...
what should i replace lines 15 to 22 with ?
Those lines only added the Box object, which now I have being added in the DisplayText method.
So, i want to display some text with a box behind it at some coordinates , those will be the hints. The problem is that i could create a class for every Hint, but i need this code to be efficient. Any ideas?
Create a Hint class. Add a parameter of type String to a constructor for its text. In the constructor, create two images, the text image and the box image. Draw the text image onto the box image and set box image to actor.
XApple15 XApple15

2021/2/2

#
danpost wrote...
XApple15 wrote...
what should i replace lines 15 to 22 with ?
Those lines only added the Box object, which now I have being added in the DisplayText method.
So, i want to display some text with a box behind it at some coordinates , those will be the hints. The problem is that i could create a class for every Hint, but i need this code to be efficient. Any ideas?
Create a Hint class. Add a parameter of type String to a constructor for its text. In the constructor, create two images, the text image and the box image. Draw the text image onto the box image and set box image to actor.
public void HoldCtoCut()
{
    Background();
    Text();
}
private void Text()
{
     // here i put the text
}
private void Background()
{
   // here i put the background for text
}
Something lile this?
danpost danpost

2021/2/2

#
XApple15 wrote...
Something lile this?
Nowhere close. More like:
import greenfoot.*;

public class Hint extends Actor
{
    public Hint(String text)
    {
        setText(text);
    }
    
    public void setText(String text)
    {
        GreenfootImage textImg = new GreenfootImage(text, 24, Color.BLACK, new Color(0, 0, 0, 0));
        GreenfootImage img = new GreenfootImage(100, 100);
        img.setColor(Color.RED);
        img.fill();
        img.drawImage(textImg, 50-textImg.getWidth()/2, 50-textImg.getHeight()/2);
        setImage(img);
    }
}
With that, in world, you can have a field for the Hint object:
private Hint hint = new Hint{" ");
and set it up like this:
String text = "Hold E to\nstart cutting\nthe vents";
hint.setText(text);
addObject(hint, Player*700-600, 100);
XApple15 XApple15

2021/2/2

#
That worked, THANK YOU ! I have one more question : If i have a public boolean IsDeleted = true; in an actor , can i check the boolean IsDeleted in any other actor?
danpost danpost

2021/2/2

#
XApple15 wrote...
That worked, THANK YOU ! I have one more question : If i have a public boolean IsDeleted = true; in an actor , can i check the boolean IsDeleted in any other actor?
If the actor is removed from the world and there are no references to it or access to the boolean field and its value are denied, then no. In other words, if you have, or can get, a reference to the actor and if the boolean field or its value is accessible, then yes. You did say "public", so it should be at least accessible (you just need a reference to the actor).
XApple15 XApple15

2021/2/2

#
If the actor where the boolean IsDeleted is named Cut , so the reference to the boolean whould be : Cut IsDeleted ? ( ex : if ( Cut IsDeleted == true ) { ....} ) ?
danpost danpost

2021/2/3

#
XApple15 wrote...
If the actor where the boolean IsDeleted is named Cut , so the reference to the boolean whould be : Cut IsDeleted ? ( ex : if ( Cut IsDeleted == true ) { ....} ) ?
Well, if Cut is the name of the class that creates the actor with the boolean, then Cut is not necessarily, and probably should not be the name of the reference (to avoid confusion). Example:
Cut cutActor = new Cut();
With this line, cutActor is the name of the reference to the actor.
Cut cut = (Cut) getWorld().getObjects(Cut.class).get(0);
Here, presuming a Cut actor is in the world, cut becomes a reference to a Cut actor (not necessarily the Cut actor you might want, however, if more than one Cut actor is in the world).
You need to login to post a reply.