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

2021/3/26

Single shot

SanderNeele SanderNeele

2021/3/26

#
Hello, When I shoot a spear with the spacebar it releases 8 or 10 spears at once. With just one press on the space bar. Can anybody tell me how I can make sure that just one spear is being released? Thanks! Sander (speer = spear) the code: if (Greenfoot.isKeyDown("space")) { World world = getWorld(); world.addObject(new Speer(), getX(), getY()); }
danpost danpost

2021/3/26

#
SanderNeele wrote...
When I shoot a spear with the spacebar it releases 8 or 10 spears at once. With just one press on the space bar. Can anybody tell me how I can make sure that just one spear is being released?
Track the state of the "space" key with a boolean field and use it to determine when the state of the key changes; then, shoot when one of the two possible changes (up to down or down to up) is detected.
SanderNeele SanderNeele

2021/3/26

#
I do not understand what I have to add in the code. I'm quiet new to Greenfoot. Can you show me what the right code is? Thanks!
Risen Risen

2021/3/26

#
I think he meant this(you also need to add boolean isShoot)
 if (Greenfoot.isKeyDown("space") && !isShoot) {
            World world = getWorld();
            world.addObject(new Speer(), getX(), getY());
isShoot = true;
        }
else 
isShoot = false;
SanderNeele SanderNeele

2021/3/26

#
This doesnt work like I had in mind. When I press the space bar many spears are released. I want 1 spear at a time. The code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * A Held swims around in the sea and eats fleas. But be careful for sharks.
 * 
 */
public class Held extends Personages
{
    public boolean isShoot;    
    /**
     * Do what a fish has to do.
     */

 

  public void act() {
checkToetsen();
checkVijhand();
}

public void checkToetsen() { 
        if (Greenfoot.isKeyDown("W")) { 
            moveUp(); 
        } 
         if (Greenfoot.isKeyDown("A")) { 
            moveLeft(); 
        } 
         if (Greenfoot.isKeyDown("S")) { 
            moveDown(); 
        } 
         if (Greenfoot.isKeyDown("D")) { 
            moveRight(); 
        } 
        
        if (Greenfoot.isKeyDown("space") && !isShoot) {
            World world = getWorld();
            world.addObject(new Speer(), getX(), getY());
        isShoot = true;
        }
        else 
        isShoot = false;      
   
}
public void checkVijhand()
{
    if(isTouching(Tijger.class))
    {
        Level1 wereld = (Level1)getWorld();
        Healthbar healthbar = wereld.haalHealthbarOp();
        healthbar.healthbarDaalt();
    }
    
}
}





Risen Risen

2021/3/26

#
SanderNeele wrote...
This doesnt work like I had in mind. When I press the space bar many spears are released. I want 1 spear at a time. The code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * A Held swims around in the sea and eats fleas. But be careful for sharks.
 * 
 */
public class Held extends Personages
{
    public boolean isShoot;    
    /**
     * Do what a fish has to do.
     */

 

  public void act() {
checkToetsen();
checkVijhand();
}

public void checkToetsen() { 
        if (Greenfoot.isKeyDown("W")) { 
            moveUp(); 
        } 
         if (Greenfoot.isKeyDown("A")) { 
            moveLeft(); 
        } 
         if (Greenfoot.isKeyDown("S")) { 
            moveDown(); 
        } 
         if (Greenfoot.isKeyDown("D")) { 
            moveRight(); 
        } 
        
        if (Greenfoot.isKeyDown("space") && !isShoot) {
            World world = getWorld();
            world.addObject(new Speer(), getX(), getY());
        isShoot = true;
        }
        else 
        isShoot = false;      
   
}
public void checkVijhand()
{
    if(isTouching(Tijger.class))
    {
        Level1 wereld = (Level1)getWorld();
        Healthbar healthbar = wereld.haalHealthbarOp();
        healthbar.healthbarDaalt();
    }
    
}
}





you mean, when you clamp space, for example in 1 second releasing 1 spear?
SanderNeele SanderNeele

2021/3/26

#
Yes, so it doesnt rapid fires spears
danpost danpost

2021/3/26

#
SanderNeele wrote...
I do not understand what I have to add in the code. I'm quiet new to Greenfoot. Can you show me what the right code is? Thanks!
I mean:
// a field like
private boolean spaceDown;

// with this in act
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = ! spaceDown;
    if (spaceDown) getWorld().addObject(new Speer(), getX(), getY());
}
You need to login to post a reply.