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

2013/9/19

Adding and Removing text object from my World

yadavabhishek yadavabhishek

2013/9/19

#
Hi following is my code, now I want to remove the object from the world which has a text message in it.Similarly I want to even add some more text in the world after some work is performed.Can you help me in this.
public void act() 
    {
        int mouseX, mouseY ;
        String text ="Have Coin!";
        String text1= "Have penny";
               
        if(Greenfoot.mouseDragged(this)) 
        {          
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            mouseX=mouse.getX();  
            mouseY=mouse.getY();  
            setLocation(mouseX, mouseY);
        if((mouseX>300 && mouseX <400) && (mouseY>200 && mouseY<250))
        {   
            World w = getWorld();
            w.removeObject(this);
            //getMessage();
            //Message message= new Message(text);
            
            w.addObject(new Message(text),380,450);
            //getWorld().addObject(message(text),380,450);
            update();
            Greenfoot.playSound("coin-drop.wav");
            Greenfoot.delay(10);
            
          
          }
----------------------------------------------------------------------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class Message extends Actor
{


public Message(String text)
{
    GreenfootImage gi= new greenfoot.GreenfootImage(100,30);
    gi.drawString(text,2,20);
    setImage(gi);
}
}
Now I am finding it difficult to add and remove a text message at runtime.... Thanks
danpost danpost

2013/9/19

#
Change line 20 to:
Message msg = new Message(text);
w.addObject(msg, 380, 450);
Then you can remove it with
w.removeObject(msg);
You may have to increase the delay a bit
yadavabhishek yadavabhishek

2013/9/20

#
I tried that, it says "cannot find symbo- variablel msg"
It means you don't have a variable named "msg". Make sure you wrote "Message msg = new Message(text);"
danpost danpost

2013/9/20

#
Please show what you tried.
yadavabhishek yadavabhishek

2013/9/20

#
yeah it worked... thank you... Wow! and you have an online admirer.... Talk to her/him ! :P
danpost danpost

2013/9/20

#
@yadavabhishek, he/she is twice my age and I am, btw, married.
danpost danpost

2013/9/20

#
I did not go to college.
You need to login to post a reply.