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

2012/5/8

Is there a contrary from this code

1
2
3
4
martijn13039 martijn13039

2012/5/8

#
I´ve a code that greenfoot stopt after something happen
Greenfoot.stop();
but how can you make a code that greenfoot start when I put on a special picture (for a menu)
 public void act() 
    {
        if(Greenfoot.mouseClicked(this)){getWorld().removeObject(this);}
    } 
plcs plcs

2012/5/8

#
Not by using Greenfoot.stop(), only if you yourself write your code to have a behaviour that acts like Greenfoot.stop. Maybe with a boolean you can set, which everything checks for running its act method? Or maybe it resets everything when set to true. Otherwise, the Run/Pause in the menu is the normal way to achieve this.
martijn13039 martijn13039

2012/5/8

#
oke but i want to make a menu and that the game start when i click on the picture (is there no code like if mousecliked greenfoot get start
plcs plcs

2012/5/8

#
You can do this with your code, but you shouldn't use Greenfoot.start() for this behaviour, as when Greenfoot is stopped nothing on the screen will function until you press the run button. What you should do instead is not use Greenfoot.stop()/start(), but rather make it so your code will not create the actors or change the world until you have clicked the picture, (which is probably an actor). Best idea is probably to look at having a world variable with getter/setter methods, where clicking on the picture will set the variable to true, and all act method/creation methods check this method to know if they can start running.
martijn13039 martijn13039

2012/5/8

#
have you maby an idea how I can do that
trash1000 trash1000

2012/5/8

#
You want to implement a pause menu. Right now you use Greenfoot.stop() to implement the pause and want to start Greenfoot again when a certain object is clicked. Like plcs said, you cannot do this with Greenfoot.stop() and Greenfoot.start(). Instead you could integrate a boolean variable in your world along with getter and setter methods:
private boolean paused = false;
public boolean isPaused() { // call in your actors act methods
    return paused;
}
public void pause() { // call when the pause button is clicked
    paused = true;
}
The trick is now that you check that variable to be false in any of your classes' act methods.
public void act() {
    if(!(World) getWorld()).isPaused()) { // be sure to cast the return object to your world
        // whatever the object has to do
    }
}
martijn13039 martijn13039

2012/5/8

#
wil the code than something like this (but there is an error
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Start extends Actor
{
    private boolean paused = false;
    public boolean isPaused() {
        return paused;  }  

    public void pause() {
        paused = true;  }  

    public void act() 
    {
        if(Greenfoot.mouseClicked(this)){getWorld().removeObject(this);}
        if(!(World) getWorld()).isPaused()) 
    }    
}

danpost danpost

2012/5/8

#
The ol' NullPointerException! Line 15 is a mistake many programmers fall for. When line 14 removes 'this', the 'getWorld()' command will now return 'null', and you cannot call a method on a null object. Besides, you would still get an error 'method not found - isPaused()' because you need your sub-class of World, not just World (because that method is not in World, but in the sub-class of World that you created). Change your 'act()' method to:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        MyWorld mw = (MyWorld) getWorld();
        mw.removeObject(this);
        if (!mw.isPaused()); // you have not completed this
    }
}
You will have to change all 'MyWorld' to the name of your sub-class of World.
trash1000 trash1000

2012/5/9

#
    private boolean paused = false;  
    public boolean isPaused() {  
        return paused;  }    
  
    public void pause() {  
        paused = true;  }    
This stuff belongs into your world. The world maintains the status of the scenario - whether it's paused or not. The subclasses of Actor just check for your world's variable 'paused' to be false or else they won't do anything. This means, anything you want your actors to do has to happen only when isPaused() from world returns true.
martijn13039 martijn13039

2012/5/9

#
what is wrong at this import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Start extends Actor { public void act() { if (Greenfoot.mouseClicked(this)) { World2 mw = (World2) getWorld(); mw.removeObject(this); if (!mw.isPaused()); // } } }
danpost danpost

2012/5/9

#
As far as the 'pausing' of the scenario at the start, for a menu click, I suggest the following: 1) In the world constructor, only set up a background image of the world and only add the menu objects to the world. 2) Since the menu objects are the only objects in the world, only it can be running and waiting for a click. When a menu item is clicked on, record the item in the world class by calling the method 'setMenuItem(String)' (see below) 3) In the world act method, make the first line a check to see if the menu is done and, if not, whether menuItem has a value yet. If both are true then we can now remove the menu objects and add the objects for the game with special attention to the menu item that was clicked on (that was saved in the world class). 4) Add a boolean variable in the world class called 'menuDone' and set it initially to false. Once the menu item is clicked and the game is set up, change it to true. Use it as part of the checks in the beginning of the world class act method.
private String menuItem = "";
private boolean menuDone = false;

public void act()
{
    if (!menuDone && !"".equals(menuItem))
    {
        removeObjects(getObjects(Menu.class));
        // set up background for game
        // add objects for game
        menuDone = true;
    }
    //  rest of act (for game play)
}

public void setMenuItem(String muItem)
{
    menuItem = muItem;
}
martijn13039 martijn13039

2012/5/10

#
hey danpost i´ve try your code but there is some error with private STring menuItem, I hope you can helpm me and I can understand the code much better
import greenfoot.*;
private String menuItem = "";
private boolean menuDone = false;

public class Start extends Actor
{

public void act()
{
    if (!menuDone && !"".equals(menuItem))
    {
        removeObjects(getObjects(Menu.class));
        
        menuDone = true;
    }
    
}

public void setMenuItem(String muItem)
{
    menuItem = muItem;
}
}
martijn13039 martijn13039

2012/5/11

#
I hope you got an idea what is wrong
trash1000 trash1000

2012/5/11

#
You probably do not call the method setMenuItem(). It's really hard to say what is wrong, when you just say "there is an error" instead of posting the actuall error message. Greenfoot will tell you what error occured and won't say "error - better luck next time". However, as for a menu you could try it otherwise. A class called Button - you pass a number to objects of this class when you construct them. In the act method different world methods are called when clicked.
public class Button  extends Actor
{
    private int act;
    
    public Button(int act) {
        //do not forget to give your button an image
        this.act = act;
    }

    public void act() {
        if(Greenfoot.mouseClicked(this)) {
            if(act == 0) {
                ((MyWorld) getWorld()).start();
            }
            if(act == 1) {
                //another call to a world method
            }
            if(act == 2) {
                //another call to a world method
            }
        }
    }
}
martijn13039 martijn13039

2012/5/11

#
het trash 1000 i´ve got an erro on start there stay cannont find symbol- methodstard() I hope you got an idea
import greenfoot.*;



public class Button  extends Actor
{
    private int act;
    
    public Button(int act) {
             this.act = act;
    }

    public void act() {
        if(Greenfoot.mouseClicked(this)) {
            if(act == 0) {
                ((World2) getWorld()).start();
            }
            if(act == 1) {
              
            }
            if(act == 2) {
                //another call to a world method
            }
        }
    }
}
There are more replies on the next page.
1
2
3
4