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

2019/4/11

How to make objects appear after clicking all the given actors

Zsazsz Zsazsz

2019/4/11

#
We are making a game wherein if we click the objects it will disappear. We want to know how to make an object appear wherein if we click all the objects in that level, a button should appear that if we click it, it will switch it to the next world or level. here is our code hope you could help thanks import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { super(1024, 768, 1); GreenfootImage bg = new GreenfootImage("garden_base.jpg"); bg.scale(getWidth(), getHeight()); setBackground(bg); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ public void prepare() { ball ball = new ball(); addObject(ball,249,391); car car = new car(); addObject(car,803,358); car.setLocation(803,358); car.setLocation(823,355); car.setLocation(802,368); chess chess = new chess(); addObject(chess,489,698); cube cube = new cube(); addObject(cube,546,673); doll doll = new doll(); addObject(doll,379,402); teddy teddy = new teddy(); addObject(teddy,621,156); teddy.setLocation(630,188); nextlvl nextlvl = new nextlvl(); addObject(nextlvl,969,46); } }
danpost danpost

2019/4/12

#
Presuming nextlvl is your button, it looks like you are adding the button into the world right at the start. I presume that no actor will be in the world after all items have been clicked on. Therefore, if you add the button at that time, the condition to add the button goes away (the fact that no actors are in the world). So, you can simply add something like this:
1
2
3
4
5
6
7
private Actor button = new nextlvl();
 
public void act()
{
    if (getObjects(Actor.class).isEmpty()) addObject(button, 969, 46);
    if Greenfoot.mouseClicked(button)) Greenfoot.setWorld(new Level2());
}
Then, remove the last two lines in your prepare method.
You need to login to post a reply.