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

2020/11/14

Removing an object once it has met the criteria

1
2
potsmoka potsmoka

2020/11/14

#
danpost wrote...
Change 'getWorld()' to '((MyWorld)getWorld())'.
It doesn't give an error now, but when I run 1) the object X disappears right away 2) When I pick up items it doesn't actually add score, it remains 0
danpost danpost

2020/11/14

#
potsmoka wrote...
1) the object X disappears right away
Show current codes for object X.
2) When I pick up items it doesn't actually add score, it remains 0
Show MyWorld class codes.
potsmoka potsmoka

2020/11/14

#
danpost wrote...
potsmoka wrote...
1) the object X disappears right away
Show current codes for object X.
2) When I pick up items it doesn't actually add score, it remains 0
Show MyWorld class codes.
Object X:
private int countCollected;
    public void act() 
    {
        Actor Mario = (Actor)getWorld().getObjects(Mario.class).get(0); 
        turnTowards (Mario.getX(), Mario.getY()); 
        move(3);
        
    
        
        if (++countCollected == 3) {
        getWorld().removeObject(this);
        
    }
    }    
My World:
public class MyWorld extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {  
        super(600, 400, 1); 

        prepare();

        setPaintOrder(Counter.class, Dark.class, PipeOpening.class, Mario.class, Zonnepaneel.class, Toolbox.class, Koperdraad.class, Lampuit.class, Lampaan.class, Bout.class);
    }

    public Counter getCounter()
    { return counter;

    }
danpost danpost

2020/11/14

#
potsmoka wrote...
Object X: << Code Omitted >>
Where is code collecting the items?
My World: << Code Omitted >>
Show prepare method.
potsmoka potsmoka

2020/11/14

#
The code collecting items it in my actor:
public class Mario extends Actor
{
    /**
     * Act - do whatever the Mario wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        movement();
        takeBout();
        takeToolbox();
        takeKoperdraad();
    }

    public Mario()
    {
        setImage(right);

    }
    private GreenfootImage left= new GreenfootImage("left.png");
    private GreenfootImage right= new GreenfootImage ("right.png");
    private GreenfootImage up= new GreenfootImage ("up.png");
    private GreenfootImage down= new GreenfootImage ("down.png");
    public void movement() 
    { if (Greenfoot.isKeyDown("right")){
            move(3);
            setImage(right);
        } 
        else if (Greenfoot.isKeyDown("left")) {
            move(-3);
            setImage(left);
        }
        else if (Greenfoot.isKeyDown("up")){
            setLocation(getX(), getY()-3);
            setImage(up);
        }
        else if (Greenfoot.isKeyDown("down"))  {
            setLocation(getX(), getY()+3);
            setImage(down);
        }
    }

    public void takeBout(){
        Actor bout;
        bout = getOneObjectAtOffset(0, 0, Bout.class);
        if(bout !=null)
        {
            World myWorld = getWorld();
            ((MyWorld)getWorld()).getCounter().addScore();
            myWorld.removeObject(bout);

        }
    }
        
        
        public void takeToolbox(){
            Actor toolbox;
            toolbox = getOneObjectAtOffset(0, 0, Toolbox.class);
            if(toolbox !=null)
            {
                
                World myWorld = getWorld();
                ((MyWorld)getWorld()).getCounter().addScore();
                
                myWorld.removeObject(toolbox);
            }

        }

        public void takeKoperdraad(){
            Actor koperdraad;
            koperdraad = getOneObjectAtOffset(0, 0, Koperdraad.class);
            if(koperdraad !=null)
            {
                World myWorld = getWorld();
                
                myWorld.removeObject(koperdraad);
                ((MyWorld)getWorld()).getCounter().addScore();
            }
        }
    }
here is the prepare:
private void prepare()
    {
        Mario mario = new Mario();
        addObject(mario,235,212);
        mario.setLocation(218,180);
        Dark dark = new Dark();

        Bout bout = new Bout();
        addObject(bout,456,121);
        bout.setLocation(258,58);
        Toolbox toolbox = new Toolbox();
        addObject(toolbox,553,372);
        bout.setLocation(189,23);
        Koperdraad koperdraad = new Koperdraad();
        addObject(koperdraad,27,376);
        Lampuit lampuit = new Lampuit();
        addObject(lampuit,306,189);
        lampuit.setLocation(303,182);
        Zonnepaneel zonnepaneel = new Zonnepaneel();
        addObject(zonnepaneel,48,194);
        toolbox.setLocation(560,23);
        PipeOpening pipeOpening = new PipeOpening();
        addObject(pipeOpening,594,343);
        toolbox.setLocation(564,30);
        lampuit.setLocation(352,176);
        mario.setLocation(274,196);
        Lampaan lampaan = new Lampaan();
        addObject(lampaan,155,179);
        lampuit.setLocation(152,188);
        mario.setLocation(207,184);
        Koperdraad koperdraad2 = new Koperdraad();
        addObject(koperdraad2,394,203);
        removeObject(koperdraad2);
        mario.setLocation(180,168);

        addObject(dark,180,168);
        mario.setLocation(199,175);
        dark.act();
        lampaan.setLocation(151,184);
        removeObject(dark);
        Counter counter = new Counter();
        addObject(counter,32,30);
        counter.setLocation(49,22);
        counter.setLocation(48,24);
    }

}
danpost danpost

2020/11/14

#
potsmoka wrote...
The code collecting items it in my actor: << Code Omitted >>
Remove lines 10 and 11 from your Object X code above. In Mario class, place the following, repeated after every addScore line:
if (((MyWorld)getWorld()).getCounter().getScore() == 3)
{
    getWorld().removeObject(this);
    return;
}
here is the prepare: << Code Omitted >>
Remove 'Counter ' from the beginning of line 41. In Mario class, start lines 11 and 12 (last 2 lines in act) with:
if (getWorld() != null) 
potsmoka potsmoka

2020/11/14

#
danpost wrote...
potsmoka wrote...
The code collecting items it in my actor: << Code Omitted >>
Remove lines 10 and 11 from your Object X code above. In Mario class, place the following, repeated after every addScore line:
if (((MyWorld)getWorld()).getCounter().getScore() == 3)
{
    getWorld().removeObject(this);
    return;
}
here is the prepare: << Code Omitted >>
Remove 'Counter ' from the beginning of line 41. In Mario class, start lines 11 and 12 (last 2 lines in act) with:
if (getWorld() != null) 
Picking items up still does not add score and object X does not disappear when all 3 are picked up.
danpost danpost

2020/11/15

#
danpost wrote...
In Mario class, place the following, repeated after every addScore line:
if (((MyWorld)getWorld()).getCounter().getScore() == 3)
{
    getWorld().removeObject(this);
    return;
}
Sorry -- wrong place. Remove from after every addScore in Mario class. Add it to act in object X class.
potsmoka potsmoka

2020/11/15

#
Thank you it finaly works!!
You need to login to post a reply.
1
2