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

2015/4/8

how to make an object move a specific distance when the right or left key button is pressed

dyl_fisher dyl_fisher

2015/4/8

#
I need to make an object move a specific distance when the right or left key button is pressed. So when i push the right arrow key i need the object to move say 10 spaces, and only 10 spaces . so every time i push right it will only move 10 spaces.
danpost danpost

2015/4/8

#
dyl_fisher wrote...
I need to make an object move a specific distance when the right or left key button is pressed. So when i push the right arrow key i need the object to move say 10 spaces, and only 10 spaces . so every time i push right it will only move 10 spaces.
What code are you using? do you want it to jump 10 spaces or move 10 spaces -- one at a time?
dyl_fisher dyl_fisher

2015/4/8

#
Move 10 spaces, so if i press the right arrow key and hols it down it will only move 10 spaces
danpost danpost

2015/4/8

#
What I was asking is if you wanted the actor to smoothly move across to the location that is 10 spaces away or just immediately appear 10 spaces away.
dyl_fisher dyl_fisher

2015/4/8

#
Smoothly move across
danpost danpost

2015/4/9

#
Okay. You will need two int fields: one to signify the direction of movement and the other to count the steps taken in the movement. When both keys are up, reset the counters. If no direction is set and a key is found down, set the direction field. If the count is not ten and the direction field is not zero, move once in direction and increment the counter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// instance fields
private int stepCount;
private int direction;
 
// in act or method it calls for moving
boolean left = Greenfoot.isKeyDown("left");
boolean right = Greenfoot.isKeyDown("right");
if (direction == 0) // accepting direction
{
    if ( left && ! right ) direction == -1;
    if ( right && ! left ) direction == 1;
}
if (direction != 0 && stepCount != 10) // moving
{
    setLocation(getX()+direction, getY());
    stepCount++;
}
if (stepCount == 10 && ( ! left && ! right ) ) // resetting
{
    direction = 0;
    stepCount = 0;
}
dyl_fisher dyl_fisher

2015/4/9

#
Perfect that works well ! Is there a way to make the object move faster though ???
Super_Hippo Super_Hippo

2015/4/9

#
You can change line 10 and 11, so that there is for example a 2 instead of a 1. Make sure you adjust line 13 and 18 then. In this case, change both 10 to 5. This is because you only need 5 times 2 steps to reach the same destination as 10 times 1 step.
dyl_fisher dyl_fisher

2015/4/9

#
Great that works.... Now how do you do the same thing for vertical movement .. i tried and it doesn't work
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
private int stepCountUp;
        private int directionUp;
        public void Run()
            {
                boolean up = Greenfoot.isKeyDown("up");
                boolean down = Greenfoot.isKeyDown("down");
             
            if (directionUp != 0 && stepCountUp != 2)
                {
                    if ( up && ! down ) directionUp = -100;
                    if ( down && ! up ) directionUp = 100;
                }
            if (directionUp != 0 && stepCountUp != 2) // moving
                {
                    setLocation(getX(), getY()+directionUp);
                    stepCountUp++;
                }  
            if (stepCountUp == 2 && ( ! up && ! down ) ) // resetting
                {
                    directionUp = 0;
                    stepCountUp = 0;
                }
               
                 
            }
danpost danpost

2015/4/9

#
Just saying it does not work is not informative. As written, however, your actor will move 200 pixels either up or down in two act cycles (it will move 100 pixels each act cycle for 2 cycles). You probably want it to move the same speed as horizontally, or two pixels per act cycle for whatever distance total divided by two act cycles.
dyl_fisher dyl_fisher

2015/4/9

#
Nothing happens when i use this code for the up and down arrow keys ... but yes i would like it to move the same as the horizontal, just vertically ..
danpost danpost

2015/4/9

#
dyl_fisher wrote...
Nothing happens when i use this code for the up and down arrow keys ... but yes i would like it to move the same as the horizontal, just vertically ..
Then change '2's to '5's and '100's to '2's.
dyl_fisher dyl_fisher

2015/4/9

#
Thanks So much danpost you have really been a help an i appreciate it so much
danpost danpost

2015/4/9

#
There is still the problem, however, that you could have both horizontal and vertical movements going at the same time because you kept the codes for each separated from the other. I mean that you could end up with movement, for example, moving up 4, then moving diagonally to the right 6 and then moving the final 4 to the right (which would then be 10 up and 10 to the right).
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
// instance fields
private int hDir; // horizontal direction field
private int vDir; // vertical direction field
private int stepCount; // the step counter
 
// in act or method it calls
boolean left = Greenfoot.isKeyDown("left");
boolean right = Greenfoot.isKeyDown("right");
boolean up = Greenfoot.isKeyDown("up");
boolean down = Greenfoot.isKeyDown("down");
if ( hDir == 0 && vDir == 0 ) // accepting direction
{
    int dx = 0;
    int dy = 0;
    if (left) dx--;
    if (right) dx++;
    if (up) dy--;
    if (down) dy++;
    if ( dx * dy == 0 && dx + dy != 0 ) // discernible direction
    {
        hDir = dx;
        vdir = dy;
    }
}
if ( ( hDir != 0 || vDir != 0 ) && stepCount != 5 ) // moving
{
    setLocation(getX()+hDir*2, getY()+vDir*2);
    stepCount++;
}
if (stepCount == 5 && ( ! left && ! right  && ! up && ! down ) ) // resetting
{
    hDir = 0;
    vDir = 0;
    stepCount = 0;
}
I have not tested it; but, it should be close (if not good).
You need to login to post a reply.