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

2020/1/1

How do I make an actor "fall" where there is a line?

pio22mibi pio22mibi

2020/1/1

#
The game I am coding consists of "digging" holes into the sand which I do by repeatedly adding the actor Drawinghole to form a line or a path. How to I make my actor (a ball called "Jaune") fall according to the path and not fall when it no longer touches it? This is my code but it does not seem to work:
private final int GRAVITY = 1;
    private int velocity;
    public jaune (){
         velocity = 0;
    }
    public void act() 
    {
            if(oktomove()){
            fall();
          }
        
            Actor touche2 = getOneIntersectingObject(rouge1.class); 
            if(touche2 != null) Greenfoot.setWorld(new​​ Niveau2());
        
          
    } 
    public void fall(){
       
            setLocation(getX() + velocity, getY() + velocity);
            Actor touche2 = getOneIntersectingObject(Drawinghole.class); 
            if(touche2 == null) velocity = 0;
            else  velocity += GRAVITY;
        
    }
    public boolean oktomove()
    {
        boolean movingisok = false;
        
        Actor touche = getOneIntersectingObject(Peg.class);  
        if(touche != null) movingisok = false; 
        
        int imageWidth = getImage().getWidth();  
        int imageHeight = getImage().getHeight(); 
        if (getOneObjectAtOffset(imageWidth / 2, imageHeight , Drawinghole.class) != (null)) movingisok = true;
        
        return movingisok;   
    }
danpost danpost

2020/1/1

#
It seems quite strange to use the same velocity value for moving AND falling (probably should use two distinct fields for (1) horizontal and (2) vertical velocity).
pio22mibi pio22mibi

2020/1/2

#
Yes I will change that right away. Thanks but any thoughts on the whole code itself because it does not work.
danpost danpost

2020/1/2

#
pio22mibi wrote...
any thoughts on the whole code itself because it does not work.
It would be difficult to give any advice without knowing exactly what you are dealing with. A detailed explanation of how you want the ball to behave would be a start. However, usually, a move would be made before checking that it is okay and retracted if found not to be.
You need to login to post a reply.