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

2017/1/11

Rotating object image and moving

nat12 nat12

2017/1/11

#
So I wanted to my move object with arrow keys(UP,DOWN,LEFT,RIGHT). I've done that. But now I'm trying to move it also UP & RIGHT, UP & LEFT, DOWN & RIGHT and DOWN & Left. Of course by pressing the 2 corresponding arrows at the same time. However, my code so far will only rotate the object's image in two of those directions (DOWN RIGHT, DOWN LEFT).
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public void playerMove()
{
    int dx=10, dy=10;
    if(Greenfoot.isKeyDown("up")) dy--;
    if(Greenfoot.isKeyDown("down")) dy++;
    if(Greenfoot.isKeyDown("right")) dx++;
    if(Greenfoot.isKeyDown("left")) dx--;
    if (dx<10 && dy<0) // left up
    {
    setRotation(225);
    move(speed);
    }
    else
    if (dy<10) //up
    {
        setRotation(270);
        move(speed);
    }
 
    if (dx>10 && dy<10) // right up
    {
        setRotation(315);
        move(speed);
    }
    else
    if (dy>10) //down
    {
        setRotation(90);
        move(speed);
    }
 
    if (dx>10 && dy>10)// right down
    {
    setRotation(45);
    move(speed);
    }
    else
    if (dx>10) //right
    {
        setRotation(0);
        move(speed);
    }
 
    if (dx<10 && dy>10) //left down
    {
    setRotation(135);
    move(speed);
    }
    else
    if (dx<10) //left
    {
        setRotation(180);
        move(speed);
    }
I noticed that when I move around the if statement that controls the two arrow keys, the image will either rotate or won't rotate in that certain directions. My brain is just out of ideas how to fix this and I gave up, so I came here for help. The image which I rotate is the default Greenfoot rocket ship.
Super_Hippo Super_Hippo

2017/1/11

#
Not tested, but try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(Greenfoot.isKeyDown("up")) dy--;
if(Greenfoot.isKeyDown("down")) dy++;
if(Greenfoot.isKeyDown("right")) dx++;
if(Greenfoot.isKeyDown("left")) dx--;
 
if (dx == 0 && dy == 0) return;
 
switch(dx)
{
    case -1:
    setRotation(180-45*dy);
    break;
     
    case 0:
    setRotation(180-90*dy);
    break;
 
    case 1:
    setRotation(45*dy);
    break;
}
move(speed);
nat12 nat12

2017/1/11

#
Unfortunately it doesn't work. The object just moves right as soon as I start the scenario.
Super_Hippo Super_Hippo

2017/1/11

#
Did you put the code into the act method or call it from there? I forgot... add this as line 1:
1
private int dx=0, dy=0;
nat12 nat12

2017/1/11

#
I just realised that you put your dx and dy as 0, mine were put as 10, I changed them to 0 and they work :) Thank you so much. I never used the switch statement so I had no idea how it worked.
Super_Hippo Super_Hippo

2017/1/11

#
(Without the "private" obviously) Glad that I could help :)
You need to login to post a reply.