• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

For Loop function

Explorer ,
Jul 21, 2022 Jul 21, 2022

Copy link to clipboard

Copied

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++;

};

Views

112

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 21, 2022 Jul 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

...

Votes

Translate

Translate
Community Expert ,
Jul 21, 2022 Jul 21, 2022

Copy link to clipboard

Copied

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++;

}

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2022 Jul 21, 2022

Copy link to clipboard

Copied

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

Best Regards...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2022 Jul 21, 2022

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines