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

2018/3/14

How do I get an actor to randomly move along a set point on the X axis but move on the Y?

itssemu itssemu

2018/3/14

#
This is what I have but it doesnt really work because all of the movements are really fast I want the actor to make random movements across a 400 pixel wide Y axis without changing the X axis variable, is this possible?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private GreenfootImage Change = new GreenfootImage("Transparent Jordan mouth opened.png");
    /**
     * Act - do whatever the Jordan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public boolean firing2 = MyWorld.firing;
    public int ranTurn = 0;
    public String[] pM = {"+","-"};
    public void act()
    {
        if(JumpPortal.level2)
        {
            getWorld().addObject(this, 200, 575);
            ranTurn++;
            if(ranTurn <= Greenfoot.getRandomNumber(80))
            {
                setLocation(getX(), getY() - 3);
            }
            if(ranTurn > Greenfoot.getRandomNumber(80) + 80)
            {
                setLocation(getX(), getY() + 3);
            }
            else if( ranTurn > 50)
            {
                ranTurn = 0;
            }
            else if(getY() == 1)
            {
                setLocation(getX(), getY() + 3);
            }
            else if(getY() == 399)
            {
                setLocation(getX(), getY() - 3);
            }
 
        }   
 
    }
}
danpost danpost

2018/3/14

#
Please explain what each part of the given code is to do as far as the 'if's and 'else if's. No. Better yet, explain in detail how you want your actor to randomly move along the y-axis. Break it down into discrete steps.
itssemu itssemu

2018/3/14

#
that code up there is just an example of what I want to do so you can ignore that code, I'm just wondering if you can set a random y variable that makes the actor class go to that and then stops once it reaches that point.
danpost danpost

2018/3/15

#
itssemu wrote...
I'm just wondering if you can set a random y variable that makes the actor class go to that and then stops once it reaches that point.
Putting that in terms of what to do instead of what not to do ("stops" is not something you can program an actor to do), what you want for the actor to do is "if not at target y-coordinate, move toward target y-coordinate". To have the actor "move" to a random y-coordinate location after being put in the world, you can have a field to hold the random y-coordinate value and make use of the 'addedToWorld(World)' method to set the field to some value. You could add an extra field to hold the direction of movement once the y-coordinate point is established:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// fields
int targetY; // to hold a random y-coordinate
int dy; // to hold direction to move
 
// overriding methods
protected void addedToWorld(World world)
{
    targetY = Greenfoot.getRandomNumber(world.getHeight()); // choose random target y-coordinate
    dy = (int)Math.signum(targetY-getY()); // determine direction to move
}
 
public void act()
{
    if (getY() != targetY) setLocation(getX(), getY()+dy); // "if not at target y-coordinate, move toward target y-coordinate"
}
itssemu itssemu

2018/3/15

#
thank you so much for that, but it only moves once and I would like it to move an infinite amount of time.
Yehuda Yehuda

2018/3/15

#
So reset targetY and then travel there if the Actor reached it's previous target (getY() == targetY), continuously througout the game.
danpost danpost

2018/3/15

#
You said this:
itssemu wrote...
I'm just wondering if you can set a random y variable that makes the actor class go to that and then stops once it reaches that point.
That is what you got. Now you want "move toward target and, if ending up at target location, select new target".
Yehuda Yehuda

2018/3/16

#
What you want(ed) is the solution to a new problem.
itssemu itssemu

2018/3/16

#
thankyou!
You need to login to post a reply.