This site requires JavaScript, please enable it in your browser!
Greenfoot back
thinud.bothma@reddam.house
thinud.bothma@reddam.house wrote ...

2025/6/30

Remove text actor from World

I have the following. If I move the mouse over the bs object the text does appear. But how do I remove the text object when the mouse pointer leaves the bs object.
        HotSpot bs;
        Text txt;
        public void act(){
            if(Greenfoot.mouseMoved(bs)){
                txt = new Text(bs.getDescription(),20,new Color(0,0,0),null);
                txt.setImage(txt.getText());
                addObject(txt,this.getWidth()/2,20);
            }else{
                this.removeObject(txt);
            }//end if else
        }//end act
I got something going; I managed to solve my problem. Maybe some suggestion to improve.
        if(Greenfoot.mouseMoved(bs)){
            if (!hovering) {
            hovering = true;
            txt = new Text(bs.getDescription(), 20, new Color(0,0,0), null);
            txt.setImage(txt.getText());
            addObject(txt, this.getWidth() / 2, 20);
            }else if (Greenfoot.mouseMoved(null) && hovering) {
            hovering = false;
            removeObject(txt);
            txt = null;
            }//end else if
        }//end if
danpost danpost

2025/6/30

#
thinud.bothma@reddam.house wrote...
I got something going; I managed to solve my problem. Maybe some suggestion to improve. << Code Omitted >>
Well, the boolean hovering field is not really needed. I believe you could simply change line 8 in your first code post to the following:
} else if (Greenfoot.mouseMoved(null)) {
Of course, I did not see that. There is a proverb: "When you're next to the tree you can't see the forest"
You need to login to post a reply.