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

2013/5/18

up and down help

FCG FCG

2013/5/18

#
How can make an object move and down, but when the object is moving up it should be slower then when its coming down. (coming down should be faster)
danpost danpost

2013/5/18

#
Do you want a constant rate, like a speed of 1 going up and a speed of 2 coming down? or, are you wanting to add gravity to your actor and it should be slowing down while going up and speeding up while coming down?
FCG FCG

2013/5/18

#
danpost wrote...
Do you want a constant rate, like a speed of 1 going up and a speed of 2 coming down? or, are you wanting to add gravity to your actor and it should be slowing down while going up and speeding up while coming down?
I constant speed will be fine.
danpost danpost

2013/5/18

#
What code do you have for the vertical movement of this actor so far? (you may want to just post the whole class)
FCG FCG

2013/5/18

#
danpost wrote...
What code do you have for the vertical movement of this actor so far? (you may want to just post the whole class)
There is nothing In the class. I want the object to move between two points
danpost danpost

2013/5/18

#
Oh, ok.
// with these instance fields 
private int hiY; // the higher y-limit
private int, loY; // the lower y-limit
private int speed; // the rate of movement

// use this in act method
if (speed == -1 && getY()<=hiY) speed = 2;
if (speed == 2 && getY()>=loY) speed = -1;
setLocation(getX(), getY()+speed;
FCG FCG

2013/5/18

#
danpost wrote...
Oh, ok.
// with these instance fields 
private int hiY; // the higher y-limit
private int, loY; // the lower y-limit
private int speed; // the rate of movement

// use this in act method
if (speed == -1 && getY()<=hiY) speed = 2;
if (speed == 2 && getY()>=loY) speed = -1;
setLocation(getX(), getY()+speed;
the code didn't work. My object didn't move at all.
danpost danpost

2013/5/18

#
Let me be a little more clear as to where these codes go, which first off is in the class of the object that is moving up slowly and down fast between two points (I will call the class 'Platform' below). The class should look something like this:
import greenfoot.*;

public class Platform extends Actor
{
    private int hiY;
    private int loY;
    private int speed;

    public Platform(int hiLoc, int loLoc)
    {
        hiY = hiLoc;
        loY = loLoc;
        speed = -1;
        // code constructing the platform objects if needed
    }

    public void act()
    {
        if (speed == -1 && getY() <= hiY) speed = 2;
        if (speed == 2 && getY() >=loY) speed = -1;
        setLocation(getX() getY()+speed);
    }
}
FCG FCG

2013/5/18

#
danpost wrote...
Let me be a little more clear as to where these codes go, which first off is in the class of the object that is moving up slowly and down fast between two points (I will call the class 'Platform' below). The class should look something like this:
import greenfoot.*;

public class Platform extends Actor
{
    private int hiY;
    private int loY;
    private int speed;

    public Platform(int hiLoc, int loLoc)
    {
        hiY = hiLoc;
        loY = loLoc;
        speed = -1;
        // code constructing the platform objects if needed
    }

    public void act()
    {
        if (speed == -1 && getY() <= hiY) speed = 2;
        if (speed == 2 && getY() >=loY) speed = -1;
        setLocation(getX() getY()+speed);
    }
}
Where would I put the high y value and the low y value.
danpost danpost

2013/5/18

#
You should be creating objects for the world in the world constructor or a 'prepare' method that it calls. You would use something like 'new Platform(100, 300)' to create a Platform object that will move vertically between the two values given.
You need to login to post a reply.