The problem I am having here isn't necessarily the code, it's more how the enemies spawn. I have constructed a script that automatically chooses a new 'X' position for the enemies, this event occurs when you have eliminated the previous level of enemies. For me, explaining how I made the hit boxes for the enemies is rather difficult. My main problem is the enemies spawn diagonally next to each other.
I''ll attach an image of how the enemies spawn, and ill chuck the code in for you as well. NOTE: enemies do spawn in different locations, but always seem to be diagonal to each other.
setInterval(function resetWaveSurviver(){
if (waveSurviver == true){
if (enemy1.style.left == '-100px'){
if (enemy2.style.left == '-100px'){
if (enemy3.style.left == '-100px'){
if (enemy4.style.left == '-100px'){
hits = 0;
E1X1 = Math.floor(Math.random() * 529) 1;
E2X1 = Math.floor(Math.random() * 521) 1;
E3X1 = Math.floor(Math.random() * 540) 1;
E4X1 = Math.floor(Math.random() * 530) 1;
E1X2 = E1X1 50
E1Y1 = E1X1
E1Y2 = E1Y1 50
E2X2 = E2X1 50
E2Y1 = E2X1
E2Y2 = E2Y1 50
E3X2 = E3X1 50
E3Y1 = E3X1
E3Y2 = E3Y1 50
E4X2 = E4X1 50
E4Y1 = E4X1
E4Y2 = E4Y1 50
enemy1.style.left = '' E1X1 'px'
enemy2.style.left = '' E2X1 'px'
enemy3.style.left = '' E3X1 'px'
enemy4.style.left = '' E4X1 'px'
enemy1.style.top = '' E1Y1 'px'
enemy2.style.top = '' E2Y1 'px'
enemy3.style.top = '' E3Y1 'px'
enemy4.style.top = '' E4Y1 'px'
cycle = false;
hits = 0;
}
}
}
}
}
})
CodePudding user response:
They are diagonal because you assign the Y coords same as X: E1Y1 = E1X1
.
If you want random positions, you need:
E1Y1 = Math.floor(Math.random() * maxHeight) 1;
E1Y2 = E1Y1 50;
E2Y1 = Math.floor(Math.random() * maxHeight) 1;
E2Y2 = E2Y1 50;
E3Y1 = Math.floor(Math.random() * maxHeight) 1;
E3Y2 = E3Y1 50;
E4Y1 = Math.floor(Math.random() * maxHeight) 1;
E4Y2 = E4Y1 50;
For X all the values remain the same.