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

2023/12/13

help inverting a players movement keys

Lonelyf60 Lonelyf60

2023/12/13

#
so im programming a maze game for my game design class and when the player touches a portal the wasd keys are supposed to invert tbh i have no idea how tf im supposed to do this and any help would be appreciated
Lonelyf60 Lonelyf60

2023/12/13

#
edit!!!: i figured out how to invert the movement but now the player moves WAY too fast while inverted heres my code:public class main extends Actor { int speed=-2; /** * Act - do whatever the main wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { left(); right(); up(); down(); } public void left() { if (Greenfoot.isKeyDown("a")){ setLocation(getX()+speed,getY()); } } public void right() { if (Greenfoot.isKeyDown("d")){ setLocation(getX()-speed,getY()); } } public void up() { if (Greenfoot.isKeyDown("w")){ setLocation(getX(),getY()+speed); } } public void down() { if (Greenfoot.isKeyDown("s")){ setLocation(getX(),getY()-speed); } } } if (isTouching(funnyinvert.class)) { invert=invert+1; } } public void invert(){ if (invert==1||invert==3||invert==5){ speed=speed+4; } if (invert==2||invert==4){ speed=speed-4; } }
danpost danpost

2023/12/14

#
Lonelyf60 wrote...
i figured out how to invert the movement but now the player moves WAY too fast while inverted
Inverting should not change the speed -- it should change the direction:
import greenfoot.*;

public class main extends Actor
{
    int speed = 2;
    int dir = 1;
    
    public void act() {
        move();
        invert();
    }
    
    private void move() {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if (Greenfoot.isKeyDown("d")) dx++;
        if (Greenfoot.isKeyDown("w")) dy--;
        if (Greenfoot.isKeyDown("s")) dy++;
        setLocation(getX()+speed*dx*dir, getY()+speed*dy*dir);
    }
    
    private void invert() {
        if (isTouching(funnyinvert.class)) {
            removeTouching(funnyinvert.class);
            dir = -dir;
        }
    }
}
You need to login to post a reply.