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

2017/1/4

Adding a button

1
2
jacquelinereilly jacquelinereilly

2017/1/4

#
I would like to create a button so it says play, and when the user presses it, the game starts, and brings up the next screen / world Thanks
Super_Hippo Super_Hippo

2017/1/4

#
Create a new class, call it Button and create an image for it. Then you can use this:
1
2
3
4
5
6
7
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        Greenfoot.setWorld(new WorldNameOfTheNextWorld());
    }
}
jacquelinereilly jacquelinereilly

2017/1/5

#
thank you !
jacquelinereilly jacquelinereilly

2017/1/5

#
What code do I put in the new world to display it once the button is pushed? Still learning how to use Greenfoot :)
Super_Hippo Super_Hippo

2017/1/5

#
What do you want to display there? When line 5 executes, a new world is created and is set active. So first the constructor of the world is executed and after it is active, its act-method and the act methods of the actors in this world will be executed every act-cycle instead of the ones in the other world.
danpost danpost

2017/1/5

#
Maybe it was not so clear. By placing the code provided by Super_Hippo (the act method) in the new Button class you just created (replacing any act method already there), any click on the actor while the scenario is running will initiate the new world (it will be displayed). That is what the code says it is to do "if a mouse button is pressed while on 'this', set the new world active" (although 'active' is only implied here).
jacquelinereilly jacquelinereilly

2017/1/5

#
Thank you for the help. I think I sort of understand, but I must be making a mistake somewhere, because when I push the button nothing happens. Maybe I am missing a line of code or something? I created an actor called Button, and placed the code provided by Super_Hippo in that class
danpost danpost

2017/1/5

#
jacquelinereilly wrote...
I must be making a mistake somewhere, because when I push the button nothing happens. Maybe I am missing a line of code or something? I created an actor called Button, and placed the code provided by Super_Hippo in that class
Please show the Button class code you have created (I will presume you create and add one into the world, start the project and click on it to test it).
jacquelinereilly jacquelinereilly

2017/1/5

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*; 
 
public class Button extends Actor
{
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        if (Greenfoot.mousePressed(this))
        {
            Greenfoot.setWorld (new LevelMenu ());
        }
    }   
}
danpost danpost

2017/1/5

#
Okay, now the World subclass code where you create and add the button.
jacquelinereilly jacquelinereilly

2017/1/5

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * This class MenuWorld
 *
 * Jacqueline Reilly
 * January 17, 2017
 */
public class MenuWorld extends World
{
 
    /**
     * Constructor for objects of class MenuWorld.
     *
     */
    public MenuWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        // Creating & adding menu object to world
        addObject (new MenuGreeting ("Mr. Krabs vs Plankton"), getWidth ()/4, getHeight ()/4);
         
        // Creating & adding play button to world
        Button button = new Button ();
        addObject (button, getWidth ()/2, getHeight ()/2);
    
     
         
           
    }
jacquelinereilly jacquelinereilly

2017/1/6

#
it works.. i just had to fix some minor errors :) I do have another question though. Once the user presses play, the next screen appears, which has an image for it, that looks like squares with the numbers for the different levels. is it possible to get the user to click just one of the squares?
danpost danpost

2017/1/6

#
NVM, reading your next post now.
jacquelinereilly jacquelinereilly

2017/1/6

#
it just keeps saying no fields when I click inspect, unless I am doing it wrong
jacquelinereilly jacquelinereilly

2017/1/6

#
ohh, haha okay !
There are more replies on the next page.
1
2