Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enemy's always spawn diagonally

The problem I am having here isn’t necessarily the code, its more how the enemy’s spawn, you see I have constructed a script that automatically chooses a new ‘X’ position for the enemy’s, this event accurse when you have eliminated the previous level of enemy’s. for me, explaining how I made the hit boxes for the enemy’s is rather difficult. My main problem is the enemy’s spawn diagonally next to each other.

ill attach an image of how the enemy’s spawn, and ill chuck the code in for you as well. NOTE: enemy’s do spawn in different locations, but always seem to be diagonal to each other

enemy spawn output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        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;
                        }
                    }
                }
            }
        }
    })

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading