简体   繁体   English

gamequery碰撞问题

[英]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. 首先,我要说我是js的完全新手,我正在上课,我们的任务之一是制作一个基本的赛车游戏。 We are able to use any scripts we find so I chose gamequery. 我们能够使用找到的任何脚本,因此我选择了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. 前提是玩家是完全自动化的,将有x%的时间获得正牌,有y%的时间获得正牌,其余的则剩下。 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. 我曾经使用gamequery教程太空游戏作为一种指南,但是当我处理运动和碰撞时,我的代码确实使它特别慢,尤其是在前后运动时。

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 如果要在真正移动播放器之前检查播放器是否会与障碍物碰撞,则可以使用本文档中说明的override参数: https : //github.com/onaluf/gameQuery/wiki/API-Changes-in- 0.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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