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

2019/6/13

actor rotation with arrows

ronald ronald

2019/6/13

#
I would like to return actor yellow1 with arrows right and left im a beginner
Super_Hippo Super_Hippo

2019/6/13

#
public void act()
{
    int r=0;
    if (Greenfoot.isKeyDown("left")) r-=2;
    if (Greenfoot.isKeyDown("right")) r+=2;
    if (r != 0) turn(r);
}
ronald ronald

2019/6/14

#
thank you
ronald ronald

2019/6/15

#
int x = getX();
int y = getY();

if(Greenfoot.mouseClicked("right"))x=x+1;
if(Greenfoot.mouseClicked("left"))y=y+1;
setLocation(x,y);
I want to move actor with by mousse clicking with left and right
Super_Hippo Super_Hippo

2019/6/16

#
Get the MouseInfo with "Greenfoot.getMouseInfo()" and call the method "getButton" on that MouseInfo. 1 is the left button and 3 is the right one. You can find the API here: https://www.greenfoot.org/files/javadoc/
ronald ronald

2019/6/17

#
public void act ()
{
int button = 0;
int x = 0;
int y = 0;
{
MouseInfo mouseInfo = new Greenfoot.getMouseInfo();
if(mouseInfo!=null)
{
button = mouseInfo.getButton();
if(button == 1 || button == 3)
{
x = mouseInfo.getX();
y = mouseInfo.getY();
setLocation(x,y);
}
}
}
}
it' s no what I was looking for but it does no matter here for example the emoticone just follow the curseur if I don't sure That's the right formula what interests me is to click with the mouse on the emoticone that turns either left or right without following the cursor I thought that with mouseclicked, it would to the trick thank you for our help
danpost danpost

2019/6/17

#
You could try this:
int direction;

public void act()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse != null)
    {
        if (direction == 0 && Greenfoot.mousePressed(null)) direction = (mouse.getButton()-2)%2;
        else if (direction != 0 && Greenfoot.mouseClicked(null) && direction == (mouse.getButton()-2)%2) direction = 0;
        else if (direction != 0 && Greenfoot.mousePressed(null)) direction = -direction;
    }
    setLocation(getX()+direction, getY());
}
which is oh so close to what you want. It can probably be refined to work slightly better; but it really does get involved.
ronald ronald

2019/6/18

#
thank you it works as wanted
You need to login to post a reply.