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

2015/10/30

Help with objects moving continuously in the y axis?

pleasehelpme pleasehelpme

2015/10/30

#
how do i make an actor move up and down continuously?
danpost danpost

2015/10/30

#
The combination of direction and location is enough to control an actor moving back and forth. If either limit is reached while approaching it, change directions.
JoaoV10 JoaoV10

2015/11/15

#
I have the same questions please can put an example of code?
Super_Hippo Super_Hippo

2015/11/15

#
private int topBorder = 100, bottomBorder = 200, direction = 1;

public void act()
{
    setLocation(getX(), getY()+direction);
    if (getY()==topBorder || getY()==bottomBorder) direction *= -1;
}
Make sure the object is spawned between the borders.
danpost danpost

2015/11/15

#
If you have a 'direction' field that holds a value of '1' or '-1' which can be multiplied by the speed of the actor, then the conditions for changing direction can be:
setLocation(getX(), getY()+speed*direction)
if (direction *(range/2-getY()) > range/2) direction = -direction;
where range is the distance between the two endpoints along its direction of movement (the turn-around points). The 'direction' field must be an instance field (declared outside the method) and the 'speed' variable can be a hard-coded value or it can be an instance field as well.
JoaoV10 JoaoV10

2015/11/15

#
I want to for example, that one actor Pedestrian stay continuously moving back and forth in the Y axis 100 for the Y axis 600 Show me this code please
danpost danpost

2015/11/15

#
I messed up with the 'if' condition. It should adjust the 'getX()' value by subtracting the minimum value of the range (or, 'direction' should be multiplied by the difference between the midpoint of the range and the current coordinate of the actor). For the given values:
if (direction*(350-(getY()) > 250) direction = -direction;
JoaoV10 JoaoV10

2015/11/15

#
Sorry, I'm through with bad English language If only put the complete code so you can do what I said I would be very grateful
danpost danpost

2015/11/15

#
JoaoV10 wrote...
Sorry, I'm through with bad English language If only put the complete code so you can do what I said I would be very grateful
I do not know what more you may want. No one on this site will create your project for you. I gave you all the information you might need on this particular issue. I do not think any more can be said about it.
JoaoV10 JoaoV10

2015/11/15

#
Sorry, my project is already ready, just missing the object moving continuously from one point to another and not sure gave that role he spent there, but still Thank you
Super_Hippo Super_Hippo

2015/11/15

#
Did you try any code we gave you?
JoaoV10 JoaoV10

2015/11/16

#
Super_Hippo wrote...
Did you try any code we gave you?
Yes, I tried, but the object is not constantly moving in a coordinated to another
danpost danpost

2015/11/16

#
JoaoV10 wrote...
Yes, I tried, but the object is not constantly moving in a coordinated to another
Why don't you show what you tried (if you want us to help)? Showing the entire class helps to make things go a lot quicker in helping, also.
JoaoV10 JoaoV10

2015/11/16

#
danpost wrote...
JoaoV10 wrote...
Yes, I tried, but the object is not constantly moving in a coordinated to another
Why don't you show what you tried (if you want us to help)? Showing the entire class helps to make things go a lot quicker in helping, also.
I did so: setRotation(90) move(4) if(isTouching(Obs1.class)) move(-4) My intention is that when you hit the Obs4 (which is an obstacle created), the pedestrian change the direction coming back, and then when you hit another obstacle back again, thus making a path still
You need to login to post a reply.