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

2018/1/18

cheat code

1
2
danpost danpost

2018/1/19

#
If this is a level cheat and you have a World subclass for each level, then the code I provided should go in your World subclass act method within the key detection code. That means you would enclose the 'if' part inside some code like the following:
String key = Greenfoot.getKey();
if (key != null)
{
    // my 'if' code block here
    // rest of key processing (if any) here
}
destroyer9909 destroyer9909

2018/1/19

#
i dont i just want the game to stop when i click the keys could you possibly write write out the whole codes and where to put it please?
danpost danpost

2018/1/19

#
destroyer9909 wrote...
i dont i just want the game to stop when i click the keys could you possibly write write out the whole codes and where to put it please?
I gave you all of it -- and I said where to place it. There is nothing more.
destroyer9909 destroyer9909

2018/1/19

#
Where do i place the code?
danpost danpost

2018/1/19

#
destroyer9909 wrote...
Where do i place the code?
danpost wrote...
the code I provided should go in your World subclass act method within the key detection code. That means you would enclose the 'if' part inside some code like the following: << Code Omitted >>
(the fields go outside the act method)
destroyer9909 destroyer9909

2018/1/19

#
it still doesnt work
destroyer9909 destroyer9909

2018/1/19

#
public class MyWorld extends World
{
    private String[] cheat = { "up", "up", "down", "down", "left", "right", "left", "right", "b", "a" };
    private int cheatDepth = 0;
    private String key = Greenfoot.getKey();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }

    public void act() 
    {
        if(key !=null)
        {
            if (cheat[cheatDepth].equals(key))
            {
                cheatDepth++;
                if (cheatDepth == cheat.length) 
                {
                    Greenfoot.stop();
                    return;
                }
            }
            else cheatDepth = 0; 
        }   
    }
}
danpost danpost

2018/1/19

#
destroyer9909 wrote...
it still doesnt work
You placed your line 5 incorrectly. It is not a field, but a local variable. Move it, without the access modifier, into the act method (as first line).
You need to login to post a reply.
1
2