title: Create objects in a loop with onclicklistener in JavaScript phaser game framework
body: <p>I'm trying to create an easy quiz game which displays a question and 5 variants of answers.</p>
  
  <p>I work with phaser.js but I think it will applicable for JavaScript in general.</p>
  
  <p>Here is my function that creates 5 buttons</p>
  
  <p>[char_db.js]</p>
  
  <pre><code>for (var i = 0; i &lt; 5; i++) {
      button[i] = game.add.button((30+10*i), (30+5*i), 'buttons', actionOnClick(i), this, 1, 0, 2);
  }
  </code></pre>
  
  <p>And here is my onclick listener which is doing pretty same for each button (but in feature it will do a different functions)</p>
  
  <p>[index.html]</p>
  
  <pre><code>function actionOnClick (id) {
     //  Manually changing the frames of the button, i.e, how it will look when you play with it
     button[id].setFrames(2, 1, 3);
  }
  </code></pre>
  
  <p>I got an error </p>
  
  <pre>[Uncaught TypeError: Cannot read property 'setFrames' of undefined]</pre>
  
  <p>My button array was declared in a very beginning </p>
  
  <pre><code>button = new Array();
  </code></pre>
  
  <p>Any suggestions?</p>