1

Let me start by saying I am a complete newbie to js I am taking a class and one of our assignments is to make a basic race game. We are able to use any scripts we find so I chose gamequery. The premise is that the players are completely automated and will got straight x% of the time,right y% of the time and left the rest. I have used the gamequery tutorial space game as a sort of guide but when I am working with movement and collisions my code makes it really slow especially with all the back an forth movement.

function Movement(){
movetwo = Math.random();
$(".obstacle").each(function(){                                 
                    var collided = $(this).collision("#player2Body,."+$.gQ.groupCssClass);
                if(collided.length > 0){                        
                             $("#player2").x($("#player2").x()-2);                                                                                          
                }               
                var collided2 = $(this).collision("#playerBody,."+$.gQ.groupCssClass);
                if(collided2.length > 0){                   
                             $("#player").x($("#player").x()-2);                        
                }
            });

if (movetwo <= twol) {
 $("#player2").y($("#player2").y()+2);
}
else if ((movetwo > twol) && (movetwo <= (twol + twor))){
$("#player2").y($("#player2").y()-2);
}
else {
$("#player2").x($("#player2").x()+2);
}
moveone = Math.random();
if (moveone <= twol) {
$("#player").y($("#player").y()+2);
}
else if ((moveone > twol) && (moveone <= (twol + twor))){
$("#player").y($("#player").y()-2);
}
else {
$("#player").x($("#player").x()+2);
}
}

I know there is a way to detect collision before the move but I really don't know how to implement it since the moves are randomized. Would a switch be faster? Also oddly it shows collision with all but about three of the obstacles I placed and for some reason it ignores those few no matter where they are randomly placed. I think I bit off more than my skill level warrants so any help would be appreciated.

flatty
  • 13
  • 5

1 Answers1

1

Instead of checking for each obstacle the collision with the players you should do the oposite: For each player check if they collide with an obstacle.

If you want to check if the player will collide with obstacle before you really move the player you can use the override parameter like explained in this document: https://github.com/onaluf/gameQuery/wiki/API-Changes-in-0.7

Selim Arsever
  • 320
  • 1
  • 6
  • Thank you. I will try that. I had seen the doc about collision override but it didn't really show me how so I was a little lost does it go something like var collided = $(this).collision(".obstacle",."+$.gQ.groupCssClass); and how to I send it the player x and y? – flatty Nov 19 '12 at 00:28
  • 1
    It would look like that: $("#"+player.id).collision(".obstacle",."+$.gQ.groupCssClass, {x: player.nextX, y: player.nextY}); – Selim Arsever Nov 19 '12 at 00:44