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

2014/4/15

sub-classes that extends actor

GamesGrinder1998 GamesGrinder1998

2014/4/15

#
On my game, i have stack of button classes and i want to make it go under a sub class that extends under an actor, i was just wandering how i would i set it out and what code should go in their, all they do is if it is clicked, it sets a new world....i need help plz
danpost danpost

2014/4/15

#
Show the code to one of your button classes and it would be easier to say.
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
danpost wrote...
Show the code to one of your button classes and it would be easier to say.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
this is the button on the menu that links it to single player mode
 
/**
 * Write a description of class Player1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Button1 extends Actor
{
    /**
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new MyGame());
        }   
    }
}
danpost danpost

2014/4/15

#
There does not appear to be a need to group your button classes under one subclass of Actor. But, if you really want to, it would be a fairly empty class like the following:
1
public class Buttons extends greenfoot.Actor{}
Then have all your button classes extend Buttons.
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
danpost wrote...
There does not appear to be a need to group your button classes under one subclass of Actor. But, if you really want to, it would be a fairly empty class like the following:
1
public class Buttons extends greenfoot.Actor{}
Then have all your button classes extend Buttons.
so there wouldn't be any button codes that the classes would inherit from button
danpost danpost

2014/4/15

#
GamesGrinder1998 wrote...
so there wouldn't be any button codes that the classes would inherit from button
Not unless you wanted to add something.
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
danpost wrote...
GamesGrinder1998 wrote...
so there wouldn't be any button codes that the classes would inherit from button
Not unless you wanted to add something.
ok, thanks...i have "next" buttons that obviously go to the next screen, but i have to make a new one for every screen, is there another way to do this instead of making 10 of these
danpost danpost

2014/4/15

#
Show the code of two of these 10 button classes and I will see what can be done.
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
danpost wrote...
Show the code of two of these 10 button classes and I will see what can be done.
Well i haven't created them yet but it will be like the previous codes if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new Screen1()); } Then another one will go like this... if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new Screen2()); } also they go under the public void act
danpost danpost

2014/4/15

#
Well, you could add an instance int field to the NextButton class and have the world that creates it pass its screen number to it. Then, have the act method in the NextButton class use a series of 'if-else's or a 'switch' statement to proceed to the proper world.
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
33
import greenfoot.*;
 
public class NextButton extends World
{
    int screen;
 
    public NextButton()
    {
        this(0);
    }
 
    public NextButton(int screenNum)
    {
        screen = screenNum;
    }
 
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (screen == 0) Greenfoot.setWorld(new Screen1());
            else if (screen == 1) Greenfoot.setWorld(new Screen2());
            else /** etc */
            /**  *****************   OR  *********************** */
            switch(screen)
            {
                case 0: Greenfoot.setWorld(new Screen1()); break;
                case 1: Greenfoot.setWorld(new Screen2()); break;
                /** etc */
            }
        }
    }
}
In the Screen1, Screen2, etc. worlds, create the NextButton object using the following format:
1
NextButton btnNext = new NextButton(1); // from within Screen1 world
You need to login to post a reply.