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/27

#
Can anyone help me i really stuck for weeks with this problem
thebrownkid23 thebrownkid23

2014/1/27

#
Well it would help with a little more information such as what exactly that point is. But what you can do is but an if statement around all the activities you want to pause in the act. So that if that condition is true continue to do everything, else, don't. This can be implemented with a counter too. Such as:
public void act(){
if(counter >= 20){
move(1);
counter = 0;
}else{
counter++
}
}
This would make it so that the only time the actor moves is when the condition is met. and then other factors outside of the if statement determine whether it is still being met or not
danpost danpost

2014/1/27

#
All you want to do is put a condition of the movement. Enclose the movement code within an 'if' block on the conditions that you want (for example: getX() < 400 , getX() < 400 && getY() < 200 , getX() != 50 || getY() != 100 , or if you have an actor as a limiting device: getIntersectingObjects(InvisibleFence.class).isEmpty() ).
Gerrit Gerrit

2014/1/28

#
mm i tried it before to set the move to 0 but that didn t work the situation is that in my world are incoming cars (objects) of class Car .and i also have an gassstationn that does nothing just stands there. what i want is that if i reached a car at the gassstation the car stops for 2 or 3 seconds , change picture of the car and make a litttle sound AND then go normally speed again. but while this car paussee , the other actors in the world should just act like they do .
danpost danpost

2014/1/28

#
Then you also need a timer and another condition to add. The condition being: 'if (pauseTimer == 0 && /* other conditions */) /* move code */'. When the conditions are right for the car to pause at the gasstation, set the value of 'pauseTimer' to about 150-180. Use something like 'if (pauseTimer > 0) { pauseTimer--; if (pauseTImer == 0) changeImage(); soundOff(); }' to run the timer.
Gerrit Gerrit

2014/1/28

#
it still doesn't work what i did ; public void atStation(){ Actor car; int pauseTimer=180; car = getOneIntersectingObject(Station.class); if (car!=null) if (pauseTimer>0){ pauseTimer --;} if (pauseTimer == 0) setImage("car.png"); move(0);} } in the Car class
danpost danpost

2014/1/28

#
I think you will need to show the entire Car class. AND, use the 'code' tag under the 'Post a reply' input box to insert your code. ALSO, auto-format your code before posting it (Ctrl-Shft-I).
Gerrit Gerrit

2014/1/28

#
public class Car extends Cars
{     
    
   
   Lives life;
 
  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;
                }
}
                     
                 
                 
public void atStation(){
 Actor car;
 int pauseTimer=180;
  car = getOneIntersectingObject(Station.class);
  if (car!=null)

   if (pauseTimer>0){
   pauseTimer --;}
   if (pauseTimer == 0)
    setImage("car.png");
    move(0);

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

  public void createNewVrachtSchip(){ 
        World myWorld;
        myWorld = getWorld();
        int worldHeight = myWorld.getHeight();
        int y = Greenfoot.getRandomNumber(worldHeight);
        myWorld.addObject(new Car(live),100, y);
        
        		
             
             } 
Gerrit Gerrit

2014/1/28

#
sorry this is the car class


public class Car extends Cars
{     
    
   
   Lives life;
 
  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;
                }
}
                     
                 
                 
public void atStation(){
 Actor car;
 int pauseTimer=180;
  car = getOneIntersectingObject(Station.class);
  if (car!=null)

   if (pauseTimer>0){
   pauseTimer --;}
   if (pauseTimer == 0)
    setImage("car.png");
    move(0);

  }
   
    
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/28

#
I do not see the methods 'followRoad' and 'placeRoad'; plus, I do not see the import statements. So, this is not the entire class. You may need to show the 'Cars' class as well. Line 48 of your most recent post of the Car class should be declared outside the method and the value of the field should be set to '180' within the 'if' block starting at line 50. Another condition to that 'if' at line 50 should be that the value of that field be at zero also. That is: 'if (car != null && pauseTimer == 0)'. You still may need to place one or more additional conditions there.
Gerrit Gerrit

2014/1/28

#
mm ok but the actor only change of image if it hits the station but not stops can you fix it this is what i got
public class Cars extends Actor
{
   

    
   
    
 
        

    public void act() 
    {
     
    } 
    private boolean carClicked()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse.getActor()==this)
        {
            return true;
        }
        else return false;
    }
    
    public void followRoad()
    {
        Actor removethis = getOneIntersectingObject(Path.class);
        int dist = 70;
        Actor closest = null;
        if(!getObjectsInRange(dist, Path.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, Path.class))
            {
                Actor car = (Actor) obj;
                int carDist = (int) Math.hypot(car.getX() - getX(), car.getY() - getY());
                if (closest == null || carDist< dist)
                {
                    closest = car;
                    dist = carDist;
                }
            }

            turnTowards(closest.getX(),closest.getY());
            move(2);
            
            

            if(closest.getY()>this.getY())
            {
                int newY = this.getY()+(1);
                this.setLocation(getX(),newY);
            } 
            else if(closest.getY()<this.getY())
            {
                int newY = this.getY()-(1);
                this.setLocation(getX(),newY);
            }

            if (dist<70 && removethis !=null){
                World world;
                world = getWorld();
                world.removeObject(removethis);
            }

        }       
        else if(getObjectsInRange(dist, Path.class).isEmpty())
        {
            move(2);
        }
    }

    public void placeroad()
    { 
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)  
        {  
            if (mouse.getButton() == 1 && carClicked()==true)  
            {  
                MouseInfo mousez = Greenfoot.getMouseInfo();
                getWorld().addObject(new Path(), mousez.getX(), mousez.getY());
            }
        }

    }
Gerrit Gerrit

2014/1/28

#
and this is car class
public class Car extends Cars
{     
    
   
   Lives life;
  int pauseTimer=180;
  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;
                }
}
                     
                 
                 
public void atStation(){
 Actor car;

  car = getOneIntersectingObject(Station.class);
  

   if (pauseTimer>0){
   pauseTimer --;}
   if (car!=null&&pauseTimer == 0){
  
    setImage("car.png");
    move(0);}

  }
   
    
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/28

#
In the Car class, line 54 ( 'move(0);' ) is useless. Also, running the timer should be done within a separate method that the act method calls:
private void runAtStationTimer()
{
    if (pauseTimer > 0)
    {
        pauseTimer--;
        if (pauseTimer == 0)
        {
            setImage("car.png");
            // play required sound
            // anything else that needs done at this time
        }
    }
}
It looks like you will have Car objects taking over the world. Once 'timer' for a car has reached zero, it will continuously create more Car objects (one per act cycle). I do not know what kind of progression you want with the creation of new cars; so, no help in this matter is available at this time. The 'atStation' method should be more like this:
private void atStation()
{
    Actor station = getOneIntersectingObject(Station.class);
    if (pauseTimer == 0 && station != null) // with possible other conditions
    {
        pauseTimer = 180;
    }
}
Lines 17 and 18, in the act method of the Car class (the movement code) should be qualified with the 'pauseTimer' field being zero:
if (pauseTimer == 0)
{
    followRoad();
    placeRoad();
}
This is what 'stops' the car from moving when the 'pauseTimer' is running.
Gerrit Gerrit

2014/1/28

#
i just did what your said it works that the car stops and change picture but it doesnt move further . it just stop . i want that it s move after it stops for two seconds. my game is about that cars coming each time in the world, each car should than send to a gasstation ,the car should pause for a second change picture that it looks like the car is fueled , the player gets points and than the player should send the car out of the world .. each car should be handled like this each time it get s points , but you lose a life if the car crashing to another car or it bumps to another object thats in the world (just stands there). if the player fueled like 10 cars the level must change and the cars must get faster so that it would be difficult.(maybe like changing world and adding speed ).
danpost danpost

2014/1/29

#
danpost wrote...
You still may need to place one or more additional conditions there.
The 'if' conditions to stop at the station, as is, will be true again after the timer runs down to zero. You need to add an instance boolean to the class to signify that the car has stopped at the station.
// add instance field
private boolean stoppedAtStation;
// add condition to 'if' clause
if (!stoppedAtStation && pauseTimer == 0 && station != null)
// inside the 'if' block for the previous line, add
stoppedAtStation = true;
There are more replies on the next page.
1
2