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

2019/2/4

problems with my bullet

Simson1000 Simson1000

2019/2/4

#
Hello, I have a problem with my new shooter game.It don't start and crashing instantly.I don't know what I did wrong. Here's the code from my shooter:
public class shooter1 extends Actor
{
    public static boolean richtung = true;


    public void act() 
    {   GreenfootImage lul= new GreenfootImage("links.png");
        GreenfootImage lal= new GreenfootImage("pixil-frame-0.png");
        
        while(getImage() == lul){
        richtung = true;
        }
      
       
        while(getImage() == lal){
        richtung = false;
        }
        
     if(Greenfoot.isKeyDown("d")){
        setLocation(getX() + 5,getY());
        setImage("pixil-frame-0.png");
        }
        if(Greenfoot.isKeyDown("a")){
        setLocation(getX() - 5,getY());
        setImage("links.png");
        
        }
        if(Greenfoot.isKeyDown("w")){
        setLocation(getX(),getY() -5);
        }
        if(Greenfoot.isKeyDown("s")){
        setLocation(getX(),getY() + 5);
        }
        if(Greenfoot.isKeyDown("space")){
    getWorld().addObject(new bullet(),getX(),getY());
    }
    }
    }
and here's the code from my bullet:
public void act() 
    {
        
        
        while(shooter1.richtung == true) {
        move(-10);
        while(shooter1.richtung != true) {
        move(10);
        }
    }  
danpost danpost

2019/2/4

#
You have several misconceptions about the way your code is processed. First, all your while loops are infinite loops. Once the condition is true, the loop will repeat forever and nothing else can then be executed. The condition must change within the loop so that it can be exited and the following command can then be processed. If statements would work, however, as greenfoot repeatedly calls the act method while your program is running to create the animation loop. All you need to put in the act method is what should be done on a single act step. Even better again, is to set the value of richtung when you set or change the image. Secondly, it is waste of computer resources to recreate the images every act step. I really do not see how either loop in the shooter1 class could ever execute as the image set to the actor would certainly not be either of the images you just created (similar instance of, but not the actual one you test for). This leads me to believe that your program crashes when you create a bullet or by some other code not given. Another issue is what the bullets will do when your shooter changes direction. I believe with what you have they will also change directions. The direction of the bullet must be set from the start and not rely on a variable that could change while the bullet exists. Next, your lines that set the shooter's image also have to create similar GreenfootImage objects as what you started the act method with. You should just set the image to one that has already been created, namely 'lul' or 'lal'. For the shooter, to create one, and only one, set of images and be done with it, move the code on line 7 and 8 up to between lines 3 and 6. In the setImage lines, change the String filenames to the appropriate field, lul or lal. It appears that all the mess with the richtung field is just to get the bullets to move the proper direction. All that would be unnecessary if the direction of a bullet was taken care of at the time the bullet was created. So, remove line 3 and lines 10 through 17; then, replace line 35 with the following:
Actor bullet = new bullet();
if (getImage() == lul) bullet.turrn(180);
getWorld().addObject(bullet, getX(), getY());
The bullet will no longer require access to the shooter1 class. In the bullet class act method, above, remove lines 5 through 7. That should fix the bullet loop problem.
You need to login to post a reply.