0

I want to iterate multiple statements with "loop" I am wondering what I want to do is even possible with loop.

 var bomb = bombs.create(x,16,'bomb');
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200,200),20);
    bomb.allowGravity =false;

var bomb1 = bombs.create(x,16,'bomb');
    bomb1.setBounce(1);
    bomb1.setCollideWorldBounds(true);
    bomb1.setVelocity(Phaser.Math.Between(-200,200),20);
    bomb1.allowGravity =false;

var bomb2 = bombs.create(x,16,'bomb');
    bomb2.setBounce(1);
    bomb2.setCollideWorldBounds(true);
    bomb2.setVelocity(Phaser.Math.Between(-200,200),20);
    bomb2.allowGravity =false;

var bomb3 = bombs.create(x,16,'bomb');
    bomb3.setBounce(1);
    bomb3.setCollideWorldBounds(true);
    bomb3.setVelocity(Phaser.Math.Between(-200,200),20);
    bomb3.allowGravity =false;

The only change is the number incrementing right behind 'bomb' variable. Is it possible to use loop to iterate more than 100 times?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
goosamsf
  • 19
  • 1
  • 2

4 Answers4

0

Yes, you can use a loop this way, for example:

var objs = {}
const total = 10;

const getBomb = () => {
    var bomb = bombs.create(x, 16, 'bomb');
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
    bomb.allowGravity = false;

    return bomb;
}

for (let i = 0; i < total; i++) {
    objs[`bomb${i}`] = getBomb();
}

console.log('objs', objs)
0
let bombArray = Array(100).fill().map(_=> {
    let bomb = bombs.create(x, 16, 'bomb');
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
    bomb.allowGravity = false;
    return bomb;
});
loop
  • 825
  • 6
  • 15
0

I do not think it is very appropriate to use the names of dynamic variables in JavaScript

In these cases, you can use arrays

Example:

var allBombs = [];
for (let i = 0; i < 100; i++) {
    var bomb = bombs.create(x, 16, 'bomb');
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
    bomb.allowGravity = false;
    allBombs.push(bomb);
}
console.log(allBombs);// log all
console.log(allBombs['15']);// log one(15)

But again, there are solutions for that, which I suggest you follow the following:

Example:

var k = 'bomb';
var i = 0;
for(i = 1; i < 5; i++) {
    eval('var ' + k + i + '= ' + i + ';');
}
console.log("bomb1=" + bomb1);
console.log("bomb2=" + bomb2);
console.log("bomb3=" + bomb3);
console.log("bomb4=" + bomb4);

Contents:

How do I create dynamic variable names inside a loop?

Use dynamic variable names in JavaScript

https://www.geeksforgeeks.org/how-to-use-dynamic-variable-names-in-javascript/

https://www.sitepoint.com/community/t/dynamic-i-variable-names-in-javascript/4236/2

Erfan Bahramali
  • 392
  • 3
  • 13
  • If you find other questions that have the answer, please flag the question as a duplicate, don't answer. – Heretic Monkey May 04 '21 at 19:38
  • True, but I think this question could be a little different from other questions , Because I do not think the method is important and is looking for a solution, I also said 2 different solutions – Erfan Bahramali May 04 '21 at 19:47
0

// Bomb constructor for demo - you would use `bombs.create` function here
function Bomb() {
  this.collideWorldBounds = true;
  this.velocity = null;
  this.allowGravity = false;
}

// An array to store all bombs, use this instead of bomb1, bomb2, bomb3 etc.
const bombs = [];

// Loop a predetermined number of iterations and populate the array.
for (let i = 0; i < 100; i++) {
  const bomb = new Bomb();
  // Set any additional `bomb` instance properties here
  bombs.push(bomb);
}

// See contents of the array
console.log(bombs);
Tom O.
  • 5,730
  • 2
  • 21
  • 35