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

2017/4/21

My plane still fires even though it shouldnt

7oel 7oel

2017/4/21

#
My plane is supposed to only be able to fire when the countdown is finished, so i added a public variable called countdownfinished, which gets changed to true when the countdown is finished, yet it still doesnt work. heres my countdown code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown extends Actor
{
    private GreenfootImage Countdown3 = new GreenfootImage("Countdown3.png");
    private GreenfootImage Countdown2 = new GreenfootImage("Countdown2.png");
    private GreenfootImage Countdown1 = new GreenfootImage("Countdown1.png");
    private GreenfootImage CountdownGo = new GreenfootImage("CountdownGo.png");
    private static final int countdowntime = 50;
    private int countdowntimer;
    public static boolean countdownfinished;
    /**
     * Act - do whatever the Countdown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        Countdown3();
    }
    public void Countdown3()
    { 
        
        if(countdowntimer > 62)
        {
           Countdown2();
           return;
        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void Countdown2()
    {
        setImage(Countdown2);
        
        if(countdowntimer > 124)
        {
            Countdown1();
            return;

        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void Countdown1()
    {
        setImage(Countdown1);
        
        if(countdowntimer > 186)
        {
            CountdownGo();
            return;
        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void CountdownGo()
    {
        setImage(CountdownGo);
        
        if(countdowntimer > 248)
        {
            World world;
            world=getWorld();
            world.removeObject(this);
            countdownfinished = true;
            

        }
        else
        {
            countdowntimer++;
        }
        
    }
    
    
    
}
And heres my plane code
import greenfoot.*; 
/**
 * Write a description of class Airplane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Airplane extends Actor
{
   private static final int max_acts_between_shots = 10;
   private int actsBetweenShots = Integer.MAX_VALUE;
   private static boolean countdowndone = Countdown.countdownfinished;
    /**
     * Act - do whatever the Airplane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
               getImage().scale(60,60);
               move(2);
        if(Greenfoot.isKeyDown("A"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            turn(5);
        }
        if(countdowndone = true)
        {
        if(Greenfoot.isKeyDown("SPACE"))
        {
            if(actsBetweenShots > max_acts_between_shots) {
          bullet b = new bullet();
          getWorld().addObject(b, getX(), getY());
           actsBetweenShots = 0;
           b.setRotation(getRotation());
                       
        }
        actsBetweenShots++;
        
        }
    }
        
    }  
}
Yehuda Yehuda

2017/4/21

#
The booleans should not be static. You should access the variable in Countdown from the instance you created when adding it into the world. So if you have the following in your world...
// class level scope
private Countdown countDown = new Countdown();

// where ever you add this count down in
addObject(countDown, x, y);

// a regular method
public Countdown getCountDown() {
    return countDown;
}
..and the following in your counter...
// line 17
private boolean countDownFinished;

// a method
public boolean isCountDownFinished() {
    return countDownFinished;
}
...then to access the variable in Airplane you would do...
// on line 29
if (((WorldName) getWorld()).getCountDown().isCountDownFinished())
7oel 7oel

2017/4/21

#
Yehuda wrote...
The booleans should not be static. You should access the variable in Countdown from the instance you created when adding it into the world. So if you have the following in your world...
// class level scope
private Countdown countDown = new Countdown();

// where ever you add this count down in
addObject(countDown, x, y);


// a regular method
public Countdown getCountDown() {
    return countDown;
}
..and the following in your counter...
// line 17
private boolean countDownFinished;

// a method
public boolean isCountDownFinished() {
    return countDownFinished;
}
...then to access the variable in Airplane you would do...
// on line 29
if (((WorldName) getWorld()).getCountDown().isCountDownFinished())
When I add these lines what do i put on line 81 of the airplane code? And where do I put the regular method?
Yehuda Yehuda

2017/4/21

#
7oel wrote...
When I add these lines what do i put on line 81 of the airplane code?
I can't tell you since the Airplane class only has 46 lines.
And where do I put the regular method?
In the World (as mentioned) where the other methods go. If you want a specific place then it can go right before the last closing squiggly bracket.
7oel 7oel

2017/4/21

#
Yehuda wrote...
7oel wrote...
When I add these lines what do i put on line 81 of the airplane code?
I can't tell you since the Airplane class only has 46 lines.
And where do I put the regular method?
In the World (as mentioned) where the other methods go.
I mean countdown sorry
Yehuda Yehuda

2017/4/21

#
7oel wrote...
Yehuda wrote...
7oel wrote...
When I add these lines what do i put on line 81 of the airplane code?
I can't tell you since the Airplane class only has 46 lines.
I mean countdown sorry
countDownFinished = true;
7oel 7oel

2017/4/21

#
Yehuda wrote...
7oel wrote...
Yehuda wrote...
7oel wrote...
When I add these lines what do i put on line 81 of the airplane code?
I can't tell you since the Airplane class only has 46 lines.
I mean countdown sorry
countDownFinished = true;
Thanks!
You need to login to post a reply.