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

2020/1/20

My Checkmarks won't go away

Chris498 Chris498

2020/1/20

#
All the code works but if I don't click all 3 images displayed (labeled objName), the checkmarks don't go away when I press the Start button.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Start extends Actor
{
    int [] objects = new int[3];
    String [] objName = {"Duck.png", "Bear.png", "Bike.png", "Pig.png", "Flower.png", "Kangaroo.png"};
    String [] bigObjName = {"BigDuck.png", "BigBear.png", "BigBike.png", "BigPig.png", "BigFlower.png", "BigKangaroo.png"};
    String [] bgName = {"Background1.png", "Background2.png", "Background3.png", "Background4.png", "Background5.png", "Background6.png"};

    public void act() 
    {

        if(Greenfoot.mouseClicked(this))
        {
            try
            {
                Object object = new Object();
                Object ob = getWorld().getObjects(Object.class).get(0);
                Object ob1 = getWorld().getObjects(Object.class).get(1);
                Object ob2 = getWorld().getObjects(Object.class).get(2);
                getWorld().removeObject(ob);
                getWorld().removeObject(ob1);
                getWorld().removeObject(ob2);
            }
            catch(Exception e)
            {
            }
        }

        if(Greenfoot.mouseClicked(this))
        {
            getWorld().setBackground(bgName[Greenfoot.getRandomNumber(6)]);
            for(int i = 0; i < 3; i++)
            {
                objects[i] = Greenfoot.getRandomNumber(6);
                Object object = new Object();
                getWorld().addObject(object, Greenfoot.getRandomNumber(975) + 25, Greenfoot.getRandomNumber(718) + 25);
                while(object.getX() < 220 && object.getY() < 350)
                {
                    object.setLocation(Greenfoot.getRandomNumber(975) + 25, Greenfoot.getRandomNumber(718) + 25);
                }
                object.setImage(objName[objects[i]]);
                object.objNum = i;
            }
            BigBike bigBike = getWorld().getObjects(BigBike.class).get(0);
            BigDuck bigDuck = getWorld().getObjects(BigDuck.class).get(0);
            BigBear bigBear = getWorld().getObjects(BigBear.class).get(0);
            bigBike.setImage(bigObjName[objects[0]]);
            bigDuck.setImage(bigObjName[objects[1]]);
            bigBear.setImage(bigObjName[objects[2]]);
            try
            {
                CheckMark cm = getWorld().getObjects(CheckMark.class).get(0);
                CheckMark cm1 = getWorld().getObjects(CheckMark.class).get(1);
                CheckMark cm2 = getWorld().getObjects(CheckMark.class).get(2);
                getWorld().removeObject(cm);
                getWorld().removeObject(cm1);
                getWorld().removeObject(cm2);
            }
            catch(Exception e)
            {
            }
        }
    }    
}
danpost danpost

2020/1/20

#
I think you need to list what all is in your world, how they interact and what exactly you want to happen when the Start object is clicked.
What if you put the "catch(Exception e)" just above BigBike bigBike = getWorld().getObjects(BigBike.class).get(0); BigDuck bigDuck = getWorld().getObjects(BigDuck.class).get(0); BigBear bigBear = getWorld().getObjects(BigBear.class).get(0); bigBike.setImage(bigObjName]); bigDuck.setImage(bigObjName]); bigBear.setImage(bigObjName]); ?????
danpost danpost

2020/1/22

#
Helpard_With_A_Stick? wrote...
What if you put the "catch(Exception e)" just above << Code Omitted >> ?????
The try/catch structure should not be used here. Any error the code may produce can be avoided with precautionary if conditions. Also, the while loop can also be avoided by properly placing the actors to begin with.
Chris498 Chris498

2020/1/22

#
danpost wrote...
Helpard_With_A_Stick? wrote...
What if you put the "catch(Exception e)" just above << Code Omitted >> ?????
The try/catch structure should not be used here. Any error the code may produce can be avoided with precautionary if conditions. Also, the while loop can also be avoided by properly placing the actors to begin with.
The while loop is necessary because the code randomly places the actors in the world, so it makes sure it isn't placed in a certain area
danpost danpost

2020/1/23

#
Chris498 wrote...
The while loop is necessary because the code randomly places the actors in the world, so it makes sure it isn't placed in a certain area
No ... not necessary:
getWorld().addObject(object, 220+Greenfoot.getRandomNumber(780), 350+Greenfoot.getRandomNumber(393));
This should do the job nicely.
Chris498 Chris498

2020/1/23

#
danpost wrote...
Chris498 wrote...
The while loop is necessary because the code randomly places the actors in the world, so it makes sure it isn't placed in a certain area
No ... not necessary:
getWorld().addObject(object, 220+Greenfoot.getRandomNumber(780), 350+Greenfoot.getRandomNumber(393));
This should do the job nicely.
no, it won't because i don't want a whole wall not chosen, i just have a small checklist that should be excluded.
danpost danpost

2020/1/23

#
Chris498 wrote...
no, it won't because i don't want a whole wall not chosen, i just have a small checklist that should be excluded.
Sorry, my bad ... you want both conditions together, not individually. Okay, this is a way:
int rand = Greenfoot.getRandomNumber(780*350+1000*393);
int startX =220;
if (rand >= 780*350)
{
    startX = 0;
    rand += 220*350;
}
int rangeX = 1000-startX;
getWorld().addObject(object, startX+rand%rangeX, rand/rangeX);
Chris498 Chris498

2020/1/23

#
danpost wrote...
Chris498 wrote...
no, it won't because i don't want a whole wall not chosen, i just have a small checklist that should be excluded.
Sorry, my bad ... you want both conditions together, not individually. Okay, this is a way:
int rand = Greenfoot.getRandomNumber(780*350+1000*393);
int startX =220;
if (rand >= 780*350)
{
    startX = 0;
    rand += 220*350;
}
int rangeX = 1000-startX;
getWorld().addObject(object, startX+rand%rangeX, rand/rangeX);
But is the while loop the problem? Because if not, i would prefer to keep the code to things that I fully understand
danpost danpost

2020/1/23

#
Chris498 wrote...
But is the while loop the problem? Because if not, i would prefer to keep the code to things that I fully understand
It is not down-right wrong. It is just more CPU extensive because you unnecessarily repeat a process that does not have a need to do so.
You need to login to post a reply.