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

2018/9/2

shot problem help

Mounirmoon Mounirmoon

2018/9/2

#
i want to shoot a bullet every time i press space using the .equals(Greenfoot.getKey())) method but the bullet do not want to appear
import greenfoot.*;
  
public class CopyOfJumper extends Actor
{
    private int ySpeed;
  
    public CopyOfJumper()
    {
    }
    public int frame = 0;
    public int waktu = 5;
    GreenfootImage gambar13 = new GreenfootImage("standinge.png");
    GreenfootImage gambar14 = new GreenfootImage("touma_kamijou_and_index_jus_by_gameinvader-d9kwuh0.png");
    public void act()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
            
        }
        else // on ground
        {
            if ("up".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
            }
            else if (Greenfoot.isKeyDown("right"))
      {
        move(3);
        animasi();
          
      }
      else if (Greenfoot.isKeyDown("left"))
      {
          move(-3);
          animasin();
        }
       else if ("space".equals(Greenfoot.getKey()))
       {
           fire();
           }
        
       else
      {
          setImage(gambar13);
        }
        }
    }
    public GreenfootImage[] imagesR = 
{
    new GreenfootImage("R1.png"),
    new GreenfootImage("R2.png"),
    new GreenfootImage("R3.png"),
    new GreenfootImage("R4.png"),
    new GreenfootImage("R5.png"),
    new GreenfootImage("R6.png"),
    new GreenfootImage("R7.png"),
    new GreenfootImage("R8.png"),
    new GreenfootImage("R9.png"),
     
};
public GreenfootImage[] imagesL = 
{
    new GreenfootImage("L1.png"),
    new GreenfootImage("L2.png"),
    new GreenfootImage("L3.png"),
    new GreenfootImage("L4.png"),
    new GreenfootImage("L5.png"),
    new GreenfootImage("L6.png"),
    new GreenfootImage("L7.png"),
    new GreenfootImage("L8.png"),
    new GreenfootImage("L9.png"),
   
};
private void animasi()
{
    frame = (frame+1)%(9*3);
    setImage(imagesR[frame/12]);
}
private void animasin()
{
    frame = (frame+1)%(9*3);
    setImage(imagesL[frame/12]);   
}   
private void fire()
{
    setImage(gambar14);
    pierre var = new pierre();
    getWorld().addObject(var, getX(), getY());
    
}

}
Mounirmoon Mounirmoon

2018/9/2

#
line 47 it work with is key down method
danpost danpost

2018/9/2

#
You will never fire a bullet with the current code because you are guaranteed to use getKey method previously in either the if (!onGround) or its else part. Each time the method is called, it will return a new key or, if no key remains in the keyboard buffer, null. Also, with Greenfoot, the buffer is loaded at the beginning of each act cycle while the scenario is running, meaning that if you use the getKey method multiple times, only the one first acted on will be guaranteed to get any key. For player controlling, better is to use the isKeyDown method and only use the getKey method for String input or menu options (where it is only called from one place at a time during any act cycle). When using the isKeyDown method for firing, you will need to track the state of the firing key with a boolean variable. I use the following format:
/**add field */
private boolean spaceDown;

/** in act */
if (spaceDown != Greenfoot.isKeyDown("space")) // did state of key change
{ // yes
    spaceDown = !spaceDown; // tracking state
    if (spaceDown == true) // is firing key now down
    {
        fire();
    }
}
You need to login to post a reply.