# MovieClip moving randomly

**URL:** https://forum.kirupa.com/t/movieclip-moving-randomly/289110
**Category:** flash
**Created:** [May 26, 2009, 2:40pm UTC](https://forum.kirupa.com/t/movieclip-moving-randomly/289110 "2009-05-26T14:40:22Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Gyan\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@Gyan\_Singh](https://forum.kirupa.com/u/Gyan_Singh)
#### Post date: [May 26, 2009, 2:40pm UTC](https://forum.kirupa.com/t/movieclip-moving-randomly/289110/1 "2009-05-26T14:40:22Z")

</div>

Hi Friends,  
I made a class for moving MovieClip randomly, its working fine…  
But I have some problem…

1. MovieClips named “ball” are overlapping with each other. when they hits, the “ball” direction should be change, which I want.

2. I have define a _moving area_ as stage.width and stage.height,  
the target file is ‘ground.fla’ I have a another movieclip named “areaMc”, I want to define _moving area_ with areaMc, How can I do.?

my code is: -

this code is in “Comman\_ball.as”

```auto

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.text.*;

    public class Comman_ball extends MovieClip {

        private var speedX:Number = Math.random()*2-1;
        private var speedY:Number = Math.random()*2-1;
        private var speedR:Number = Math.random()*10-5;
        private var sizeX:Number = Math.random()*30+20;
        private var sizeY:Number = Math.random()*30+20;

        function Comman_ball ():void {
            this.addEventListener (Event.ENTER_FRAME, moveSprite);

        }
        private function moveSprite (evt:Event):void {

            evt.target.ranNumber.text = Math.round(Math.random()*100);
            
            evt.target.x += speedX;
            evt.target.y += speedY;
            //evt.target.rotation += speedR;
            if (evt.target.x > stage.stageWidth || evt.target.x < 0) {
                speedX = -speedX;
                speedR = -speedR;
            }
            if (evt.target.y > stage.stageHeight || evt.target.y < 120) {
                speedY = -speedY;
                //speedR = -speedR;
            }
            /*if (evt.target.hitTestObject(evt.target.hitMc))
            {
            speedY = -speedY;
            trace("hitting")
            }*/
        }
        

    }
}

```
