简体   繁体   English

使用createJS进行Javascript游戏中的碰撞检测

[英]Collision detection in Javascript game made using createJS

Hello I am making a space shooter game and I want to check collision between boss and player bullets. 您好,我正在制作太空射击游戏,我想检查老板和玩家子弹之间的碰撞。 The problem I am facing is that collision between boss bullet and player is perfectly detected but boss is colliding with its own bullet and loosing all its health and this happens only when animation goes to left side of canvas. 我面临的问题是,可以很好地检测到老板子弹与玩家之间的碰撞,但是老板正在与自己的子弹相撞,失去了所有的健康,这只会在动画进入画布的左侧时发生。 Here is my code 这是我的代码

function fire_Boss02_first(){

        var boss02_bullet_first=bullet_first.clone();
        boss02_bullet_first.x=boss02.x+bullet2_boss_stats.offSetX;
        boss02_bullet_first.y=boss02.y+bullet2_boss_stats.offSetY;
        boss02_bullet_first.st=bullet2_boss_stats;
        stage.addChild(boss02_bullet_first);
        time=(boss02_bullet_first.y+500)/0.99;
        createjs.Tween.get(boss02_bullet_first).to({y:1000},time);
        boss02_bullet_first.addEventListener("tick",collisionEvent);
        function collisionEvent(event) {
            for(var i=0; i<stage.children.length; i++){
                if(stage.getChildAt(i).name == "player"){
                    var intersection = ndgmr.checkRectCollision(stage.getChildAt(i),boss02_bullet_first );
                    if(intersection != null){
                        stage.getChildAt(i).st.health = stage.getChildAt(i).st.health - boss02_bullet_first.st.damage;
                         health_txt.text="ENERGY: "+stage.getChildAt(i).st.health;
                        if(stage.getChildAt(i).st.health <= 0){
                            explode = explode_fx.clone();
                            explode.x=stage.getChildAt(i).x -50;
                            explode.y=stage.getChildAt(i).y -50;
                            explode.addEventListener("animationend", endAnimation); 
                                function endAnimation(event) {
                                stage.removeChild(explode);
                             }                              
                             stage.addChild(explode);
                             stage.removeChildAt(i);
                             lives--;
                             lives_txt.text = "LIVES: " + lives;
                        }
                        stage.removeChild(boss02_bullet_first);
                        intersection = null;
                    }
                }   

                else if(stage.getChildAt(i).name == "boss02"){
                        var intersection = ndgmr.checkRectCollision(stage.getChildAt(i),bullet);
                        if(intersection != null){
                        stage.getChildAt(i).st.health = stage.getChildAt(i).st.health - bullet.st.damage;
                        console.log(stage.getChildAt(i).st.health);
                        console.log(bullet);
                        if(stage.getChildAt(i).st.health <= 0){
                            score+=500;
                            score_text.text="SCORE: "+score;
                            explode = explode_fx.clone();
                            explode.x=stage.getChildAt(i).x +50;
                            explode.y=stage.getChildAt(i).y +50;
                            explode.addEventListener("animationend", endAnimation); 
                                function endAnimation(event) {
                                stage.removeChild(explode);
                             }                              
                             stage.addChild(explode);
                             stage.removeChildAt(i);

                        }
                        stage.removeChild(bullet);
                        intersection = null;
                    }
                }

            }
        }

    }

这是控制台输出

This is the Console Output. 这是控制台输出。 I am using ndgmr.checkRectCollision for collision detection. 我正在使用ndgmr.checkRectCollision进行碰撞检测。 Cant seem to find the bug.Any more code necessary do let me know Thanks. 似乎无法找到该错误。任何其他必要的代码都会让我知道,谢谢。

Something useful from a recent EaselJS prototype I've been working on: 我正在研究的最新EaselJS原型中有用的东西:

if ( shipBox.x >= enemyBox.x + enemyBox.width || shipBox.x + shipBox.width <= enemyBox.x || shipBox.y >= enemyBox.y + enemyBox.height || shipBox.y + shipBox.height <= enemyBox.y )
{
  return false;
}else{
  return true;
}    

This similarly helped me when I was trying to draw those bounding boxes: 当我尝试绘制边界框时,这同样对我有帮助:

function DrawBoundingBox(collider){
    var bb = new createjs.Shape();
    bb.graphics.clear().beginStroke("black").drawRect(collider.x, collider.y, collider.width, collider.height);
    bb.regX = collider.width / 2 + collider.x;
    bb.regY = collider.height / 2 + collider.y;
    bb.x = collider.x;
    bb.y = collider.y;
    stage.addChild(bb);
    stage.update();
} 

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

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