简体   繁体   中英

Detecting Collision of two sprites in arrays? Javascript

I have been reading/searching for an answer to detect collisions of sprites in two arrays. I am not understanding how to pass two arrays into the detection function and have it check all contents of each array against each other. Any input would be greatly appreciated.

<script type="text/javascript">

        var FIRE = 0;
        var NORTH = 38;
        var SOUTH = 40;
        var EAST  = 39;
        var WEST  = 37;
        var destX = 350;
        var destY = 500;
        var canvas = null;
        var context = null;
        var sprites = null;
        var player = null;
        var island = null;
        var enemies = [];
        var fires = [];
        var gameLoopInterval = null;
        var offScreenFire = null;
        var isShooting = false;
        var intersect = null;

    var Fire = function() {
            this.spriteX = 278;
            this.spriteY = 110;
            this.spriteWidth = 13;
            this.spriteHeight = 16;
            this.destX = player.destX + 25;
            this.destY = player.destY;
            this.speed = 5;
        }

    var Player = function(name) {
            this.name = name;
            this.spriteX = 5;
            this.spriteY = 400;
            this.spriteWidth = 64;
            this.spriteHeight = 64;
            this.destX = 350;
            this.destY = 500;
            this.speed = 5;
            this.level = 1;
        }

    var Enemy = function() {

            this.spriteX = 4;
            this.spriteY = 4;
            this.spriteWidth = 32;
            this.spriteHeight = 32;
            this.destX = Math.ceil(Math.random() * (800 - this.spriteWidth));
            this.destY = this.spriteWidth;
            this.speed = Math.ceil(Math.random() * 5);
        }

    var Island = function() {

            this.spriteX = 168;
            this.spriteY = 500;
            this.spriteWidth = 64;
            this.spriteHeight = 64;
            this.destX = Math.ceil(Math.random() * (800 - this.spriteWidth));
            this.destY = this.spriteWidth - 64;
            this.speed = 2;
        }

    Fire.prototype.takeTurn = function() {

        var intersect;
        var projdestX  = this.destX;
        var projdestY  = this.destY;
        var projspriteWidth  = this.spriteWidth;
        var projspriteHeight  = this.spriteHeight;


        for (enemy in enemies)  {
             intersect = intersect || intersects(enemy.destX, enemy.destY, enemy.spriteWidth, enemy.spriteHeight, projdestX, projdestY, projspriteWidth, projspriteHeight);
        }
            if(intersect == true) { alert("colliding"); }
        else{drawImage(this);}
            // if (intersect != true){
                // drawImage(this);
            // }
            // else {
                // alert("boom");
            // }

            if(this.destY <= 0){
            offScreenFire = fires.indexOf(this);
            fires.splice(offScreenFire, 1);
            }
            else
            this.destY -= this.speed;

        }

    Player.prototype.takeTurn = function() {
            drawImage(this);
        }

    Enemy.prototype.takeTurn = function() {

            drawImage(this);


         if (this.destY < canvas.height)
                this.destY += this.speed;
         else 
             this.destY = -32;
        }

    Island.prototype.takeTurn = function() {
            drawImage(this);
            this.destY += this.speed;
        }

function fireAction() {

            var fire = new Fire();

                drawImage(fire);
                fires.push(fire);

    } 

function drawImage(sprite) {
            context.drawImage(sprites, sprite.spriteX, sprite.spriteY, sprite.spriteWidth, sprite.spriteHeight, sprite.destX, sprite.destY, sprite.spriteWidth, sprite.spriteHeight );
    }

function gameLoop () {

            context.clearRect(0, 0, canvas.width, canvas.height);

            island.takeTurn();
            player.takeTurn();

            //console.log(fires); //debug

            for (fire in fires) {
                fires[fire].takeTurn();

               }
            for (enemy in enemies) {
                enemies[enemy].takeTurn();

                    }   

        }


function intersects(x1, y1, w1, h1, x2, y2, w2, h2) {
        if (w2 !== Infinity && w1 !== Infinity) {
                 w2 += x2;
                 w1 += x1;
            if (isNaN(w1) || isNaN(w2) || x2 > w1 || x1 > w2)
                return false;
            }

        if (y2 !== Infinity && h1 !== Infinity) {
                h2 += y2;
                h1 += y1;
              if (isNaN(h1) || isNaN(y2) || y2 > h1 || y1 > h2)
                return false;
            }

         return true;
}

window.onload = function() {
        //alert('here');
            canvas =  document.getElementById('gameWorld');
            context = canvas.getContext("2d");
            sprites = new Image();

            player = new Player('Brad');
            island = new Island();


            sprites.onload = function() {
                drawImage(player);  
                for (i = 0; i < 3; i++) {
                    var enemy = new Enemy();
                    drawImage(enemy);
                    enemies.push(enemy);
                }
            }

            sprites.src = "Sprites/1945.png";

            gameLoopInterval = setInterval('gameLoop()', 100)
    }




    window.onkeypress = function(e){
            var evt = window.event ? event : e;
            //alert(evt.keyCode);
            switch(evt.keyCode) {
                case NORTH:

                 if (player.destY > 0)
                    player.destY -= player.speed;
                else
                    player.destY == player.destY;   

                    break;

                case SOUTH:

                 if (player.destY < canvas.height - player.spriteWidth)
                    player.destY += player.speed;
                else
                    player.destY == player.destY;   

                    break;

                case EAST:

                    if (player.destX < canvas.width - player.spriteWidth)
                       player.destX += player.speed;
                else
                    player.destX == player.destY;

                    break;

                case WEST:

                if (player.destX > 0)
                    player.destX -= player.speed;
                else
                    player.destX == player.destX;

                    break;

                case FIRE:

                        fireAction();

                    break;
            }
    }
</script>

Your problems seems to be in:

for (enemy in enemies) {
    intersect = intersects(enemy.destX, enemy.destY, enemy.spriteWidth, enemy.spriteHeight, projdestX, projdestY, projspriteWidth, projspriteHeight);
}

intersect will always have the last value saved. (Meaning you are really only checking if it intersects with the last enemy.)

A quick solution would be to change the inner line to:

intersect = intersect || intersects(enemy.destX, enemy.destY, enemy.spriteWidth, enemy.spriteHeight, projdestX, projdestY, projspriteWidth, projspriteHeight);

This will make intersect stay as true if the fire doesn't intersect with the next enemy.

Edit:

Your second problem is in the same for(). In javascript, when you do a for in , the first variable does not have a reference to the instance, but rather is only the key.

Your final for should look like:

for (enemy in enemies) {
    intersect = intersect || intersects(enemies[enemy].destX, enemies[enemy].destY, enemies[enemy].spriteWidth, enemies[enemy].spriteHeight, projdestX, projdestY, projspriteWidth, projspriteHeight);
}

You also seem to not be spawning any enemies. In my fiddle of your code ( http://jsfiddle.net/path411/umjnQ/ ) I added the following snippet into your gameLoop() :

if(enemies.length < 1) {
    enemies.push(new Enemy());
}

This simply creates a new enemy if you don't already have one. (You will probably want to change later).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM