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

#
yes doesn't work
plsFast plsFast

2019/3/30

#
does it move through walls when moving back? or also when moving normally?
plsFast plsFast

2019/3/30

#
it moves 2 at a time which means that if he hits the wall at the first move he wont stop only if the wall is there on the second move
Super_Hippo Super_Hippo

2019/3/30

#
Your order is wrong. Also, the speed of moving back should be the same as the normal move. --turn --move --check for wall ---->move back if wall detected
danpost danpost

2019/3/31

#
Line 78 does nothing for your actor. No matter where it is at, that line leaves it right there. It does not move it back to where it was before the act method started. That is, once you use move or setLocation on the actor, getX and getY will return its new location coordinates, not its original ones.
yalamberingnam yalamberingnam

2019/3/31

#
Super_Hippo wrote...
Your order is wrong. Also, the speed of moving back should be the same as the normal move. --turn --move --check for wall ---->move back if wall detected
plsFast wrote...
it moves 2 at a time which means that if he hits the wall at the first move he wont stop only if the wall is there on the second move
ok. Thank you it is much better now. But the randomness of the actor is decreased. Also rarely they pass through the wall.
yalamberingnam yalamberingnam

2019/3/31

#
danpost wrote...
Line 78 does nothing for your actor. No matter where it is at, that line leaves it right there. It does not move it back to where it was before the act method started. That is, once you use move or setLocation on the actor, getX and getY will return its new location coordinates, not its original ones.
Thank you. I wanna know more about the method getX() and getY(). Then it means it is not possible to use these method to not walk through wall or is there a way to increase or decrease the getX() and getY().
yalamberingnam yalamberingnam

2019/3/31

#
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() 
    {
       
       //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);
        move(4);
        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)){
            move(-4); 
        
        }
    
    
    
    }
    
    
}
This is the modify code. Now i will start my documentation, thank you all.
danpost danpost

2019/3/31

#
yalamberingnam wrote...
Thank you. I wanna know more about the method getX() and getY(). Then it means it is not possible to use these method to not walk through wall or is there a way to increase or decrease the getX() and getY().
There are two ways to have the actor return to its original location (where it was at the beginning of the act step). One is to locally save that location and the other is to save the change in coordinate value(s). As examples:
public void act()
{
    int x0 = getX(); // initial x-coordinate
    int y0 = getY(); // initial y-coordinate
    .... /** set a new location or move */
    if (/** not a good location */) setLocation(x0, y0);
}
// or
public void act()
{
    int ySpeed = 1; // change in y-coordinate
    ... /** setLocation(getX(), getY()+ySpeed); */
    if (/** not a good location */) setLocation(getX(), getY()-ySpeed);
}
yalamberingnam yalamberingnam

2019/3/31

#
Possible scenario; Let's say at first I have kept my Actor1 in position(100,100), and another Actor2 in (200.100). Now if I move my actor1 and it touches another actor i.e actor2 does it return to the positon (100,100) or positon near the actor2. I tried the above code, but the actor walks right through the actor.
danpost danpost

2019/3/31

#
yalamberingnam wrote...
I tried the above code, but the actor walks right through the actor.
Please show the exact codes you are trying.
yalamberingnam yalamberingnam

2019/4/1

#
public class Ant extends Actor
{
    /**
     * Act - do whatever the Ant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(2);

    int x0 = getX(); // initial x-coordinate
    int y0 = getY(); // initial y-coordinate
     /** set a new location or move */
    if (isTouching(Ciricle.class)){
        setLocation(x0, y0);
This is my code.
yalamberingnam yalamberingnam

2019/4/1

#
Circe is the obstacle that ant is not supposed to pass through
yalamberingnam yalamberingnam

2019/4/1

#
yalamberingnam wrote...
Circe is the obstacle that ant is not supposed to pass through
danpost danpost

2019/4/1

#
yalamberingnam wrote...
<< Code Omitted >> This is my code.
Line 13 indicates where line 9 should be. However, with the speed being constant, you do not need to save the original coordinates. The following act method should suffice:
public void act()
{
    move(2);
    if (isTouching(Ciricle.class)) move(-2);
}
There are more replies on the next page.
1
2
3