Question

this is my first post on these boards, sorry if I've done anything wrong. I've been searching all day for the answer to this question. I'm trying to make a simple game, but the colission detection is killing me. I'm not sure what the issue is with it, it's either I'm storing the enemies incorrectly in the array, or not referencing the array correctly when I'm checking for collision. Any help would be greatly appreciated.

<script>

var paper = Raphael(0,0,300,450);
var x_Pos = 130;
var y_Pos = 400;
var new_Pos;
var player = paper.rect(x_Pos,y_Pos,40,5,10);
var enemy_A = new Array();
var gold_A = new Array();
var goldie = 0;
var lives = 1;
var enemy_Count = 0;
var key_Down = false;
var t_X = 0;
var t_Y = 0;
var k_X = 0;
var k_Y = 0;
//var alive = false;
//var next = true;
var enemy;
var gold;
create_all();
function create_all()
{
            for (var i = 0; i < 45; i++)
            {

                var x = ((Math.random()*200)+50);
                enemy = paper.circle(x, -10, 10);
                enemy.attr({fill: 'black'});
                enemy_A.push(enemy);
                var anim = Raphael.animation({cx:x, cy:500},7000);
                enemy_A[i].animate(anim.delay(i*500));

            }

                for (var j = 0; j < 15; j++)
                {
                var x2 = ((Math.random()*200)+50);
                gold = paper.circle(x2, -10, 5);
                gold.attr({fill: 'yellow'});
                gold_A.push(gold);
                var anim2 = Raphael.animation({cx:x2, cy:500},15000);
                gold_A[j].animate(anim2.delay(j*1000));

            }
        check_Hit(enemy_A); 
}


function check_Hit(array) 
{ 
    var e_a = array;
    var e_abb;
    for (var m = 0; m < e_a.length ; m++) 
    { 
    e_abb = e_a[m].getBBox();
    t_X = e_abb.x + e_abb.width/2;
    t_Y = e_abb.y + e_abb.height/2;
    if (t_Y >= this.y_Pos) 
    { 
        if (t_X >= x_Pos && t_X <= x_Pos + 40) 
        {
        damageHealth(); 
        e_a[m].remove();  
        e_a.pop(m); 

        } 
        }
    }setInterval(check_Hit, 200);
}

function collectGold()
{
    this.goldie = goldie + 10;
}
function damageHealth()
{
    this.lives = lives - 1;
    if (lives == 0)
    {
        //alive = false;
    }
}


function set_Player_Pos_Left(x)
{

    x_Pos = x_Pos - x;
    if (x_Pos < 0)
    {
        x_Pos = 0;
    }
    document.getElementById("test").innerHTML = "GOLD: " + goldie + "LIVES LEFT: " + lives;
    player.animate({x:x_Pos,y:y_Pos},500);
}
function set_Player_Pos_Right(x)
{

    x_Pos = x_Pos + x;
    if(x_Pos > 260)
    {
        x_Pos = 260;
    }
    document.getElementById("test").innerHTML = "GOLD: " + goldie + "   LIVES LEFT: " + lives;
    player.animate({x:x_Pos,y:y_Pos},500);
}
document.onkeydown = function(event) 
{
    var x_Pos = player.x_Pos;
    var key_Code = event.keyCode; 
    if (key_Down == false)
    {
    if (key_Code == 65 || key_Code == 37) //A OR LEFT
        {
            key_Down = true;
            new_Pos = x_Pos - 5;
            set_Player_Pos_Left(10);
        }
        //Whilst 'A' key is down, player moves left screen 2 co-ords a second.
    if (key_Code == 68 || key_Code == 39) //D OR RIGHT
        {
            key_Down = true;
            new_Pos = x_Pos + 5; //var timer = setTimeout('set_Player_Pos(new_Pos)',500);}
            set_Player_Pos_Right(10);
        }
    }
}

    document.onkeyup = function(event)
    {
        key_Code = null;
        key_Down = false;
    }




</script>
Was it helpful?

Solution

setIntervals sets a periodic task. You don't have to call it recursively in your handler. So either use setTimeout instead, or call setInterval only once:

setInterval(function() {
    check_Hit(enemy_A); 
}, 200);

UPDATE:

Well I can't debug this for you but I can see a few issues in your code:

  • setInterval(check_Hit, 200) Will not pass any parameter to check_Hit, so the function will fail miserably.
  • method Array.pop() removes the last element of the array, and does not take any parameter.
  • did you mean to remove the m'th element from the array? if you do this (with e_a.splice(m,1) for instance), element m+1 will take its place, and at the next iteration, you will increment m, thus skipping an element.

Hope this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top