简体   繁体   中英

gamequery collision issue

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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