JavaScript: .push is not a function

1

How are you? I'm practicing some JavaScript, since I'm new to it, and I'm trying to create a "space invaders" game with p5.js. I have made some progress, but, I have encountered a problem that I can not solve:

var ship;
var aliens = []; 
var missiles = [];
...
...
function keyPressed() {
if (key === ' ') {
    var missiles = new missile (width/2, height/2);
    missiles.push(missile)

When I try to test by pressing the space bar, it tells me the following:

It is assumed that when you press the spacebar, the code adds a new element to the missiles array, but why does it throw that error?

    
asked by Jose A. Pacheco 10.08.2018 в 01:37
source

1 answer

1

With this, your problem should be solved, taking into account that you only want to add those parameters to the previously defined array:

var ship;
var aliens = []; 
var missiles2 = [];
...
...
function keyPressed() {
if (key === ' ') {

  //creamos el objeto que deseas con sus parametros
   var obj = new missile (width/2, height/2);
  //agregamos el objeto al array
   missiles2.push('obj');

}

more info. Push Method

I hope it serves you and marques XD ... ReNiceCode ....

    
answered by 10.08.2018 / 02:21
source