Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Attempting to make a ammo reload script in JS, trying to use switch/case with either Enum, or Array (newbie )

$
0
0
So like I said I'm trying to right a script that will reload the "gun" (there is no gun all it does is add and subtract from two separate variables) and at the moment I can only "reload" if the "mag" is completely empty. Now I know I could just go through and write eight different "if statements" but I'm trying to further develop my knowledge of the language so I'm trying to use some thing else. Right now the else is a Switch and Case (I'm pretty sure it can be applied to this) and I'm trying to use either an array or an Enumeration. I don't know which one to use(If either) or how to use it I would love some help. Thank you! Here's the code: #pragma strict var ammoInMag = 8; var ammoReserve = 32; function Update () { if (Input.GetKeyDown("space") && ammoInMag > 0){ ammoInMag--; print(ammoInMag); } if (ammoInMag == 0){ if (Input.GetKeyDown("space")){ print("press R to reload"); } } if(Input.GetKeyDown("r") && ammoInMag == 0){ ammoInMag = 8; ammoReserve = ammoReserve - 8; if(ammoReserve == 0 || ammoReserve < 0){ ammoInMag = 0; ammoReserve = 0; print("you need to find more ammo!"); } } } function OnGUI(){ GUI.Box(Rect(1290,590,150,150),ammoInMag + "/" + ammoReserve); } At the moment I'm also trying to make it display how much is in the mag and how much is left in a little GUI box on the screen. Also for some reason the code tags aren't working... I don't know why...

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>