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

2024/3/25

Intstant transisiton while pressing space

zehbi-tracker zehbi-tracker

2024/3/25

#
When pressing space in GameOver screen it instantly goes to MapSelect instant of going to Startscherm and then pressing space again to transition into mapselect. Help is appreciated im a studend doing this voor school.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Startscherm here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Startscherm extends World {
    private GreenfootSound backgroundMuziek;
    private GreenfootSound soundEffect;
    public Startscherm() {    
        super(1280, 720, 1); 
        backgroundMuziek = new GreenfootSound("Brawling.mp3");
        backgroundMuziek.playLoop(); 
        soundEffect = new GreenfootSound("8bit Click Sound Effect.mp3");
    }
    
    public void act() {
        if (Greenfoot.isKeyDown("space")) {
            backgroundMuziek.stop(); 
            soundEffect.play();
            Greenfoot.setWorld(new MapSelect());
        }
        
        if (Greenfoot.isKeyDown("z")) {
            backgroundMuziek.stop(); 
            soundEffect.play();
            Greenfoot.setWorld(new Uitleg());
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOverScherm here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOverScherm extends World
{
    GifImage myGif = new GifImage("gameover.gif");
    private GreenfootSound backgroundMusic;
    /**
     * Constructor for objects of class GameOverScherm.
     * 
     */
    public GameOverScherm()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(480,360, 1); 
        backgroundMusic = new GreenfootSound("bonnie's lullaby.mp3");
        backgroundMusic.playLoop(); 
    }
    
     public void act()
    {
        setBackground( myGif.getCurrentImage() );
        if (Greenfoot.isKeyDown("space")) {
            backgroundMusic.stop(); // Stop background music
            Greenfoot.setWorld(new Startscherm());
        }
    }
}
danpost danpost

2024/3/26

#
zehbi-tracker wrote...
When pressing space in GameOver screen it instantly goes to MapSelect instant of going to Startscherm and then pressing space again to transition into mapselect. << Code Omitted >>
So, what you need to know is not when the space key is up or down, but when it goes up or down. I know two ways of handling that. One is by using the Greenfoot.getKey() method, which only returns a key when it is "entered" (during the release of the key), as opposed to using the Greenfoot.isKeyDown(String) method which only tells you the current state of the key. It does not tell you when the key changes its state from up to down or down to up. The other way is to actually track the state of the key by using a boolean variable which can be called spaceDown. Put one in each class that uses the space key:
private boolean spaceDown = true;
We will have the behavior that the space key will trigger the action (changing worlds) when the key goes from an up state to a down state. So, setting it to true will prevent it from triggering until the key is in a raised state and pressed again. Another way to handle that part is to add the following line into the constructor, if a world class, or either the constructor or the addedToWorld(World) method, if an actor:
spaceDown = Greenfoot.isKeyDown("space");
In all those classes, the code to trigger on key state change from up to down goes as follows:
if (spaceDown != Greenfoot.isKeyDown("space")) {
    spaceDown = ! spaceDown;
    if (spaceDown) {
        // perform "space" triggered action here
    }
}
zehbi-tracker zehbi-tracker

2024/3/26

#
okay thank you so much it works
You need to login to post a reply.