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

2020/11/4

hint button

superman2 superman2

2020/11/4

#
Hi everyone! I want to make a button in my game where when you press it, it shows you a hint for that particular room for x seconds. Does anyone have any idea how you program this?
danpost danpost

2020/11/4

#
superman2 wrote...
I want to make a button in my game where when you press it, it shows you a hint for that particular room for x seconds. Does anyone have any idea how you program this?
What do you have for a Hint class?
superman2 superman2

2020/11/4

#
mport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Message here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Message extends Actor
{
 /**
     * Act - do whatever the Message wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {   
       
  if(Greenfoot.mouseClicked(this))
    {
   
    setImage(new GreenfootImage("hint 1", 30, Color.WHITE, null));
   if(Greenfoot.mouseClicked(getWorld().getObjects(Message.class))){
       getWorld().removeObjects(getWorld().getObjects(Message.class));
    }
  
}
}
}
superman2 superman2

2020/11/4

#
So when i click the hint image it shows me "hint 1" but when i try clicking on hint 1 it doesn't disappear anymore.
danpost danpost

2020/11/4

#
superman2 wrote...
So when i click the hint image it shows me "hint 1" but when i try clicking on hint 1 it doesn't disappear anymore.
You have a List object as an argument for mouseClicked on line 22. You cannot click on a list.
superman2 superman2

2020/11/5

#
So how do i fix this?
danpost danpost

2020/11/6

#
superman2 wrote...
So how do i fix this?
The keyword 'this' in the Message class act method will refer to an instance of the Message class:
if (Greenfoot.mouseClicked(this))
superman2 superman2

2020/11/25

#
but like, how do i remove the list then? do I use a timer instead or something?
danpost danpost

2020/11/25

#
superman2 wrote...
but like, how do i remove the list then? do I use a timer instead or something?
I am not sure what you want a Message object to do. When is one added to the world to begin with? What does it initially show? It appears once it is in the world that an initial mouse click will have it show a hint and a secondary click will have it removed from the world. Is this correct? As far as removing a list, it appears only one will be in the world -- and removing "this" will be equivalent to removing all Message objects.
danpost danpost

2020/11/25

#
Oh, I think I see -- "x seconds". If you do not mind the scenario to temporarily freeze during the hint display, you can use the Greenfoot class delay method. Otherwise, an int timer field would be sufficient.
You need to login to post a reply.