0

So. Im trying to create an "Alien Invasion" inspired game. And i was wondering how to correctly create a variable with an if statement.

Fx.

    if (gunIsShot){
     // Create variable 'theShot' 
    }

    var theShot{

     // theShot configs

    }

Thanks

Heinevolder
  • 298
  • 2
  • 5
  • 17

1 Answers1

2

You can use an if statement

var theShot;
if(gunisshot){
    theShot=1;
} else {
    theShot=0;
}

Or ternary operators

var theShot = gunisshot ? 1 : 0;
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
  • Thanks. Can i create mulitple instances of the variable this way? :) – Heinevolder May 17 '14 at 09:12
  • What do you mean by "instances of the variable"? – elclanrs May 17 '14 at 09:16
  • You dont instanciate variables in js. If you know object oriented programming and try toapply the concept to your js app, you should read this http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – Armel Larcier May 17 '14 at 09:23