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

2015/3/25

HELP

SioneF SioneF

2015/3/25

#
I can only shoot one bullet at a time and I cannot shoot another bullet until the last bullet disappears from the world when it hits the edge.
danpost danpost

2015/3/25

#
Show your code (the Bullet class and the class that has the actor that fires the bullets).
SioneF SioneF

2015/3/25

#
theres no bullet class I just used it for an example. What I need to do is whenever I press "right" button, the Player actor shoots a strawberry. There can only be one strawberry that exist in the world at a time, after that I can shoot another one. I got the code below but I need to figure out how to only shoot one strawberry at a time and when that strawberry hits the right edge of world, I can shoot another one.
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
     

       if(Greenfoot.isKeyDown("up"))
        {
        setRotation(-90);
        setLocation(getX(), getY()-1);
        }
       
       else if(Greenfoot.isKeyDown("down"))
       {
        setRotation(90);
        setLocation(getX(), getY()+1);
       }
       
       if (Greenfoot.isKeyDown("right"))
       {
        Actor x = (Actor)getWorld().getObjects(Player.class).get(0);
        getWorld().addObject(new Delayer(), x.getX()+25, x.getY());
       }
       
        else
        setRotation(0);
        
      
       Actor lobs = getOneIntersectingObject(Lobstrosity.class);
       if(lobs != null) 
          {
             getWorld().removeObject(this);  
             gameOver();
    
          } 
       
    }
    
    public void gameOver()
    {
        Greenfoot.stop();
    }
   

}
here is the strawberry code
public class Delayer extends Actor
{
    /**
     * Act - do whatever the Lobstrosity wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
      move(1);
     int rightedge = getWorld().getWidth();
       if (this.getX()+10 == rightedge)
          {
            
            getWorld().removeObject(this);
            
            
          }
 }   
 

 }
SioneF SioneF

2015/3/25

#
thought id post the other class and world class also just to clarify the code
public class Lobstrosity extends Actor
{
    
    /**
     * Act - do whatever the Lobstrosity wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
      setRotation(180);
      setLocation(getX()-1, getY());
      
      Actor lobMonster = getOneIntersectingObject(Delayer.class);
          if(lobMonster != null) 
          {
             this.move(-1);  
    
          } 
      
      int edge = getWorld().getWidth()-750;
          
      if (this.getX() == edge) 
      { 
          getWorld().addObject(new Lobstrosity(), 490 + Greenfoot.getRandomNumber(740), Greenfoot.getRandomNumber(getWorld().getHeight()));
          getWorld().addObject(new Lobstrosity(), 490 + Greenfoot.getRandomNumber(740), Greenfoot.getRandomNumber(getWorld().getHeight()));
          getWorld().removeObject(this);
          
       }
      
     
      
    }  
    
 

    
}
heres the world class public class DodgingWorld extends World { public DodgingWorld() { super(750, 500, 1); addObject(new Player(),15,250); addObject(new Lobstrosity(), 700, 250); } //public void addObject(object, int x, int y) } and so basically this is the game i have to create A Game World that has a Human that can move up and down along the left edge of the world, * with lobster monsters moving from right to left. Whne a lobster hits the Human, the game * is over. Since lobsters love strawberries, they get delayed (i.e. stop moving) whenever * they are touching a strawberry. Thus, the human throws strawberrues at the lobsters, * horixontally across the world. Only one strawberry may be in the world at a single time.
danpost danpost

2015/3/25

#
The conditions that will allow a Delayer object to be thrown are: (1) the right arrow key is pressed; and (2) no delayers are currently in the world Change line 24 of the Player class code above to:
if (Greenfoot.isKeyDown("right") && getWorld().getObjects(Delayer.class).isEmpty())
SioneF SioneF

2015/3/25

#
Thank you!!
You need to login to post a reply.