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

2013/10/31

How to make an actor continuously moving back and forth horizontally?

1
2
strangers1412 strangers1412

2013/10/31

#
Hello, I need some help. I want to make an actor that can move back and forth, without turning any direction or make any angle rotation, just moving forward and reverse. As I cannot find any tutorial for this type of movement. this are my currently code. as the problem is, when the object is moving forward until it arrive at the edge,it will continuously reverse one step only but then move forward and thus make a looping there, so, how to make the object goes from getX() == 0 to getX() == 600 and vice versa. using forwad and reverse movement only?
1
2
3
4
5
move();
        
        if (atWorldEdge()) {
           reverse(5);
        }
The move() method for actor allows you to move backwards without changing angle. You use move(+num) to move forward, and move(-num) to move backwards.
strangers1412 strangers1412

2013/10/31

#
ok, but how to make it moving back and forth? is this correct? if (!atWorldEdge()) {move(+1);} else (atWorldEdge()) {move(-1);} but this will make it not move right?
danpost danpost

2013/10/31

#
You will need to keep the direction in an instance field and perform a double conditional check to reverse direction. Using an int field is the easiest since you can set it to '1' and negate its value to reverse directions and use its value as a factor in the speed of the movement.
1
2
3
4
5
6
// instance field
int direction = 1;
// checking edges
if ((direction == -1 && getX() == 0) || (direction == 1 && getX() == getWorld().getWidth()-1)) direction = -direction;
// movement
move(direction*speed);
Speed can be hard coded or it can also be held in an instance field.
strangers1412 strangers1412

2013/11/6

#
Thanks for helping, but my elephant keep on banging the wall, reverse only 1 pixel then move forward 1 pixel and looping like that. sorry, i am so new in this. i try many times already. i just want my object to move like this... forward from here to here (O>) ---------------------------------------------------------------------------------------------> (O>) and then reverse tohere from here (O>) <----------------------------------------------------------------------------------------- (O>)
danpost danpost

2013/11/6

#
Please show the code you are currently trying for this (the more code you supply, the easier it will be to fix).
strangers1412 strangers1412

2013/11/6

#
int direction=1; if ( getX() == 0 && direction == 1) move(direction); else if (getX() >0 || getX()<599 && direction == 1) move (direction); else if (getX()== 600 && direction == 1) move(-direction); else if (getX() <600 || getX()>0 && direction == -1) move(-direction);
danpost danpost

2013/11/6

#
You are checking to see if the value of 'direction' is positive one (1) and negative one (-1); yet, nowhere are you actually changing its value from positive one (1). First, check to see if the value of 'direction' needs changed (changing it if needed), then execute the moving.
strangers1412 strangers1412

2013/11/7

#
thank you so much for your help.......i try to change a little...now it work.... i change your code from direction == -1 to direction > 0
1
2
3
4
5
6
7
8
9
10
11
12
13
private int direction = 5
    public void act()
    {
        move();
    }
 
private void move()
  {
        // checking edges 
        if ((direction < 0 && getX() == 0) || (direction > 0 && getX() == getWorld().getWidth()-1)) direction = -direction; 
        // movement 
        move(direction);
    }
thanks for everything. i need too study more.
ckdowney ckdowney

2014/11/28

#
Hello, I'm working on something very similar. move back and forth just like that except it move at different integers back and forth building up longer distances... like for example starting at 0, 1,-1,2,-2,3,-3 ect or also like 0,1,-2,4,-8,16 ect.
Mickey09 Mickey09

2014/11/28

#
Isn't there a way to use the while or looping code to keep the act method going over and over.
1
2
3
4
5
6
7
8
9
10
11
12
int a = 1
..
..
..
  public void act()
    {
       while (a>=0)
       {
       move(a);
        }
         
    }
Then within the loop code, make a line of code that adds one to a and for the other algorithm, multiply a by 2. However, not sure how to set that coding up. A a = a + 1 A a = a * 2 Also, to comment about the coding above, it will not work for the idea that we are trying to do, downey, because the actor will move to the end of the world, then walk to the other side. We need to tweak the code to only move 1, the set a code to add one to the variable, and then move the same variable and do a forever loop.
Super_Hippo Super_Hippo

2014/11/28

#
I don't think that while loop makes sense. This should work I think.
1
2
3
4
5
6
7
8
9
private int a=0, b=0;
 
public void act()
{
    if (b==0) b=1;
    else b=0;
    a=a*(-1)+b;
    move(a);
}
danpost danpost

2014/11/28

#
Mickey09 wrote...
Isn't there a way to use the while or looping code to keep the act method going over and over.
If you use a while loop inside the act method, it, the while loop, will execute to completion in one act frame (or in about 1/60 of a second) and only the end result will be displayed on screen. The act method is already being called repeatedly while your scenario is running, in loop fashion. Only put code that describes what the actor should do at any instance in the act method. You can use an if statement to determine when to turn around and increase speed.(or for whatever does not happen constantly).
Mickey09 Mickey09

2014/11/28

#
Oh duh! My apologies, I am still new to the Greenfoot atmosphere. Thanks!
Mickey09 Mickey09

2014/11/30

#
Super_Hippo wrote...
I don't think that while loop makes sense. This should work I think.
1
2
3
4
5
6
7
8
9
private int a=0, b=0;
 
public void act()
{
    if (b==0) b=1;
    else b=0;
    a=a*(-1)+b;
    move(a);
}
So, I appreciate the help, but I input this code and my actor "freaks" out. It moves back and forth but super fast, but never really leaves the one spot.... Hehe!
There are more replies on the next page.
1
2