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

2021/2/17

problem in move and down and left and right

Guru1998 Guru1998

2021/2/17

#
in game, my player move up and down but it return to it start position public class Singh extends Actor { public void act() { checkKeyPress(); move(2); } /** * Check whether keyboard key press nd react if it has. */ public void checkKeyPress () { setLocation(248,237); if(Greenfoot.isKeyDown("W")) { setLocation(getX(),getY()+80); turn(2); } if(Greenfoot.isKeyDown("A")) { setLocation(getX(),getY()-80); } if(Greenfoot.isKeyDown("S")) { setLocation(getX()+80,getY()); } if(Greenfoot.isKeyDown("D")) { setLocation(getX()-80,getY()); } } }
danpost danpost

2021/2/17

#
Remove first setLocation command in checkKeyPress method.
Guru1998 Guru1998

2021/2/17

#
how i can keep actor in center can any help?code given above
danpost danpost

2021/2/17

#
Guru1998 wrote...
how i can keep actor in center can any help?code given above
Are you wanting the actor to appear moving while staying center screen? ... that is, a scrolling world?
Guru1998 Guru1998

2021/2/17

#
in my secanrio, when i run game my player goes in the corner of world. just need a solution to keep inside world.
Guru1998 Guru1998

2021/2/17

#
And when i hit the right key to move forward its goes forward and return to started position.
danpost danpost

2021/2/18

#
Guru1998 wrote...
in my secanrio, when i run game my player goes in the corner of world. just need a solution to keep inside world.
private int lastX;

private void checkKeyPress()
{
    lastX = Math.min(lastX, getX());
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("W")) dy--;
    if (Greenfoot.isKeyDown("S")) dy++;
    if (Greenfoot.isKeyDown("A")) dx--;
    if (Greenfoot.isKeyDown("D")) dx++;
    setLocation(getX()+80*dx, getY()+80*dy);
    int imgW = getImage().getWidth();
    int worldW = getWorld().getWidth();
    if (getX() < imgW/2) setLocation(imgW/2, getY());
    if (getX() > worldW-imgW/2) setLocation(worldW-imgW/2, getY());
    int imgH = getImage().getHeight();
    int worldH = getWorld().getHeight();
    if (getY() < imgH/2) setLocation(getX(), imgH/2);
    if (getY() > worldH-imgH/2) setLocation(getX(), worldH-imgH/2);
    if (lastX < getX() && dx == 0) setLocation(getX()-80, getY());
}
This may or may not be exactly what you want. However, some of it may put you on the right track.
Guru1998 Guru1998

2021/2/18

#
thank u for the help..that would help me
You need to login to post a reply.