Note that there are some explanatory texts on larger screens.

plurals
  1. POgameQuery collision detection
    primarykey
    data
    text
    <p>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.</p> <p>i am developing a game like Space Invader, the detection for collision between player missile and enemies not working.</p> <p>This is my code:</p> <p>the definition for my Enemy class </p> <pre><code>function Enemy(node){ this.node = $(node); this.pts_value = 0; return true; } </code></pre> <p>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</p> <pre><code>$.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; }); </code></pre> <p>so when i need to move the enemies, i move the enemies together, i move the group that includes all the sprites "#enemy_group"</p> <pre><code> if(ENEMY_TO_RIGHT){ var enemiesNewPos = (parseInt($("#enemy_group").css("left"))) + ENEMY_SPEED; if(enemiesNewPos &lt; 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 &gt; 0){ $("#enemy_group").css("left", ""+enemiesNewPos+"px"); } else { ENEMY_TO_RIGHT = true; } } </code></pre> <p>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"</p> <pre><code> $(".playerMissiles").each(function(){ var posy = parseInt($(this).css("top")); if(posy &lt; 0){ $(this).remove(); return; } $(this).css("top", ""+(posy - MISSILE_SPEED)+"px"); //Test for collisions var collided = $(this).collision(".enemy, .group"); if(collided.length &gt; 0){ //An enemy has been hit! collided.each(function(){ $(this).setAnimation(enemies[0], function(node){$(node).remove();}); }) } }); </code></pre> <p>i was following the documentation tutorial on the gameQuery website.</p> <p>any help appreciated, thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload