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

2017/6/13

Title/Start screen

1
2
3
SchoolStuff SchoolStuff

2017/6/13

#
Hello, I have a question. So I have to make this game kinda like piano tiles and I want to make a title/start screen. I used some codes but if I click on enter it doesn't work. The code I used is: I put it as a world, but it didn't work... import greenfoot.*; /** * Write a description of class StartScreen here. * * @author (your name) * @version (a version number or a date) */ public class StartScreen extends World { /** * Constructor for objects of class StartScreen. * */ public StartScreen() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); } private void prepare() { TitleLetters titleletters = new TitleLetters(); addObject(titleletters, 400, 300); } public void act() { click(); } private void click() { if(Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new MusicFallWorld()); } } } can anyone help me?
lookaround lookaround

2017/6/13

#
Does it gives you some kind of fault message ? I do think if you put this in ---- if(Greenfoot.mouseClicked(this)) --- you didn't specify where it should click, it just clicked the world. Maybe this works
1
2
3
4
5
6
7
8
private void click()
{
 
if(Greenfoot.mouseClicked(TitleLetters.class)) {
Greenfoot.setWorld(new MusicFallWorld());
}
 
 }
Super_Hippo Super_Hippo

2017/6/13

#
'Greenfoot.mouseClicked(this)' returns true if you click on the world's background, it will return false if you click on any actor. Using 'null' instead of 'this' will check for any click. If you want to only check for clicks on the titleletters objects, then you need to do the following changes:
1
2
3
4
5
6
7
8
//move this line outside methods
private TitleLetters titleletters = new TitleLetters();
 
//so the prepare method only has this line in it
addObject(titleletters, 400, 300);
 
//and you use this for the condition in the click method
if (Greenfoot.mouseClicked(titleletters))
@lookaround: Your code does not work because you need to pass an object (or null) and not a class.
lookaround lookaround

2017/6/13

#
@Super_Hippo, thanks for the heads up
Yehuda Yehuda

2017/6/13

#
If your TitleLetters actor blocks the whole World then the click that it's looking for will never be detected (so you should pass null as Hippo suggested).
SchoolStuff SchoolStuff

2017/6/13

#
thank you for commenting fast, but @Super_Hippo I did what you said, but what I don't understand is where do I put private TitleLetters titleletters = new TitleLetters(); like can you be more specific, this is my first time you know, so I don't understand anything.
Super_Hippo Super_Hippo

2017/6/14

#
Put it outside methods right after the class starts, so
1
2
3
4
5
6
7
8
9
//imports
 
public class StartScreen extends World
{
    private TitleLetters titleletters = new TitleLetters();
     
    //constructor
    //...
}
SchoolStuff SchoolStuff

2017/6/14

#
Yes, it works, thank you!!
SchoolStuff SchoolStuff

2017/6/14

#
This has nothing to do with title screen, but we are facing another problem. So we are making a game, kind of like piano tiles. We made a counter and if you click on the block it adds 1 point, but if you click on the background the same thing happens. The code we used is: This is in class counter. public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.BLACK, Color.WHITE)); if(Greenfoot.mouseClicked(null)){ addScore(); } } How can we avoid this problem. And do you know how we can make the game over if you click on the background and if the blocks are gone but you haven't clicked on it yet? I hope that I'm clear. @Super_Hippo or someone else?
Yehuda Yehuda

2017/6/14

#
You seem to have mixed things up. You want the title screen to go away with any click so that argument should just be null. If you put null as the argument in the counter then it will add a point with every and any click, you have to put that mouseClicked 'if' statement in the piano tiles class and then have 'this' as the argument.
SchoolStuff SchoolStuff

2017/6/14

#
So how does that looks like?
Yehuda Yehuda

2017/6/14

#
You would have to access the Counter from the World.
1
2
3
4
5
6
7
public class Tile extends Actor {
    public void act() {
        if (Greenfoot.mouseClicked(this)) {
            //code for pressed tile
        }
    }
}
Super_Hippo Super_Hippo

2017/6/15

#
The counter doesn't need an act method. It only has to change its image when the value changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private int score = 0;
private String text = "";
 
public Counter(String txt)
{
    text = txt;
    updateImage();
}
 
public void add(int amount)
{
    score += amount;
    updateImage();
}
 
private void updateImage()
{
    setImage(new GreenfootImage(text + score, 24, Color.BLACK, Color.WHITE));
}
To get a reference to the counter, you can either use (if there is always only one counter in the world)
1
getWorld().getObjects(Counter.class).get(0)
or you save a reference in the 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
private Counter counter = new Counter("Score : ");
 
//called from constructor
addObject(counter, 300, 50);
 
public void act()
{
    //spawn new tiles
    //...
    if (Greenfoot.mousePressed(this)) //clicked the background -- only working if the space between the tiles are not occupied by other objects
    {
        gameOver();
    }
}
 
public void gameOver()
{
    //something which should happen when the game is lost
}
 
 
public Counter getCounter()
{
    return counter;
}
and then you can use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Tile class
 
public void act()
{
    setLocation(getX(), getY()+1); //or some speed depending on the time passed
    if (getY() > getWorld().getHeight()-getImage().getHeight()/2) //reached the bottom
    {
        ((MyWorld) getWorld()).gameOver();
    }
    else if (Greenfoot.mousePressed(this)) //mouse click on this object
    {
        ((MyWorld) getWorld()).getCounter().add(1); //add one to the counter
    }
}
SchoolStuff SchoolStuff

2017/6/15

#
I'm a little bit confused, thank you for the codes, but which one helps for the background. Because if I click on the background it adds 1 point, but I only want the points if you click on the block, I hope I'm you understand what I'm trying to say.
Super_Hippo Super_Hippo

2017/6/15

#
All three. First one is for the counter, second for the world and third one for tile.
There are more replies on the next page.
1
2
3