Skip to main content
Known Participant
July 21, 2022
Answered

For Loop function

  • July 21, 2022
  • 1 reply
  • 246 views

Hi,

Can anyone modify this code to for loop? I have searched in this forum but found nothing. I comented on some similar post, but no respon.

 

//I want to add movieclips named mc1, mc2, mc3, mc4.

this.stageW = 550;
this.stageH = 400;
this.speed = 7;

if(!this.alreadyStarted){
this.alreadyStarted = true;
randomNumbersF.bind(this)();
createjs.Ticker.addEventListener("tick", updateF.bind(this));
}

function randomNumbersF(){
this.randX = Math.floor(Math.random()*this.stageW);//scaled to the size of the screen
this.randY = Math.floor(Math.random()*this.stageH);
this.r = Math.sqrt( (this.mc0.y-this.randY)*(this.mc0.y-this.randY) + (this.mc0.x-this.randX)*(this.mc0.x-this.randX) );
linearEqF.bind(this)();
}
function linearEqF(){
this.inc = 1;
this.theta = Math.atan2( this.randY-this.mc0.y, this.randX-this.mc0.x );
}

function updateF(){
this.mc0.x += this.speed*Math.cos(this.theta);
this.mc0.y += this.speed*Math.sin(this.theta);
if(this.inc*this.speed >= this.r){
randomNumbersF.bind(this)();
}
this.inc++;

};

    This topic has been closed for replies.
    Correct answer kglad

    the mc's to change destination at different times, that code will need some changes:

     

    this.stageW = 550;
    this.stageH = 400;
    this.speed = 7;

    if (!this.alreadyStarted) {
    this.alreadyStarted = true;
    randomNumbersF.bind(this)();
    createjs.Ticker.addEventListener("tick", updateF.bind(this));
    }

    function randomNumbersF() {
    for(var i=0;i<5;i++){
    this["mc"+i].randX = Math.floor(Math.random() * this.stageW); //scaled to the size of the screen
    this["mc"+i].randY = Math.floor(Math.random() * this.stageH);
    this["mc"+i].r = Math.sqrt((this["mc"+i].y - this["mc"+i].randY) * (this["mc"+i].y - this["mc"+i].randY) + (this["mc"+i].x - this["mc"+i].randX) * (this["mc"+i].x - this["mc"+i].randX));
    linearEqF.bind(this)();
    }
    }

    function linearEqF(){
    for(var i=0;i<5;i++){  // mc0,..,mc4
    this["mc"+i].inc = 1;
    this["mc"+i].theta = Math.atan2( this["mc"+i].randY-this["mc"+i].y,this["mc"+i].randX-this["mc"+i].x );
    }
    }

     

    function updateF(){

    for(var i=0;i<5;i++){  // mc0,..,mc4
    this["mc"+i].x += this.speed*Math.cos(this["mc"+i].theta);
    this["mc"+i].y += this.speed*Math.sin(this["mc"+i].theta);
    if(this["mc"+i].inc*this.speed >= this["mc"+i].r){
    randomNumbersF.bind(this)();
    }
    this["mc"+i].inc++;

    }

    };

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    July 21, 2022

    the mc's to change destination at different times, that code will need some changes:

     

    this.stageW = 550;
    this.stageH = 400;
    this.speed = 7;

    if (!this.alreadyStarted) {
    this.alreadyStarted = true;
    randomNumbersF.bind(this)();
    createjs.Ticker.addEventListener("tick", updateF.bind(this));
    }

    function randomNumbersF() {
    for(var i=0;i<5;i++){
    this["mc"+i].randX = Math.floor(Math.random() * this.stageW); //scaled to the size of the screen
    this["mc"+i].randY = Math.floor(Math.random() * this.stageH);
    this["mc"+i].r = Math.sqrt((this["mc"+i].y - this["mc"+i].randY) * (this["mc"+i].y - this["mc"+i].randY) + (this["mc"+i].x - this["mc"+i].randX) * (this["mc"+i].x - this["mc"+i].randX));
    linearEqF.bind(this)();
    }
    }

    function linearEqF(){
    for(var i=0;i<5;i++){  // mc0,..,mc4
    this["mc"+i].inc = 1;
    this["mc"+i].theta = Math.atan2( this["mc"+i].randY-this["mc"+i].y,this["mc"+i].randX-this["mc"+i].x );
    }
    }

     

    function updateF(){

    for(var i=0;i<5;i++){  // mc0,..,mc4
    this["mc"+i].x += this.speed*Math.cos(this["mc"+i].theta);
    this["mc"+i].y += this.speed*Math.sin(this["mc"+i].theta);
    if(this["mc"+i].inc*this.speed >= this["mc"+i].r){
    randomNumbersF.bind(this)();
    }
    this["mc"+i].inc++;

    }

    };

    Known Participant
    July 22, 2022

    Wow... it works perfect! Thanks. All movieclips move randomly!

    Best Regards...

    kglad
    Community Expert
    Community Expert
    July 22, 2022

    you're welcome.