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?

3
  • no, that's not possible, but you don't need that. What you need is an Array, here's a tutorial: developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/… Commented May 4, 2021 at 19:18
  • an array might be helpful?.. Commented May 4, 2021 at 19:19
  • so loop and push to an array Commented May 4, 2021 at 19:30

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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;
});

Comments

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

2 Comments

If you find other questions that have the answer, please flag the question as a duplicate, don't answer.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.