# Need help for random

**URL:** <https://forum.kirupa.com/t/need-help-for-random/263667>\
**Category:** Uncategorized\
**Created:** [June 18, 2008, 5:23am UTC](https://forum.kirupa.com/t/need-help-for-random/263667 "2008-06-18T05:23:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 18, 2008, 5:23am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/1 "2008-06-18T05:23:17Z")

</div>

I have written the script but something is wrong in it. I wanted the object to be duplicated and randomly move towards the center of the stage. but the object is not at all being dupicated and the object is still.

Anyone please help with this…

```auto
// Initial variables
var numItems = 10;
         
var stageX = Stage.width;

var stageY = Stage.height;

this.initAnimation = function() {

    for (var i = 0; i<=numItems; i++) {

        var xpos = random(stageX);
        var ypos = random(stageY);

        this.attachMovie("object_mc", "object_mc_"+i, i, {_x:xpos, _y:ypos});

        var object= this["object_mc_"+i];

        angle = 0;
       
        agility = 10;
        
        speed = random(10);
       
        object.onEnterFrame = render();
    }
};
// Calculate animation
this.render = function() {
    trace("render ");
  
    var randomDirection = (Math.random()*2)-1;

    var deviation = randomDirection*this.agility;
 
    this.angle += deviation;
    var rad = this.angle*(Math.PI/180);

    var xpos = this.speed*Math.cos(rad);
    var ypos = this.speed*Math.sin(rad);
    trace("ypos"+ypos);
  
    this._x = (this._x<=0) ? stageX : this._x+xpos;
    this._y = (this._y<=0) ? stageY : this._y+ypos;
    
    this._x %= stageX+1;
    this._y %= stageY+1;
    trace(this._x);
    trace(this._y);
};
initAnimation();

```

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [June 18, 2008, 5:46am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/2 "2008-06-18T05:46:36Z")

</div>

try defing your functions like this

```auto

function initAnimation(){

}
function render(){

}

```

also, change

```auto

object.onEnterFrame = render();

```

to

```auto

object.onEnterFrame = render;

```

(When you pass a reference to a function you dont use the “()” )

---

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 18, 2008, 6:20am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/3 "2008-06-18T06:20:43Z")

</div>

still it is not working i have made the changes

---

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 18, 2008, 10:17am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/4 "2008-06-18T10:17:47Z")

</div>

the script but it is showing only one object but i want to have the object to be duplicated and seen on the stage

thanks for the help

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [June 19, 2008, 4:06am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/5 "2008-06-19T04:06:10Z")

</div>

post your .fla and i can fix it for you

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2008, 4:34am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/6 "2008-06-19T04:34:06Z")

</div>

Gives this a whirl:

```auto
// Initial variables
var numItems = 10;
var stageX = Stage.width;
var stageY = Stage.height;
this.initAnimation = function() {
	for (var i = 0; i <= numItems; i++) {
		var xpos = random(stageX);
		var ypos = random(stageY);
		var object = this.attachMovie("object_mc", "object_mc_" + i, i, {_x:xpos, _y:ypos});
		object.angle = 0;
		object.agility = 10;
		object.speed = random(10);
		object.onEnterFrame = render;
	}
};
// Calculate animation
function render() {
	trace("render ");
	var randomDirection = (Math.random() * 2) - 1;
	var deviation = randomDirection * this.agility;
	this.angle += deviation;
	var rad = this.angle * (Math.PI / 180);
	var xpos = this.speed * Math.cos(rad);
	var ypos = this.speed * Math.sin(rad);
	trace("ypos" + ypos);
	this._x = (this._x <= 0) ? stageX : this._x + xpos;
	this._y = (this._y <= 0) ? stageY : this._y + ypos;
	this._x %= stageX + 1;
	this._y %= stageY + 1;
	trace(this._x);
	trace(this._y);
}
initAnimation();

```

---

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 19, 2008, 5:03am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/7 "2008-06-19T05:03:55Z")

</div>

im attaching my .fla and the object is not appearing on the screen it  
is counting but i want the objects to be displayed on the screen all  
10 of them. and they are not arriving from all parts of the screen to the  
instead they are arriving the stage from one position. why can u just me

---

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 19, 2008, 5:06am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/8 "2008-06-19T05:06:27Z")

</div>

wrong file format so it dnt got uploaded

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2008, 5:34am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/9 "2008-06-19T05:34:02Z")

</div>

Well there’s no movie clip in you library that’s exported for AS as object\_mc.

---

<div class="post-metadata">

**Author:** ![shadowkeeper](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shadowkeeper](https://forum.kirupa.com/u/shadowkeeper)\
**Post date:** [June 19, 2008, 5:57am UTC](https://forum.kirupa.com/t/need-help-for-random/263667/10 "2008-06-19T05:57:21Z")

</div>

well the object\_mc is the instance name for the beeee
