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

2018/1/18

cheat code

1
2
destroyer9909 destroyer9909

2018/1/18

#
im currently working on my final project in greenfoot for school and i want to create a cheat code by press "up" "up" "down" down" "left" "right "left" "right" "b" "a" in a row and then something happens my question is how do you save the memory of keypressed using arrays so that when all of these keys have been pressed in the correct sequence something else happens
xbLank xbLank

2018/1/18

#
.. literally what i posted a few days ago. LINK So for your case:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
import greenfoot.*;
public class MyWorld extends World
{
    private String key = "",
                  code = "";
    private int wait = 0;
    public MyWorld()
    {   
        super(600, 400, 1);
    }
    public void act()
    {
        if(wait > 0)
            wait--;
        else code = "";
        if((key = Greenfoot.getKey()) != null)
        {
            wait = 120;
            code = HandleKey(key);
            key = "";
        }
        switch(code.toLowerCase())
        {
            default:
                break;
            case "upupdowndownleftrightleftrightba":
                Greenfoot.stop();
                break;
        }
    }
    private String HandleKey(String key)
    {
        switch(key)
        {
            default:
                return code+key;
            case "space":
                return code+" ";
            case "backspace":
                return (code.length() > 0) ? code.substring(0,code.length()-1) : code; //simulate backspace(remove last character from string if possible)
            case "escape": //reset progress
                wait = 0;
                return "";
        }
    }
}
danpost danpost

2018/1/18

#
I would list the cheat code sequence in an array field and include an index field to track the depth of the correct cheat code entered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// fields
private String[] cheat = { "up", "up", "down", "down", "left", "right", "left", "right", "b", "a" };
private int cheatDepth = 0;
 
//in key detection code
if (cheat[cheatDepth].equals(key))
{
    cheatDepth++;
    if (cheatDepth == cheat.length) // cheat code completed correctly
    {
        // level up (or whatever)
        return;
    }
}
else cheatDepth = 0; // not part of cheat code
CxVercility CxVercility

2018/1/19

#
That's clean as hell lol. Kudos.
destroyer9909 destroyer9909

2018/1/19

#
when i try to put that in my actor class it says cannot find symbol variable key.
destroyer9909 destroyer9909

2018/1/19

#
am i suppose to copy both of the codes
danpost danpost

2018/1/19

#
destroyer9909 wrote...
when i try to put that in my actor class it says cannot find symbol variable key.
You will need to provide what code you have in the class so that the problem can be determined.
Super_Hippo Super_Hippo

2018/1/19

#
Replace 'key' with 'Greenfoot.getKey()'.
destroyer9909 destroyer9909

2018/1/19

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Hi here. * * @author (your name) * @version (a version number or a date) */ public class Hi extends Actor { private String cheat = { "up", "up", "down", "down", "left", "right", "left", "right", "b", "a" }; private int cheatDepth = 0; public void act() { if (cheat.equals(Greenfoot.getKey())) { cheatDepth++; if (cheatDepth == cheat.length) { Greenfoot.stop(); return; } } else cheatDepth = 0; } } it wont stop when i pressed all the keys
Super_Hippo Super_Hippo

2018/1/19

#
Is a Hi actor in your world? Maybe you should place the code in your world instead.
destroyer9909 destroyer9909

2018/1/19

#
ya Hi is an actor in my world i will try
destroyer9909 destroyer9909

2018/1/19

#
it still doesnt work
Super_Hippo Super_Hippo

2018/1/19

#
Do you use 'Greenfoot.getKey' anywhere else in your code?
destroyer9909 destroyer9909

2018/1/19

#
no
destroyer9909 destroyer9909

2018/1/19

#
the cheatDepth is not even going up
There are more replies on the next page.
1
2