How do I set the location of 1 element, the location of another random element? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I set the location of 1 element, the location of another random element?

This is so hard to explain. I'm making a game in Javascript. I have a grid of enemy ships which are stored in an array. I also have an empty array of enemy bullets. I want to add an object to the array of enemy bullets every 2 seconds. I want to set the x axis of the enemy bullet, the same x axis of a random enemy. So in other words, I want to make a bullet have the same location as a random enemy in the game. My bug is on LINE 168 Doing this makes my head spin. Please can someone help? https://code.sololearn.com/WRNWxkxdCzpg/?ref=app

1st Jul 2020, 9:11 PM
Clueless Coder
Clueless Coder - avatar
2 Answers
+ 2
ranE = 24.580624913679184 line 168 should be ranE = floor(random()*enemies.length); https://www.w3schools.com/jsref/jsref_random.asp
1st Jul 2020, 11:00 PM
ODLNT
ODLNT - avatar
+ 1
ODLNT Sorry for asking again. But I've nearly finished the game, my only issue is that the damage is nuts. When the enemy bullets collide with the ship, the ship damage is meant to increment and the enemy bullet to get spliced out of the array. I put it in a span, now I know why it doesn't work. 1 hit causes the damage value to go absolutely crazy. How can I fix it so it just increments? The enemies are working but the bullets aren't. The code for it is on line: 231 - end. See for your self in the span, 1 hit is equal to 100,000 hits.
2nd Jul 2020, 9:34 AM
Clueless Coder
Clueless Coder - avatar