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

2015/11/21

Problem with moving object

USE1ESS USE1ESS

2015/11/21

#
I want an object to move up and down the whole time (touch ground -> go up, touch ceiling -> go down) but its only doing it 4 times and then the object gets stuck into the ground and wont move up somehow :/ would be nice if someone could help! :D
import greenfoot.*;


public class upDownObject extends Actor
{
    private int down = 5;   //down-moving speed
    
    private int up = -3;  //up-moving speed
    
    private int falling = 1;
    
   
    
    public void act() 
    {
       checkIfTouching(); //check if he is touching the ground cause if he does he should move up
       
       checkIfDown();       //check if he can move down
       
       checkeIfUp();          //check if he can move up
       
    }    
    
    
                public void moveDown()
                {
                    setLocation(getX(), getY() +down);  // +5
                }
                public void moveUp()
                {
                    setLocation(getX(), getY() +up);  // +-3
                }

    public void checkIfDown()   //check if he can move down
    {
        if (falling == 1)
        {
            moveDown();
        }
        
    } 
    public void checkIfUp()   //check if he can move up
    {if (falling == 0)
        {
           moveUp();
        }

public void checkIfTouching()
    {
        if(isTouching(ground.class))
        {
            falling--; // if he's touching the ground he will go to checkIfUp-method and move up
        }
        if(isTouching(ceiling.class))
        {
            falling++; //if he's touching the ceiling he will go to checkIfDown-method and move down
        }
    }
    }
danpost danpost

2015/11/21

#
I bet if you stop the scenario and inspect the upDownObject object (right click on it and select 'Inspect'), you will find that the 'falling' field does not contain either a '1' or a '0' value. The main reason for this is that you do not take into account the direction of movement when touching either the 'ground' or 'ceiling' objects. Now, if the speed in both the up and down directions were the same, then it would not have a chance to behave this way (getting stuck on the ground or ceiling). However, since your speed downward is greater than the speed upward, you can move into the ground further than you can get out in one act cycle. This means you will still be in the ground and the falling field will be decreased again (to '-1'). Then, you will not move (the conditions to move requires that the 'falling' field hold a '1' or a '0' value) and the field will be decreased again and again. There are two ways to prevent this. One way is to check the value of 'falling' after changing it and if it is overly increased or decreased, change it back to the last valid value. The other way is to check the value before changing it. Either way the value of the field needs to be assessed and maintained at valid values.
USE1ESS USE1ESS

2015/11/21

#
Thanks for the explanation , danpost! My changed checkIfTouching method :
public void checkIfTouching()
    {
        if(isTouching(ground.class))
        {
            falling = 0;  //changed from "falling--;" to "falling = 0;"
        }
        if(isTouching(ceiling.class))
        {
            falling = 1; //changed from "falling++;" to "falling = 1;"
        }
    }
Ps: Everything works now how it should work :)
You need to login to post a reply.