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

2020/11/26

Wondering how to remove objects from other classes

Duck12 Duck12

2020/11/26

#
here is my code:
public void remove()
    {
        Actor cobble = getOneIntersectingObject(Cobble.class);
        if(getOneIntersectingObject(PressurePlate.class) !=null)
        {
            getWorld().removeObject(cobble);
        }
    }
here is my problem: I want the player to remove Cobble if the player is intersecting PressurePlate.class but I couldn't figure out why it doesn't work. if I put cobble into the getWorld().removeObject(cobble); it doesn't do anything but if I put getWorld().removeObject(this); it is removing the player which isn't obviously what in want. I'd appreciate any help! Thanks!
danpost danpost

2020/11/26

#
Remove line 3. You actor is touching pressure plate -- not cobble. Use the following for line 6:
getWorld().removeObjects(getWorld().getObjects(Cobble.class));
Duck12 Duck12

2020/11/26

#
Thank you so much danpost! I was trying to figure it out but yeah I'm quite new. Anyways thanks!
Duck12 Duck12

2020/11/26

#
Just one more question: how can I make it add back to the position in a certain time? I tried this but it didn't work. Sorry if I ask too much.
//ignore where the int is
int timer = 300;

public void remove()
    {
        if(getOneIntersectingObject(PressurePlate.class) !=null)
        {
            timer--;
            getWorld().removeObjects(getWorld().getObjects(Cobble.class));
        }
        if(timer == 0)
        {
            timer = 300;
            getWorld().addObject(new Cobble(),984-210, 45);
            getWorld().addObject(new Cobble(),984-210, 45+30);
            getWorld().addObject(new Cobble(),984-210, 45+60);
        }
    }
danpost danpost

2020/11/26

#
Make line 2 be:
int timer;
Change line 8 to:
timer = 300;
Then remove line 13 and change line 11 to:
if (timer > 0 && --timer == 0)
Duck12 Duck12

2020/11/26

#
sadly not working :( if the player remains on the PressurePlate nothig happens. Cobble dissappears and wont be added back. If player leaves the PressurePlate nothing will be added too. Anyways Thank you for your help.
Duck12 Duck12

2020/11/26

#
never mind it works Thank you so much!
You need to login to post a reply.