简体   繁体   中英

GameQuery collision detection not working properly

Below is my code. Collisions actually register and execute, but only if your ship is at the right edge of the screen and one of the monsters is behind you. It makes no sense and I'm going crazy. Please help.

Demo: http://cosmati.net/biebsattack/test2.html It outputs any collisions below the play area, including the coordinates of both the missile and the enemy. Also, feel free to laugh, this is just the gamequery tutorial with some added flavor and embellishments. ;) edit : Also, the assets are HUGE, I know. Just for testing. Sorry about the load time.

        $.playground().registerCallback(function () {

            $(".playerMissiles").each(function () {
                //Test for collisions
                var collided = $(this).collision(".enemy,#enemies");
                if (collided.length > 0) {
                    var missilenum = $(this).attr("id");
                    var missilecoords = $(this).css("left") + ", " + $(this).css("top");
                    //An enemy has been hit!
                    collided.each(function () {
                        $("#lblCollisionLog").append(missilenum + " (" + missilecoords + ") collided with " + $(this).attr("id") + " (" + $(this).css("left") + ", " + $(this).css("top") + ") <br>");
                        if ($(this)[0].enemy.damage()) {
                            $(this).setAnimation(enemies[0]["explode"], function (node) { $(node).remove(); });
                            $(this).css("width", 99);
                            $(this).removeClass("enemy");
                        }
                    })
                    $(this).setAnimation(missile["playerexplode"], function (node) { $(node).remove(); });
                    $(this).css("width", 99);
                    $(this).css("height", 99);
                    $(this).css("top", parseInt($(this).css("top")) - 7);
                    $(this).removeClass("playerMissiles");
                }

            });
        }, 10);

I just discovered it was because I was setting the x,y coordinates using the deprecated method of updating the CSS (selector.css("left",posx);) instead of the new method (selector.x).

It's working properly now.

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