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

**URL:** <https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987>\
**Category:** Coding Questions\
**Created:** [July 25, 2019, 11:08pm UTC](https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987 "2019-07-25T23:08:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjiiller](https://avatars.discourse-cdn.com/v4/letter/k/90db22/32.png) [@kjiiller](https://discourse.processing.org/u/kjiiller)\
**Post date:** [July 25, 2019, 11:08pm UTC](https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987/1 "2019-07-25T23:08:58Z")

</div>

## My code

```auto
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;
    }
    }
    }

```

* * *

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 25, 2019, 11:11pm UTC](https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987/2 "2019-07-25T23:11:50Z")

</div>

You can just check if

`if(x<=10) x=10;`

etc.:

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

if(y<10) ....

if(y>height-10) ...

```

Chrisir

---

<div class="post-metadata">

**Author:** ![kjiiller](https://avatars.discourse-cdn.com/v4/letter/k/90db22/32.png) [@kjiiller](https://discourse.processing.org/u/kjiiller)\
**Post date:** [July 26, 2019, 12:46am UTC](https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987/3 "2019-07-26T00:46:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 26, 2019, 11:56am UTC](https://discourse.processing.org/t/need-help-adding-a-border-so-my-guy-doesnt-go-off-screen/12987/4 "2019-07-26T11:56:26Z")

</div>

x should be known within the class

So the function should work inside the class

But I understand you solved it.
