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

2013/3/28

How to make something spawn once?

ColonelCaboose ColonelCaboose

2013/3/28

#
I am making a game where you can spend points to get powerups. I've got it to actually work where you can collect points and spend them on various items, but I'm having a problem where if I have too many points and I buy a low costing upgrade, it purchases the same item for as many it can buy even if I tap the key once on my keyboard. I've listed the coding below to show my problem. My problem is if I have 20 exp points, when I click the "S" key once to buy a ray shield it buys two of them instead of just one. Is there anyway to tell it to only buy one item at a time?
 public void rayShield()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            if(exp >= 10)
            {
                exp = exp - 10;
                getWorld().addObject(new RayShieldArwing(), getX(), getY() - 40);
            }
        }
    }
Draymothisk Draymothisk

2013/3/28

#
You need to set a timer or a boolean (that's how I would do it) for when you press the key, so it knows that it has been pressed, and only adds up once. If you touch a key, even for a second, it counts that key several times. Try something to this nature: if(Greenfoot.isKeyDown("s")) { boolean keyPressed = true; if(keyPressed == true) { keyPressed = false; if(exp >= 10) { exp = exp - 10; getWorld().addObject(new RayShieldArwing(), getX(), getY() - 40); } }
danpost danpost

2013/3/28

#
@Draymothisk, the code as you gave it above would be equivalent to:
if(Greenfoot.isKeyDown("s") && exp >=10)
{
    exp -= 10;
    getWorld().addObject(new RayShieldArwing(), getX(), getY()-40);
}
This would still spawn two or more if 'exp' was greater than or equal to 20. To use a boolean to track the state of the key, you would:
// add this instance object field
private boolean sPressed;
// then the 'if' block would be
if (!sPressed && Greenfoot.isKeyDown("s"))
{
    if (exp >= 10)
    {
        exp -= 10;
        getWorld().addObject(new RayShieldArwing(), getX(), getY() - 40);
        sPressed = true;
    }
}
if (sPressed && !Greenfoot.isKeyDown("s")) sPressed = false;
With this, holding down the "s" key will spawn an object when 'exp' reaches 10. If you want to negate any keypress that is done while 'exp' is less than 10 and force the user to press the key again once it reaches 10, then move line 10 down one line (outside the following closing squiggly bracket).
danpost danpost

2013/3/28

#
Another solution is to use 'getKey' instead of 'isKeyDown', as follows:
String key = Greenfoot.getKey();
if ("s".equals(key) && exp >=10)
{
    exp -= 10;
    getWorld().addObject(new RayShieldArwing(), getX(), getY() - 40);
}
This will spawn the object when the key is released, while the previous code will spawn the object when the key is pressed.
ColonelCaboose ColonelCaboose

2013/3/28

#
It works, thank you so much!
You need to login to post a reply.