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

2018/2/26

How do I make the bullets shoot at in interval?

Andreizota29 Andreizota29

2018/2/26

#
I want the bullets to shoot at in interval. Something like the binding of isaac. How can I make that? This is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class player extends Actor
{
   
    public void act() 
    {
          if(Greenfoot.isKeyDown("w"))  
 {  
     int x = getX();
     int y = getY();
     int ny = getY()-2;
     setLocation(x,ny) ;   
     setImage("player sus1.png");
 }
      
        if(Greenfoot.isKeyDown("s"))  
 {  
     int x = getX();
     int y = getY();
     int ny = getY()+2;
     setLocation(x,ny) ;   
     setImage("player jos1.png");
 }
           if(Greenfoot.isKeyDown("a"))  
 {  
     int x = getX()-2;
     int y = getY();
     int ny = getY();
     setLocation(x,ny) ;   
     setImage("player stanga1.png");
 }
  if(Greenfoot.isKeyDown("d"))  
 {  
     int x = getX()+2;
     int y = getY();
     int ny = getY();
     setLocation(x,ny) ;   
     setImage("player dreapta1.png");
 }

if (Greenfoot.isKeyDown("up"))  
        {  
            firew(); 
        } 
        if (Greenfoot.isKeyDown("down"))  
        {  
            fires(); 
        } 
        if (Greenfoot.isKeyDown("left"))  
        {  
            firea(); 
        } 
        if (Greenfoot.isKeyDown("right")) 
        {  
            fired(); 
        } 
}
private void firew()
{
    bullet bullet = new bullet();
    getWorld().addObject(bullet, getX(), getY());
      bullet.setRotation(270);
}
private void fires()
{
    bullet bullet = new bullet();
    getWorld().addObject(bullet, getX(), getY());
      bullet.setRotation(90);
}
private void firea()
{
    bullet bullet = new bullet();
    getWorld().addObject(bullet, getX(), getY());
      bullet.setRotation(180);
}
private void fired()
{
    bullet bullet = new bullet();
    getWorld().addObject(bullet, getX(), getY());
      bullet.setRotation(0);
}
}
And this is the bullet subclass
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends Actor
{
    public void act()
    {
        move(5);
       

}

}
I dont know how to make it so that it moves at in interval because I have seen some suggestions, but I have a diferent kind of bullet code and i just simply cannot make it work on my own. Please help!
Super_Hippo Super_Hippo

2018/2/26

#
Use a timer (int variable). Decrease it by 1 every act cycle. Only make shooting possible if it is (for example) below 0. If it shoots, set the timer to some positive value. (Higher value = longer delay)
but I have a diferent kind of bullet code
You bullet code is basically nothing, but that shouldn't matter.
Andreizota29 Andreizota29

2018/2/26

#
Super_Hippo wrote...
Use a timer (int variable). Decrease it by 1 every act cycle. Only make shooting possible if it is (for example) below 0. If it shoots, set the timer to some positive value. (Higher value = longer delay)
but I have a diferent kind of bullet code
You bullet code is basically nothing, but that shouldn't matter.
I'm sorry but I just have no idea how to make that. Could you give me some kind of starting code for me to see?
Super_Hippo Super_Hippo

2018/2/26

#
private int timer=0, shootingDelay=60;

//in act
if (--timer<0)
{
    if (/**check for shooting*/)
    {
        timer = shootingDelay;
        //shoot the bullet
    }
}
Andreizota29 Andreizota29

2018/2/27

#
I did it. Thanks!
You need to login to post a reply.