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.