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

2019/3/30

Emergency Guys!! How can randomly moving object not move through wall??

1
2
3
yalamberingnam yalamberingnam

2019/3/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bom here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bom extends Actor
{
    
    private GreenfootImage image1;
    private GreenfootImage image2;
    
    public Bom(){
        image1 = new GreenfootImage("blue.png");
        image2 =new GreenfootImage("red.png");
        setImage(image1);
    
    }
    
    public void switchImage(){
        if (getImage() == image1){
                setImage(image2);
        }
        else{
            setImage(image1);
        }
    
    }
    /**
     * Act - do whatever the Bom wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(2);
       //turnAtEdge();
       unknownMove();
       hitCar();
       switchImage();
      checkForWall();
    }    
    
    /*public void turnAtEdge(){
        if (isAtEdge()){
        
            turn(17);      
        }
        
    }*/
    
    public void unknownMove(){
    if(Greenfoot.getRandomNumber(100)>60){
        
        turn(+Greenfoot.getRandomNumber(90) -45);
        checkForWall();
    
    }
    }
    
    public void hitCar(){
    
            if(isTouching(car.class)){
                
            World myWorld = getWorld();
            GameOver over =new GameOver();
            myWorld.addObject(over,myWorld.getWidth()/2,myWorld.getHeight()/2);
            Greenfoot.stop();
            
            
            }
    
    }
    
    public void checkForWall(){
        if(isTouching(wall1.class)){
            setLocation(getX(),getY());  
        
        }
    
    
    
    }
    
    
}
This is my assignment guys. Deadline is veray close.
yalamberingnam yalamberingnam

2019/3/30

#
Guys help
yalamberingnam yalamberingnam

2019/3/30

#
see checkForWall method plz.
yalamberingnam yalamberingnam

2019/3/30

#
@mik
yalamberingnam yalamberingnam

2019/3/30

#
Anybody there to save my day today??
plsFast plsFast

2019/3/30

#
u could check if it hits the wall and if thats true move back
yalamberingnam yalamberingnam

2019/3/30

#
Yes, I have tried that too. But it passes through the wall ultimately
yalamberingnam yalamberingnam

2019/3/30

#
you mean if(isTouching(wall.class)){ move(-4) }
yalamberingnam yalamberingnam

2019/3/30

#
plsFast wrote...
u could check if it hits the wall and if thats true move back
yalamberingnam wrote...
you mean if(isTouching(wall.class)){ move(-4) }
plsFast plsFast

2019/3/30

#
what happends if u write that?
yalamberingnam yalamberingnam

2019/3/30

#
plsFast wrote...
what happends if u write that?
somtimes, it doesn't pass through the wall but somtimes it does.
yalamberingnam yalamberingnam

2019/3/30

#
yalamberingnam wrote...
plsFast wrote...
what happends if u write that?
somtimes, it doesn't pass through the wall but somtimes it does.
sometimes*
yalamberingnam yalamberingnam

2019/3/30

#
It is due to the randomly moving and turning behaviour of the actor bom
yalamberingnam yalamberingnam

2019/3/30

#
}
 
    public void checkForWall(){
        if(isTouching(wall1.class)){
            move(-4); 
        
        }
    
    
    
    }
    
yalamberingnam yalamberingnam

2019/3/30

#
yalamberingnam wrote...
}
 
    public void checkForWall(){
        if(isTouching(wall1.class)){
            move(-4); 
        
        }
    
    
    
    }
    
There are more replies on the next page.
1
2
3