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

2015/4/30

Trouble with timers

JMcCaffrey JMcCaffrey

2015/4/30

#
Hi. Lately, I've been spending hours trying to get this to work, but for whatever reason it is just not running correctly. Basically, what I'm doing it having a variety of timers going off to signal when the enemy is supposed to move. It's supposed to go down at a random time, let out a beam for a short pause, and then go back up. I've been able to get it to go down and shoot the beam, but it gets stuck there and won't move up. I've discovered that the bolded line is where the program is stopping:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy3 extends SmoothMover
{
   private int timer = 160;
   private int timer2 = 0;
   private int timer3 = 0;
   private int timer4 = 0;
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act()
   {
   if (timer == getRandomNumber(1,1000)) 
   {
      timer = 0;
      timer2 = 150;
   }
   if (timer > 80)
   {
    timer--;
    moveLeft();
   }
   if (timer <= 80 && timer != 0) 
   {
      timer--;
      moveRight();
   }
   if (timer < 1 && timer2 <= 150 && timer2 != 0 && timer2 != 2 && timer2 != 1)   
   {
       moveDown();
       timer2--;
   }
   if (timer < 1 && timer2 == 0) 
   {
       timer = 160;
   }
   if (timer2 <= 2 && timer2 != 0)
   {
      timer3 = 150;
   }
   if (timer3 <= 150 && timer3 != 0)   
   {
       timer3--;
       shootBeam();
   }
[b]   if (timer3 == getRandomNumber(1,5))
   {
       timer4 = 150;
       getWorld().removeObjects(getWorld().getObjects(Lazer.class));
   }[/b]
   if (timer4 <= 150 && timer4 != 0 && timer4 != 2 && timer4 != 1)
   {
       moveUp();
       timer4--;
   }
   if (timer4 <= 2 && timer4 != 0)
   {
       timer = 160;
   }
   }
   public void shootBeam()
   {
       Space space = (Space)getWorld();
       Lazer lazer = new Lazer();
       space.addObject(lazer, getX(), getY()+90);
   }
   public int getRandomNumber(int start,int end)
   {
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
   }
   public void moveRight()
   {
    int x = (getX() + 1);
    int y = (getY());
     
    setLocation(x, y);
   }
   public void moveLeft()
   {
    int x = (getX() - 1);
    int y = (getY());
     
    setLocation(x, y);
   }
   public void moveDown()
   {
    int x = (getX());
    int y = (getY()+1);
     
    setLocation(x, y);
   }
      public void moveUp()
   {
    int x = (getX());
    int y = (getY()-1);
     
    setLocation(x, y);
   }
}
That certain code is only one of the many different attempts I've made to get it to work, it seems like no matter what I do it won't go past there. I've discovered that everything around this code works, as when I change the values to set timer4 to 150, the code runs. It's just when I have the timer3 code that is supposed to remove the beam and make timer4 = 150, it just doesn't run. If you need anymore information to help, I'm open to give some.
azazeel azazeel

2015/4/30

#
just guessing : if (timer == getRandomNumber(1,1000)) line 21, it will always random every act
danpost danpost

2015/4/30

#
Line 54 has a similar issue. The random number is chosen each act cycle and in the 5 act methods that the time3 value is in range, the numbers may not match all five of those times. In other words, you are not guaranteed that the condition will be true to remove the laser in the five cycles that the timer is in range to match.
You need to login to post a reply.