简体   繁体   English

游戏查询碰撞检测

[英]gameQuery collision detection

it is the first time for me to explore jQuery and gameQuery for building games using JavaScript, so am asking about sth that might look very naive, but really i cant get it.这是我第一次探索 jQuery 和 gameQuery 以使用 JavaScript 构建游戏,所以我问的问题可能看起来很幼稚,但我真的无法理解。

i am developing a game like Space Invader, the detection for collision between player missile and enemies not working.我正在开发像 Space Invader 这样的游戏,检测玩家导弹和敌人之间的碰撞不起作用。

This is my code:这是我的代码:

the definition for my Enemy class我的敌人 class 的定义

function Enemy(node){
    this.node = $(node);
    this.pts_value = 0;
    return true;
}

this is the code i use to add ten enemy sprite next to each other.这是我用来添加十个敌人精灵的代码。 the enemies move together to the left and the right敌人一起向左和向右移动

$.each(new Array(10), function(index, value) {
    $("#enemy_group").addSprite("enemy2_"+index,{animation: enemies[2],
         posx: index * 55, posy: 0, width: 48, height: 48})
    $("#enemy2_"+index).addClass("enemy");
    $("#enemy2_"+index)[0].enemy = new Enemy($("#enemy2_"+index));
    $("#enemy2_"+index)[0].pts_value = 150;
});

so when i need to move the enemies, i move the enemies together, i move the group that includes all the sprites "#enemy_group"所以当我需要移动敌人时,我会一起移动敌人,我会移动包含所有精灵的组“#enemy_group”

    if(ENEMY_TO_RIGHT){
        var enemiesNewPos = (parseInt($("#enemy_group").css("left"))) + ENEMY_SPEED;
        if(enemiesNewPos < PLAYGROUND_WIDTH - 550){
            $("#enemy_group").css("left", ""+enemiesNewPos+"px");
        } else {
            ENEMY_TO_RIGHT = false;
        }
    } else {
        var enemiesNewPos = (parseInt($("#enemy_group").css("left"))) - ENEMY_SPEED;
        if(enemiesNewPos > 0){
            $("#enemy_group").css("left", ""+enemiesNewPos+"px");
        } else {
            ENEMY_TO_RIGHT = true;
        }
    }

finally for collision detection, i want to remove the enemy sprite that the players missile has hit, each missile sprite has an added class names ".playerMissiles"最后为了碰撞检测,我想移除玩家导弹击中的敌人精灵,每个导弹精灵都有一个添加的 class 名称“.playerMissiles”

    $(".playerMissiles").each(function(){
        var posy = parseInt($(this).css("top"));

        if(posy < 0){
            $(this).remove();
            return;
        }

        $(this).css("top", ""+(posy - MISSILE_SPEED)+"px");
        //Test for collisions
        var collided = $(this).collision(".enemy, .group");
        if(collided.length > 0){
            //An enemy has been hit!
            collided.each(function(){
                $(this).setAnimation(enemies[0], function(node){$(node).remove();});
            })

        }
    });

i was following the documentation tutorial on the gameQuery website.我正在关注 gameQuery 网站上的文档教程。

any help appreciated, thanks,任何帮助表示赞赏,谢谢,

I can't see any problem with your code.我看不出你的代码有任何问题。 I can only give you a few pointers:我只能给你几个指点:

  • Did you create "enemy_group" with the addGroup function?您是否使用 addGroup function 创建了“enemy_group”?
  • Is "enemy_group" nested in something special like a custom div? “enemy_group”是否嵌套在像自定义 div 这样的特殊东西中? for the collision detection to work you need a chain of parent composed only of sprites and groups (and tiles map)为了使碰撞检测起作用,您需要一个仅由精灵和组(以及瓦片图)组成的父链
  • Is "enemy_group" nested in a sprite, if so it's a bad idea because you will need to add the selector for this sprite in your methode call and this sprite will be included in the colliding element list. “enemy_group”是否嵌套在精灵中,如果是这样,这是一个坏主意,因为您需要在方法调用中添加此精灵的选择器,并且此精灵将包含在碰撞元素列表中。
  • The same goes for the ".playerMissiles" “.playerMissiles”也是如此

Just to be sure what version of gameQuery and jQuery do you use?只是为了确定您使用的是哪个版本的 gameQuery 和 jQuery? The last version from gitHub is unstable and I wouldn't recomend using it, user 0.5.1 instead. gitHub 的最后一个版本不稳定,我不建议使用它,而是使用用户 0.5.1。

You could use jquery collision plugin, so you avoid doid the logic by yourself.您可以使用jquery 碰撞插件,避免自己做逻辑。

Hope this helps.希望这可以帮助。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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