简体   繁体   English

2D Processing.js游戏中的碰撞

[英]Collision in 2D Processingjs Game

I have this Javascript code that uses the ProcessingJS library 我有使用ProcessingJS库的Javascript代码

 // Game var player = { x: 50, y: 100, width: 30, height: 30, speed: 10.5, score: 0 }; var food = { x: 200, y: 200, width: 30, height: 30, speed: 10.5 }; var up = 38; var down = 40; var left = 37; var right = 39; draw = function() { background(155, 154, 156); fill(0, 255, 85); rect(player.x,player.y,player.width,player.height); fill(245, 3, 3); rect(food.x,food.y,food.width,food.height); text('Score: ' + player.score,50,50); }; var collided = function(){ if (player.x < food.x + food.width && player.x + player.width > food.x && player.y < food.y + food.height && player.height + player.y > food.y) { return true; } else { return false; } }; keyPressed = function(){ switch(keyCode){ case up: player.y -= player.speed; break; case down: player.y += player.speed; break; case right: player.x += player.speed; break; case left: player.x -= player.speed; break; default: break; } if(collided()){ while(collided()){ if(/* condition */){ // Do What? }; } } }; 

I named an object "food" for some reason, so don't take that in mind. 我出于某种原因将对象命名为“食物”,所以请不要忘记这一点。 I'm trying to make it so that when the player collides with the "food" object, it will act as if the "food" object is a solid object and won't be able to pass through it (like you're trying to walk through a wall.) 我正在尝试使之与玩家与“食物”对象碰撞时的行为,就好像“食物”对象是固体对象并且无法通过它一样(就像您正在尝试的那样)穿过一堵墙。)

I tried subtracting 1 from the player's X position until collision stops. 我尝试从玩家的X位置减去1,直到碰撞停止。 I moved the player to collide with the side of the "food" object, and it worked. 我移动了播放器使其与“食物”对象的侧面碰撞,并且它起作用了。 But when I collided at the top or bottom of the object, it moves the player to the edge of the object. 但是,当我在对象的顶部或底部碰撞时,它会将播放器移到对象的边缘。 I have no idea how to fix this, so help would be appreciated. 我不知道如何解决此问题,因此将不胜感激。

1) Instead of trying to guess where the player should go back to, I would start off by storing the player-position at the start of each keyPressed 1)我不会尝试猜测玩家应该回到哪里,而是从在每个按键的开始处存储玩家位置开始

keyPressed = function(){
    var oldX = player.x;
    var oldY = player.y;
    //do the other stuff

Now, if there is a collision, you can just set him back to the old location with player.x = oldX etc. Of course, this has the possibility of setting him back too far. 现在,如果发生碰撞,您可以使用player.x = oldX等设置他回到原来的位置。当然,这有可能使他回到太远。

2) Instead, you can use something a bit closer to your approach. 2)相反,您可以使用一些更接近您的方法的方法。 You know which direction the player is coming from because of the keyPressed and you know how far you need to move him back because you have the coordinates of 'food'. 您知道由于keyPressed而导致玩家从哪个方向来回运动,并且知道您需要将其移回多远,因为您具有“食物”坐标。 We can skip the "while(collided)if(condition)" and put another switch directly into the "if(collided)" 我们可以跳过“ while(collided)if(condition)”,然后将另一个开关直接放入“ if(collided)”中

if(collided()){
    switch(keyCode){
        case up:
            player.y = food.y + food.height;
            break;
        case down:
            player.y = food.y - player.height;
            break;
        //etc.

3) Finally, it might be that you coded the continuous checking in because you want to add more obstacles and figured this the easiest way to make sure the player doesn't collide with anything. 3)最后,可能是因为您要添加更多的障碍并想出了确保玩家不会与任何物体发生碰撞的最简单方法,所以您对连续签入进行了编码。 If that's the case, I would suggest you look up a bit more about how classes and objects work (if you want to code everything yourself) or look for a collision-library (if you want it to be more efficient). 如果是这样,我建议您更多地了解类和对象的工作方式(如果您想自己编写所有代码)或寻找一个碰撞库(如果您想提高效率)。 This hardcoded approach seems likely to give you headaches later on if you want to expand the scope of the game or add functionality. 如果您想扩大游戏范围或添加功能,这种硬编码方法似乎可能会在以后让您头疼。

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

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