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

2015/5/4

How to delay and remove object few secs after the object appears?

ChelseaIsland ChelseaIsland

2015/5/4

#
Heyy, sorry for a noob problem. But i got some problem with removing an object. I want to create a notification error such as "WRONG!" to appears after clicking an actor and disappear soon after it. But, in my scenario whenever i click the actor the notif is working but it just wont to be disappear. Superclass:
    public void act() 
    {
       
    }
    public void wrong() 
    {
        Wrong1 Wrong1 = new Wrong1();
        getWorld().addObject(new Wrong1(),300,200);
        Greenfoot.delay(5);
        getWorld().removeObject(Wrong1);
    }
Subclass:
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            wrong();
        }
    }  
yedefei yedefei

2015/5/4

#
Since you used an nominated objects "new Wrong1()" when adding object ,the compiler couldn't find the reference of the object of Wrong1 ,so Line 10 “getWorld().removeObject(Wrong1);” can't work 。You may adjust the code like this:
getWorld().removeObjects(getWorld().getObjects(Wrong1.class));
or you could just change the code in Line 8 to:
getWorld().addObject( Wrong1,300,200);
One more thing , you‘d better not to define the same identifier of object as the class。
You need to login to post a reply.