2016-07-25 138 views
0

我基本上在画布上做了两面墙。一个在顶部,另一个在底部。我的球员由MOUSE控制,我想知道如何让球员不能穿过墙壁。在画布中碰撞检测后停止玩家运动

下面是两个物体之间的碰撞一般的功能:

function collides(a, b) { 
    var val = false; 

    val = (a.x < b.x + b.width) && 
    (a.x + a.width > b.x) && 
    (a.y < b.y + b.height) && 
    (a.y + a.height > b.y); 

    return val;   
} 

这里的检测碰撞检测代码:

if (collides(player, block)){ 
    //I don't know what goes here. 
} 

任何帮助,将不胜感激。

回答

0

重新定位球员,因为你已经做了夹着球员的y位置始终在顶部和底部的墙壁之间。

在你的鼠标移动处理器(或任何玩家通过鼠标重新定位):

// reposition the player as you already do 
... 

// and clamp the player to stay below the top wall 
if(player.y < wall.y+wall.height){ player.y = wall.y+wall.height); 

// and clamp the player to stay above the bottom wall 
if(player.y+player.height > wall.y){ player.y = wall.y-player.height); 
+1

谢谢!我能够得到它的工作。 –