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

2016/4/27

Problem with IsKeyDown() and backgrounds

M8R1X M8R1X

2016/4/27

#
Hi, I hava some problems with the following code, the M key doesn't seam to go back to the "main menu".
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Main here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Main extends World
{

    /**
     * Constructor for objects of class Main.
     * 
     */
    public Main()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1280, 725, 1); 
    }
    public void act(){
        showText("A for Bank", 50, 20);
        showText("B for Chalet", 55, 40);
        showText("C for Club House", 80, 60);
        showText("D for Consulate", 70, 80);
        showText("E for Hereford Base", 90, 100);
        showText("F for House", 55, 120);
        showText("G for Kafe Dostoyevsky", 110, 140);
        showText("H for Kanal", 50, 160);
        showText("I for Oregon", 55, 180);
        showText("J for Presidential Plane", 110, 200);
        showText("K for Yacht", 50, 220);
        
        if (Greenfoot.isKeyDown("K")){
            showText("", 50, 20);
            showText("", 55, 40);
            showText("", 80, 60);
            showText("", 70, 80);
            showText("", 90, 100);
            showText("", 55, 120);
            showText("", 110, 140);
            showText("", 50, 160);
            showText("", 55, 180);
            showText("", 110, 200);
            showText("", 50, 220);
            while(true){
              setBackground("YACHT-1.png");
              Greenfoot.delay(200);
              setBackground("YACHT0.png");
              Greenfoot.delay(200);
              setBackground("YACHT1.png");
              Greenfoot.delay(200);
              setBackground("YACHT2.png");
              Greenfoot.delay(200);
            }
        }
        if (Greenfoot.isKeyDown("M")){
           showText("A for Bank", 50, 20);
           showText("B for Chalet", 55, 40);
           showText("C for Club House", 80, 60);
           showText("D for Consulate", 70, 80);
           showText("E for Hereford Base", 90, 100);
           showText("F for House", 55, 120);
           showText("G for Kafe Dostoyevsky", 110, 140);
           showText("H for Kanal", 50, 160);
           showText("I for Oregon", 55, 180);
           showText("J for Presidential Plane", 110, 200);
           showText("K for Yacht", 50, 220);
           setBackground("bricks3.jpg");
        }
        
        
    }
}
danpost danpost

2016/4/27

#
The 'while' loop, lines 46 through 55 appears to be an infinite loop. The condition, 'true', will obviously never be 'false' and there are no 'break' statements within the block to break out of the loop.
You need to login to post a reply.