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

2016/1/18

How to change my (horizontal-positioned plane) into vertical direction

codedroid codedroid

2016/1/18

#
Hi, i'm new to greenfoot. I'm ask for your help about how to change my horizontal-positioned plane into vertical direction without rotate it. So, when the player press 'A' or 'D' on keyboard, it mill move forward and backward (i've already make that work). And when the player press 'W' or 'S' it will moving up and down,but i want it work without changing the rotation. Here's my code for moving forward and backward :
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
public void act()
    {
    
       // This isn't work like what i want
       if(Greenfoot.isKeyDown("w"))
       {
           move(-4);
       }
        
       // This isn't work like what i want too
       if(Greenfoot.isKeyDown("s"))
       {
           move(4);
       }
        
       //Worked
       if(Greenfoot.isKeyDown("a"))
       {          
move(Greenfoot.getRandomNumber(10)*-1);
       }
        
       //Worked
       if(Greenfoot.isKeyDown("d"))
       {
           move(Greenfoot.getRandomNumber(10));
       }
    }
I need your help and guide. Thanks before.
davmac davmac

2016/1/18

#
There are basically two ways to do this. The first way is to rotate, and then rotate back after moving (the rotation will not be visible, since the screen is not repainted while the act() method is running). For example:
1
2
3
4
5
6
if(Greenfoot.isKeyDown("w"))
{
    rotate(90);
    move(-4);
    rotate(-90);
}
The other way is to use the setLocation method, together with getX() and getY(), to re-position the actor at an offset from its current location.
danpost danpost

2016/1/18

#
Lines 3 and 5 above should have method names of 'turn', not 'rotate'. As an alternative, lines 3 and 5 could be coded using the 'setRotation' method using value of '90' and '0' respectively.
davmac davmac

2016/1/18

#
danpost wrote...
Lines 3 and 5 above should have method names of 'turn', not 'rotate'
Absolutely correct, thanks. It should be:
1
2
3
4
5
6
if(Greenfoot.isKeyDown("w"))
{
    turn(90);
    move(-4);
    turn(-90);
}
codedroid codedroid

2016/1/19

#
Thanks @davmac and @danpost for your collaboration for helping me! It's all working :D
danpost wrote...
Lines 3 and 5 above should have method names of 'turn', not 'rotate'. As an alternative, lines 3 and 5 could be coded using the 'setRotation' method using value of '90' and '0' respectively.
davmac wrote...
danpost wrote...
Lines 3 and 5 above should have method names of 'turn', not 'rotate'
Absolutely correct, thanks. It should be:
1
2
3
4
5
6
if(Greenfoot.isKeyDown("w"))
{
    turn(90);
    move(-4);
    turn(-90);
}
You need to login to post a reply.