Need help for random

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…

// 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();


try defing your functions like this


function initAnimation(){

}
function render(){

}

also, change


object.onEnterFrame = render();

to


object.onEnterFrame = render;

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

still it is not working i have made the changes

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

post your .fla and i can fix it for you

Gives this a whirl:

// 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();

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

wrong file format so it dnt got uploaded

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

well the object_mc is the instance name for the beeee