# Random Movement Help Please

**URL:** https://forum.kirupa.com/t/random-movement-help-please/8802
**Category:** flash
**Created:** [November 28, 2002, 9:25pm UTC](https://forum.kirupa.com/t/random-movement-help-please/8802 "2002-11-28T21:25:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ntt](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@ntt](https://forum.kirupa.com/u/ntt)
#### Post date: [November 28, 2002, 9:25pm UTC](https://forum.kirupa.com/t/random-movement-help-please/8802/1 "2002-11-28T21:25:09Z")

</div>

Hey everyone, first will you please look at this- [http://www.angelfire.com/sd2/flashstuff/project.html](http://www.angelfire.com/sd2/flashstuff/project.html)

im trying to make some enemies that will move around randomly and NOT walk over the walls inside the movieclip named ‘back’.

now i tried to do this but the guy walks over the walls and stuff, plus he doesnt randomly move, it just moves in the same 3 ways. can anyone please help me out here?

Thank You!!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 30, 2002, 5:37am UTC](https://forum.kirupa.com/t/random-movement-help-please/8802/2 "2002-11-30T05:37:55Z")

</div>

What code are you currently using on the main character?

What code are you attempting to use on the antagonist?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 30, 2002, 8:02pm UTC](https://forum.kirupa.com/t/random-movement-help-please/8802/3 "2002-11-30T20:02:38Z")

</div>

hi, you can look at the fla here. WARNING- the code is very sloppy and disorganized 🙂

[http://www.angelfire.com/sd2/flashstuff/opensource.fla](http://www.angelfire.com/sd2/flashstuff/opensource.fla)

i think it may have something to do with the scrolling because if i test it outside of my game it works perfectly. someone suggested i use localToGlobal. problem is, i dont know what the code should look like then…

Thanks For your help 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 30, 2002, 8:04pm UTC](https://forum.kirupa.com/t/random-movement-help-please/8802/4 "2002-11-30T20:04:34Z")

</div>

oh by the way the code for the guy im trying to add random movement isnt in that fla.

the code is

```auto

onClipEvent (load) {
    hitme = 0;
    position = new Array(4, 0, 0, 0, 4, 90, -4, 0, 180, 0, -4, -90);
    positioncounter = 0;
    speed_x = position[positioncounter];
    speed_y = position[positioncounter+1];
    gotoAndStop (2);
    function testhit () {
        // hittest the wall
        if (_root.back.hitTest(this._x+speed_x+30, this._y+speed_y, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x+speed_x-30, this._y+speed_y, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x+speed_x, this._y+speed_y+30, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x+speed_x, this._y+speed_y-30, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x+speed_x+30, this._y+speed_y+30, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x+speed_x+30, this._y+speed_y-30, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x-30+speed_x, this._y+speed_y+30, true) and hitme<>1 ) {
            hitme = 1;
        }
        if (_root.back.hitTest(this._x-30+speed_x, this._y+speed_y-30, true) and hitme<>1 ) {
            hitme = 1;
        }
    }
}
onClipEvent (enterFrame) {
    // see the hero
    distance = Math.sqrt((this._x-_root.hero._x)*(this._x-_root.hero._x)+(this._y-_root.hero._y)*(this._y-_root.hero._y));
    angle = Math.acos((_root.hero._y-this._y)/distance)*180/Math.PI;
    if (this._x<_root.hero._x) {
        angle = -angle;
    }
    if (distance<200 and Math.abs(angle-this._rotation+90)<60) {
        _root.detected = "YES";
    } else {
        _root.detected = "NO";
    }
    if (hitme == 0) {
        testhit();
        if (hitme == 0) {
            // moving
            setProperty (this, _x, this._x+speed_x);
            setProperty (this, _y, this._y+speed_y);
        }
        if (random(100)<1) {
            hitme = 1;
        }
    }
    // i hit the wall
    if (hitme<>0) {
        gotoAndStop (1);
        hitme = hitme+1;
        // stop to think
        if (hitme == 20) {
            hitme = 0;
            if (random(2) == 1) {
                // turn left
                positioncounternew = positioncounter-3;
                if (positioncounternew<0) {
                    positioncounternew = 9;
                }
                speed_x = position[positioncounternew];
                speed_y = position[positioncounternew+1];
                setProperty ("", _rotation, position[positioncounternew+2]);
                testhit();
            } else {
                // turn right
                positioncounternew = positioncounter+3;
                if (positioncounternew>9) {
                    positioncounternew = 0;
                }
                speed_x = position[positioncounternew];
                speed_y = position[positioncounternew+1];
                setProperty ("", _rotation, position[positioncounternew+2]);
                testhit();
            }
            if (hitme == 1) {
                hitme = 19;
            } else {
                positioncounter = positioncounternew;
                gotoAndStop (2);
            }
            speed_x = position[positioncounter];
            speed_y = position[positioncounter+1];
            setProperty ("", _rotation, position[positioncounter+2]);
        }
    }
}

```
