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

2020/5/28

moving side to side

Topben107 Topben107

2020/5/28

#
I am not sure how to get my character to only move side to side and not turn is this possible? Also, can an object be deflected off of another object?
Kostya20052011 Kostya20052011

2020/5/28

#
For movement, you can use: int x; int y; y=getY(); if(Greenfoot.isKeyDown(right)){ x=x+10; setLocation(x,y); } if(Greenfoot.isKeyDown(left)){ x=x-10; setLocation(x,y); }
Kostya20052011 Kostya20052011

2020/5/28

#
Also, can I reject an object from another object? - you can use the following code to do this: Actor obj=getOneIntersectingObject(Object.class); if(obj!=null && Greenfoot.isKeyDown(left)){ move(10); } if(obj!=null && Greenfoot.isKeyDown(rigth)){ move(-10); }
Kostya20052011 Kostya20052011

2020/5/28

#
As I understand it, if something is wrong, write to me and I will redo it.
danpost danpost

2020/5/28

#
With fields:
private int speed = 5; // change value to fit
private int direction = 1; // '1' is right; '-1' is left
you can use:
move(speed*direction);
to move; and:
if (isTouching(Actor.class))
{
    direction = -direction;
    move(speed*direction);
}
to deflect off other actors.
You need to login to post a reply.