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

2014/1/27

How do i get one actor to pause for a while when reached a certain point(actor)

1
2
Gerrit Gerrit

2014/1/29

#
It still doesn't work this is what i did
public class Car extends Cars
{     
    
    private boolean stoppedAtStation;  
   Lives life;
  int pauseTimer=0;
  private int timer = 100;
  
public Car(Lives liven){
    life=liven;
}
       
    
public void act() {
      checkMeter();
       checkLives();
      followroad();
      placeroad();
     atStation();
      
      crashing();
      checkEdges();
      
      
         }
         
    
public void checkLives(){
    if(life.getValue()== 2){
        
    Greenfoot.stop();}
}
    
    public void checkEdges() {
          if(atWorldEdge()){
                 getWorld().removeObject(this);
                if(getWorld() == null) return;
                }
}
                     
private void runAtStationTimer()  
{  
    if (pauseTimer > 0)  
      {   pauseTimer--;  
     if (pauseTimer == 0)  
      {  setImage("car.png");  
        // play required sound  
                
        // anything else that needs done at this time  
     }      
     }  
  }                  
                 
public void atStation(){
  Actor station = getOneIntersectingObject(Dijk.class);  
    if (pauseTimer == 0 && station != null) // with possible other conditions  
    {  
        pauseTimer = 180;
        
    } 
    if (!stoppedAtStation && pauseTimer == 0 && station != null)  
// inside the 'if' block for the previous line, add  
     stoppedAtStation = true; 

   

  }
   
    
private void checkMeter(){
   
        if (timer > 0){
         timer--;
        if( timer == 0){
            createNewCar();
            
        }
     }
   }

  public void createNewCar(){ 
        World myWorld;
        myWorld = getWorld();
        int worldHeight = myWorld.getHeight();
        int y = Greenfoot.getRandomNumber(worldHeight);
        myWorld.addObject(new Car(live),100, y);
        
        		
             
             }
           
          
  public void crashing(){
        Actor car;
        car = getOneObjectAtOffset(0, 0, Car.class);
        if (car != null){
            getWorld().removeObject(car);
            leven.add(1);
           
        }
        
      
    }            
     
   public boolean atWorldEdge() {
       
       if(getX() < 10 || getX() > getWorld().getWidth() - 10)
       return true; 
       if(getY() < 10 || getY() > getWorld().getHeight() - 10)
       return true; 
       else 
       return false; }
   
       
   
    
 
   } 
danpost danpost

2014/1/29

#
Remove line 58 and insert it at line 64. Then remove lines 56, 57, and 60
joandvgv joandvgv

2015/2/10

#
Hey. Could you make this work? any Idea? I'm having the same trouble with other project.
danpost danpost

2015/2/10

#
The following is just a general idea of what you may need. It is not intended to be directly placed into your code, as is. The points of interest, as far as pausing at the gas station, are as follows: (1) arriving at the gas station (starting timer), and (2) leaving the gas station (a) begin to leave (timer exhausted) (b) has departed (station no longer detected) The boolean field 'stoppedAtStation' which you included is not necessary because the value of 'pauseTimer' can be used to track that state. The conditions would then be as follows:
if (pauseTimer <= 0) /** code to move */); // not allowed to move if value is greater than zero
if (pauseTimer == 0 && getOneObjectAtOffset(0, 0, Dijk.class) != null) pauseTimer == 361; // arrives at station
if (pauseTimer > 0) pauseTimer -= 2; // runs timer
if (pauseTimer < 0 && getOneObjectAtOffset(0, 0, Dijk.class) == null) pauseTimer = 0; // left station
I doubled the initial pause timer value and made it odd by adding one so that subtracting by two would skip over zero to negative one. That way, the last statement can be used to determine when the station is no longer detected before allowing a reset (the car will not move a little and pause at the station multiple times). In other words, the car will have a chance to leave the station before line 2 becomes true again.
joandvgv joandvgv

2015/2/16

#
Hey! Sorry for the late response, I was trying to fix some other stuff in my code before i was able to implement this one. But it did work, thank you. So, for anyone who needs this i'll let my implementation using danpost idea here:
public void act(){
  /** whatever your actor does, in my case it's a switch for setting an image depending on some values */
 if (pauseTimer<=0){
        moverse(); /** this is for turning when it comes to a certainf location */
        move(1);
        pasos++; //steps.  this records how many steps the actor has gone 
        }
if(haybasura() && (capacidad>randomNum)) { // if my truck found garbage and it still can collects it 
            Random rand = new Random();
            randomNum = rand.nextInt((200 - 50) + 1) + 50;
            pauseTimer=361; // here I start the timer
            recoger(); //Collect the garbage it found
        }
        if (pauseTimer > 0) pauseTimer -= 2;  //start
        if (pauseTimer < 0 && getOneObjectAtOffset(0, 0, Punto.class) == null) { /**this is null because i removed the object in recoger() method *//
           pauseTimer = 0; // stop collecting it 
Once again, thank you so much. I hope your idea and an example of my implementation could help someone.
You need to login to post a reply.
1
2