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

2013/3/5

Delaying time before each object is created?

henrYoda henrYoda

2013/3/5

#
Hi guys I need some help with some code, I need to make it so that every time a bullet is created it waits some time before creating another one. At the moment it will create loads per second, even if i just touch a key for a split second. Here is the code, anyone know how to do what I want? Or is it not possible?
{
 public void act() 
    {
        move(7);
        disappear();
        if (getWorld() == null)return;
        collisionDetection();
        //colision detection and point awarding
        
        
        
        //create bullet object
    }
   public void collisionDetection()
   {
    Actor Key = getOneIntersectingObject(Key.class);
            if (Key != null) 
               { // We've hit an asteroid!
                hitObject();
                getWorld().removeObject(Key);
                getWorld().removeObject(this);
            
    }
}    //make bullet disappear
 public void disappear()
 {
if( getX() <= 4 || getX() >= getWorld().getWidth()-2) 
    
        {
            getWorld().removeObject(this);
            return;
        }
      if( getY() <= 4 || getY() >= getWorld().getHeight()-2) 

        {
            getWorld().removeObject(this);
            return;
        }
    
}      
     
    
    public void hitObject()
    {
        
        menu menuWorld = (menu) getWorld();
        Counter counter = menuWorld.getCounter();
        counter.scoreCount(5);
        
         
            
            
                
            
           
        }
    }
henrYoda henrYoda

2013/3/5

#
Woops, this is the wrong code, this is not the code that creates the bullet, though it might help. Here is the code that creates the bullet:
{
    /**
     * Act - do whatever the key2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //call methods
   public void act()
{   movementAndRotation();
    shoot();
}             
    
    
    
   //movement
   private void movementAndRotation()
   {
        //up
        if (Greenfoot.isKeyDown("w"))
        {
            setRotation(270);
            move(4);
        }
        //down
        if (Greenfoot.isKeyDown("s"))
        {
            setRotation(90);
            move(4);
        }
        //left
        if (Greenfoot.isKeyDown("a"))
        {
            setRotation(180);
            move(4);
        }
        //right
        if (Greenfoot.isKeyDown("d"))
        {
            setRotation(0);
            move(4);
        }
         //Restart game
        if (Greenfoot.isKeyDown("r"))
        {
            menu.restart();
        }
        if (Greenfoot.isKeyDown("escape"))
        {
        Greenfoot.setWorld(new Piano());
        }
        
    }
   public void shoot()
     {
       //Shooting
        if (Greenfoot.isKeyDown("right"))
        {
         
        
            Actor bullet = new bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(0);
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);
        }
        if (Greenfoot.isKeyDown("down"))
        {
         
        
            Actor bullet = new bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(90);
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);
        }
        if (Greenfoot.isKeyDown("left"))
        {
         
        
            Actor bullet = new bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(180);
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);
        }
        if (Greenfoot.isKeyDown("up"))
        {
         
        
            Actor bullet = new bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(270);
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);
        }
    }
}
Oxy Oxy

2013/3/5

#
what i would do would be a shooting boolean variable. is !shooting, shooting will be possible, something like...
//in your act method
if (wait > 0)  wait--;
if (wait == 0) shooting = false;

//in shooting class
if (//command for shooting// && !shooting)
{
        shootingCode();
        shooting = true;
        wait+= (some constant);
}
henrYoda henrYoda

2013/3/5

#
Could you please incorporate your code into my code? I'm not really sure how to do it...
Oxy Oxy

2013/3/5

#
//   -----   IM assuming your variables are somewhere up here   -----//
private int wait = 0;
private final int waitTime = 10;
private boolean shooting = false;
//-------------------------------------------------------------------------//

{  
    /** 
     * Act - do whatever the key2 wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    //call methods  
   public void act()  
{   
    checkShootingStatus();
    movementAndRotation();  
    shoot();  
}               
      
      
   private void checkShootingStatus()////THIS WHOLE method i just added...
{
	if (wait > 0) wait--;
        if (wait == 0) shooting = false;

}
   //movement  
   private void movementAndRotation()  
   {  
        //up  
        if (Greenfoot.isKeyDown("w"))  
        {  
            setRotation(270);  
            move(4);  
        }  
        //down  
        if (Greenfoot.isKeyDown("s"))  
        {  
            setRotation(90);  
            move(4);  
        }  
        //left  
        if (Greenfoot.isKeyDown("a"))  
        {  
            setRotation(180);  
            move(4);  
        }  
        //right  
        if (Greenfoot.isKeyDown("d"))  
        {  
            setRotation(0);  
            move(4);  
        }  
         //Restart game  
        if (Greenfoot.isKeyDown("r"))  
        {  
            menu.restart();  
        }  
        if (Greenfoot.isKeyDown("escape"))  
        {  
        Greenfoot.setWorld(new Piano());  
        }  
          
    }  
   public void shoot()  
     {  
       //Shooting  
        if (Greenfoot.isKeyDown("right") && !shooting)  //! followed by boolean basically means if false
        {  
            shooting = true;
            wait += waitTime;
            Actor bullet = new bullet();  
            getWorld().addObject(bullet, getX(), getY());  
            bullet.setRotation(0);  
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);  
        }  
        if (Greenfoot.isKeyDown("down") && !shooting) //- 
        {  
           shooting = true; //-
           wait = waitTime; //-
          
            Actor bullet = new bullet();  
            getWorld().addObject(bullet, getX(), getY());  
            bullet.setRotation(90);  
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);  
        }  
        if (Greenfoot.isKeyDown("left") && !shooting)  //-
        {  
           shooting = true;//-
           wait = waitTime;//-
            Actor bullet = new bullet();  
            getWorld().addObject(bullet, getX(), getY());  
            bullet.setRotation(180);  
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);  
        }  
        if (Greenfoot.isKeyDown("up") && !shooting) //- 
        {  
           shooting = true;//-
           wait = waitTime;//-
            Actor bullet = new bullet();  
            getWorld().addObject(bullet, getX(), getY());  
            bullet.setRotation(270);  
           Actor Key = getOneObjectAtOffset(0, 0, Key.class);  
        }  
    }  
}  
OK i took the code from your second post and added to it. notice i put comments where i added code. by the way the variable waitTime is how long to wait before shooting again, to make it waiting longer or shorter just change that number. let me know how it works, if not we can see what to do from there...
henrYoda henrYoda

2013/3/5

#
Yup, it works, how would i change the time between each shot though?
Oxy Oxy

2013/3/5

#
easy you will notice i put in a variable name waitTime and its = to 10. to increase the time between each shot, increase the variable. so instead of private final int waitTime = 10; do private final int waitTime = 20; if you implemented that variable like i did. then changing that one variable will change the speed for all direction bullets.
You need to login to post a reply.