Collisions on canvas html5 game

0

I hope you are having a good day. I'm here because I have a canvas collision problem ... I am creating or trying to create my first jjuego with canvas and javascript. I'm on the side of collisions, which does not work for me, I do not understand why. I have already tried several algorithms, I tried a basic one made by me and nothing. I do not see where the problem is, I hope you can help me. Thank you very much

Here I leave the full code: link

and here I leave the specific parts:

function creandoEnemigos(numeroEnemigos){  //CREATING ENEMIES
        for(var i=0;i<numeroEnemigos;i++){
            ctx.fillStyle='yellow';
            ctx.fillRect(enemigos[i].posX,enemigos[i].posY,enemigos[i].width,enemigos[i].height);
            //ctx.drawImage(enemy,enemigos[i].posX,enemigos[i].posY);   
        }   
    }

    function moviendoEnemigos(){   //moving enemies
        for(var i=0;i<numeroEnemigos;i++){
            //ctx.clearRect(enemigos[i].posX,enemigos[i].posY,enemigos[i].width,enemigos[i].height);
            enemigos[i].posX+=enemigos[i].velocidad; //va a moverse con la operacion: + n veces sea declarada la variable velocidad
        }
        for(var j=0;j<numeroEnemigos;j++){
            if(enemigos[j].posX>canvas.width){
                timerEnemy++;
                enemigos[j].posX=0;
                enemigos[j].posY+=2;
            }
            if(enemigos[j].posY==canvas.height/2){
                enemigos[j].posX=0;
                enemigos[j].posY-=200;
            }
        }
    }

    function creandoDisparos(){ //CREATING BULLETS
        //se crea instancia del objecto disparosJson
        var disp = new disparosJson(canonJson.posX+5, disparosJsonStartValues.posY+30, disparosJsonStartValues.width, disparosJsonStartValues.height)
        //agregamos al arreglo el nuevo disparo
        arrayDisparos[arrayDisparos.length] = disp;
        if(arrayDisparos.length>20){
           /*intervaloBalas=window.setInterval(function(){
                alert("Balas agotadas, espera 5 segundos...");
            },2000); */
        }
    }

    function moviendoDisparos(){ //MOVING BULLETS
        ctx.fillStyle='white';
        for(var i=0;i<arrayDisparos.length;i++){
            let disp = arrayDisparos[i];
            //clear space of bullet
            ctx.clearRect(disp.posX1,disp.posY,disp.width,disp.height);
            disp.posY-=2.2;
            //draw space of bullet
            ctx.fillRect(disp.posX1,disp.posY,disp.width,disp.height);  
        }
    }

    function disparar(){
        creandoDisparos(); 
    }

    function colisionObjetos(){ //FUNCTION COLLISION

        var colision= false;
        if (arrayDisparos.posX < enemigos.posX + enemigos.width  && arrayDisparos.posX + arrayDisparos.width  > enemigos.posX &&
        arrayDisparos.posY < enemigos.posY + enemigos.height && arrayDisparos.posY + arrayDisparos.height > enemigos.posY){
            colision=true;
        }
            if(colision){
                alert("coli detected");
            }
    }
    
asked by TELPRO 19.11.2017 в 18:11
source

1 answer

0

Your variables are arrays, so you have to access them with notation [].

enemigos[i].posX 

instead of

enemigos.posX 
    
answered by 19.11.2017 в 19:44