# Restricting my game character's movement

**URL:** <https://forum.kirupa.com/t/restricting-my-game-characters-movement/312640>\
**Category:** flash\
**Created:** [August 9, 2010, 7:37pm UTC](https://forum.kirupa.com/t/restricting-my-game-characters-movement/312640 "2010-08-09T19:37:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylan](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@taylan](https://forum.kirupa.com/u/taylan)\
**Post date:** [August 9, 2010, 7:37pm UTC](https://forum.kirupa.com/t/restricting-my-game-characters-movement/312640/1 "2010-08-09T19:37:18Z")

</div>

I have made a simple game with a scuba diver that has to avoid oncoming sharks and would like help on 2 things please.

1. How do I prevent my scuba diver from leaving the screen? As when i hold either up, down, left or right long enough he will leave the stage entirely.

2. How can I make my sharks enter the screen at random rather than having them enter from the same position each time?

Any help will be greatly appreciated - Here is my code:

(FOR MY SCUBA)

onClipEvent (load) {  
xspeed = 10;  
yspeed = 10;  
}

onClipEvent (enterFrame) {

```
if (Key.isDown(Key.RIGHT)) {
    _x += xspeed;

}
if (Key.isDown(Key.LEFT)) {
    _x -= xspeed;

}
if (Key.isDown(Key.UP)) {
    _y -= yspeed;

}
if (Key.isDown(Key.DOWN)) {
    _y += yspeed;    
}

```

}

(AND FOR MY SHARK)

onClipEvent (load) {  
lw = -150;  
xspeed =8;  
}  
onClipEvent (enterFrame) {  
if (this.hitTest(\_root.scuba)) {  
\_root.gotoAndPlay(3)  
//\_root.health.play()  
//\_x = 1200;  
//\_y = random(100)+200;  
}

```
_x -= xspeed;

if (_x&lt;lw) {
    _x = 1000;
    //_y = random(0)+400;
    //xspeed = random(4)+1;
}

```

}
