Need help adding a border so my guy doesn't go off screen

My code

class player{
    int x,y,spd,size;
    boolean[] keys=new boolean[5];
    player(int x1,int y1,int spd1,int size1){
    x=x1;
    y=y1;
    spd=spd1;
    size=size1;
    }
    
    
    void update(){
      if(keys[1]==true){
      y=y-spd;
      }
      if(keys[2]==true){
      x=x-spd;
      }
      if(keys[3]==true){
      y=y+spd;
      }
      if(keys[4]==true){
      x=x+spd;
      }
    rect(x,y,size,size);
    }
    
    
    void keyp(){
    if(key=='w'){
    keys[1]=true;
    }
    if(key=='a'){
    keys[2]=true;
    }
    if(key=='s'){
    keys[3]=true;
    }
    if(key=='d'){
    keys[4]=true;
    }
    }
    void keyr(){
    if(key=='w'){
    keys[1]=false;
    }
    if(key=='a'){
    keys[2]=false;
    }
    if(key=='s'){
    keys[3]=false;
    }
    if(key=='d'){
    keys[4]=false;
    }
    }
    }

player player;
void settings(){
size(800,800);
player = new player(400,400,3,50);
}

void draw(){
background(255);
player.update();
}

void keyPressed(){
player.keyp();
}

void keyReleased(){
player.keyr();
}

You can just check if

if(x<=10) x=10;

etc.:

if(x>width-10) ....

if(y<10) ....

if(y>height-10) ...

Chrisir

1 Like

x isn’t a variable (atleast that’s what it sais whenever I try to do so)[EDIT]
it works now frikkin hell

1 Like

x should be known within the class

So the function should work inside the class

But I understand you solved it.

1 Like