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

3 days ago

Help with keeping a key down after one press.

Someguyoutside Someguyoutside

3 days ago

#
I'm doing the spaceship game and I want to have a key to switch between control schemes. However the problem is that if I use .isKeyDown, I'd have to keep that key pressed the entire time. Is there a way to make it a single press only?
public void act() 
    {
        if(Greenfoot.isKeyDown("1"))
        {
            processKeys();
            trackMouse();
        }
        if(Greenfoot.isKeyDown("2"))
        {
            processKeysAlt();
        }
    }  
danpost danpost

3 days ago

#
Someguyoutside wrote...
I'm doing the spaceship game and I want to have a key to switch between control schemes. However the problem is that if I use .isKeyDown, I'd have to keep that key pressed the entire time. Is there a way to make it a single press only? << Code Omitted >>
Please provide the entire class codes as what was given is too limited to discern what might need changed to fix the issue.
Someguyoutside Someguyoutside

2 days ago

#
Here it is
import greenfoot.*;
public class Hero extends Actor
{
    private int speed_;
    int cooldown = 0;
    public Hero()
    {
        speed_ = 5;
    }
    
    public int getSpeed()
    {
        return speed_;
    }
    
    public void setSpeed(int s)
    {
        speed_ = s;
    }
    
    public void act() 
    {
        if(Greenfoot.isKeyDown("1"))
        {
            processKeys();
            trackMouse();
        }
        if(Greenfoot.isKeyDown("2"))
        {
            processKeysAlt();
        }
        processShot();
    }  
    //Alternate Controls
    public void processKeysAlt()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        
        else if(Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        
        else if(Greenfoot.isKeyDown("up"))
        {
            if(speed_ <= 10)
            {
                speed_++;
                speed_ = speed_ + 1;
            }
        }
        
        else if(Greenfoot.isKeyDown("down"))
        {
            if(speed_ > 0)
            {
                speed_--;
                speed_ = speed_ - 1;
            }
        }
        move(speed_);
    }
    //Main Controls
    public void processKeys()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation( getX() - speed_, getY() );
        }
        
        else if(Greenfoot.isKeyDown("d"))
        {
            setLocation( getX() + speed_, getY() );
        }

        if(Greenfoot.isKeyDown("w"))
        {
            setLocation( getX(), getY() - speed_ );
        }
        
        else if(Greenfoot.isKeyDown("s"))
        {
            setLocation( getX(), getY() + speed_ );
        }
    }
    //Mouse Tracking
    public void trackMouse()
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        if(mi != null)//program may crash without this
        {
            turnTowards(mi.getX(), mi.getY());
        }
    }
    
    public void shootLaser()
    {
        Laser laser = new Laser(5, 5);
        laser.setRotation(getRotation());
        getWorld().addObject(laser, getX(), getY());
    }
    
    public void shootFireball()
    {
        Fireball fireBall = new Fireball(1, 10);
        fireBall.setRotation(getRotation());
        getWorld().addObject(fireBall, getX(), getY());
    }
    
    public void shootMissle()
    {
        Missle missle = new Missle(7, 7);
        missle.setRotation(getRotation());
        getWorld().addObject(missle, getX(), getY());
    }
    
    public void processShot()
    {
        if(cooldown >0)
        {
            cooldown -= 1;
        }
        
        if(Greenfoot.isKeyDown("space") && (cooldown == 0))
        {
            shoot(new Laser(5, 2));
            cooldown = 10;
        }
        
        else if(Greenfoot.isKeyDown("f") && (cooldown == 0))
        {
            shoot(new Fireball(2, 10));
            cooldown = 20;
        }
        else if(Greenfoot.isKeyDown("g") && (cooldown == 0))
        {
            shoot(new Missle(10, 7));
            cooldown = 50;
        }
    }
    
    public void shoot(Projectile fireThis)
    {
        fireThis.setRotation(getRotation());
        getWorld().addObject(fireThis, getX(), getY());
    }
}
danpost danpost

2 days ago

#
Someguyoutside wrote...
Here it is << Code Omitted >>
Add a boolean field to track which key set is currently being used:
private boolean usingAlt; // (initially false by default
To change its value, use;
if (Greenfoot.isKeyDown("1")) usingAlt = false;
if (Greenfoot.isKeyDown("2")) usingAlt = true;
Call the appropriate move/turn method with:
if (usingAlt) processKeysAlt(); else
{
    processKeys();
    trackMouse();
}
The two code sets above would replace lines 23 thru 31
You need to login to post a reply.