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

2019/4/12

Cheat Code Help

Celesterra Celesterra

2019/4/12

#
I am having trouble with a cheat code I am trying to make. I got the part where you type in the cheat just fine, but I have no clue as to how to make the cheat override previous code. Here is the code I am trying to override and the code I want to 'replace it'. Code to Override: //If the spacebar is pressed, a laser is fired if(Greenfoot.isKeyDown("space") && !shooting) { //If the rapid fire cheat isn't typed, keep limited firing getWorld().addObject(new Laser(), getX(), getY()); shooting = true; } Cheat Code: //If rapid is typed into cheat bar (accessed through shift + c), enable rapid fire if(Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("c")) { if(Greenfoot.ask("Enter cheat code: ") == code) { if(Greenfoot.isKeyDown("space") && !shooting) { getWorld().addObject(new Laser(), getX(), getY()); shooting = false; } } } I don't know where to put the cheat code, nor do I know how to enable it and KEEP it enabled. Any help would be appreciated. Thanks in advance!
danpost danpost

2019/4/12

#
Add a boolean field to indicate whether cheat is enabled or not:
private boolean cheatEnabled;
Set it to true if cheat code entered is correct. Outside of that use the value of the boolean to determine how to fire.
Celesterra Celesterra

2019/4/12

#
And when spacebar isn't being pressed, it resets the boolean variable 'shooting' to false.
Celesterra Celesterra

2019/4/12

#
Alright, I will try that out. Thanks for the fast response!
Celesterra Celesterra

2019/4/15

#
I made the boolean, but for some reason, I can't get the cheat to work still. Here is the entire code:
public class Rocket extends Actor
{
    //Variables/Lists used later
    private int speed = 3;
    private boolean cheatEnabled = false;
    private boolean shooting = false;
    
    public void act() 
    {
        checkKeys();
        cheats();
        
        if(cheatEnabled == true)
        {
            rapidFire();
        }
        
        if(cheatEnabled == false)
        {
            shootWithSpace();
        }
    }    
    
    private void checkKeys()
    {
        //If the up arrow key is pressed, the rocket moves up
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-speed);
        }
        
        //if the down arrow key is pressed, the rocket moves down
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+speed);
        }
        
        //If the right arrow key is pressed, the rocket moves forward
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+speed, getY());
        }
        
        //if the left arrow key is pressed, the rocket moves backwards
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-speed, getY());
        }
    }
    
    private void shootWithSpace()
    {
        //If the spacebar is pressed, a laser is fired
        if(Greenfoot.isKeyDown("space") && !shooting)
        {
            getWorld().addObject(new Laser(), getX(), getY());
            Greenfoot.playSound("pew.mp3");
            shooting = true;
        }
        
        //If the space bar isn't pressed, reset the shooting variable to false
        if(!Greenfoot.isKeyDown("space"))
        {
            shooting = false;
        }
    }
    
    private void cheats()
    {
        //If rapid is typed into cheat bar (accessed through shift + c), enable rapid fire
        if(Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("c") && cheatEnabled == false)
        {
            if(Greenfoot.ask("Enter cheat code:") == "rapid")
            {
                if(cheatEnabled = false)
                {
                    cheatEnabled = true;
                }
            }
        }
    }
    
    private void rapidFire()
    {
        //If the rapid fire cheat is enabled, lasers will be fired rapidly
        if(Greenfoot.isKeyDown("space") && !shooting)
        {
            getWorld().addObject(new Laser(), getX(), getY());
            Greenfoot.playSound("pew.mp3");
            shooting = false;
        }
        
        //If the space bar isn't pressed, reset the shooting variable to false
        if(!Greenfoot.isKeyDown("space"))
        {
            shooting = false;
        }
    }
}
danpost danpost

2019/4/15

#
Line 75 has an assignment in it which precludes the block from ever executing.
Super_Hippo Super_Hippo

2019/4/15

#
Maybe this will work:
if ("rapid".equals(Greenfoot.ask("Enter cheat code:"))
To test if the cheat would work at all, you could remove the condition and just active it right away when shift+c is pressed.
Celesterra Celesterra

2019/4/15

#
@Super_Hippo I will give it a shot! Thanks!
danpost danpost

2019/4/15

#
danpost wrote...
Line 75 has an assignment in it which precludes the block from ever executing.
and you already know it is false from line 71. Remove lines 75, 76 and 78
Celesterra Celesterra

2019/4/15

#
It's working now. Thank you both!
You need to login to post a reply.