Javascript HTML5画布游戏中的实体对象-can';不要沿着山顶走

Javascript HTML5画布游戏中的实体对象-can';不要沿着山顶走,javascript,html,canvas,Javascript,Html,Canvas,正如标题所示,我很难为我的HTML5画布游戏创建一个实体对象 到目前为止,我可以阻止玩家进入盒子的左右两侧,阻止它跳到盒子上(玩家撞到盒子底部并摔倒),但我似乎无法让玩家站着/沿着顶部行走,它只是不断地向上或向下浮动(取决于我给它的+或-数字来阻止它进入盒子) 玩家还需要能够在物体顶部跳上/跳下 以下是我迄今为止为此编写的代码: function collides(player, platform){ //return !(pl

正如标题所示,我很难为我的HTML5画布游戏创建一个实体对象

到目前为止,我可以阻止玩家进入盒子的左右两侧,阻止它跳到盒子上(玩家撞到盒子底部并摔倒),但我似乎无法让玩家站着/沿着顶部行走,它只是不断地向上或向下浮动(取决于我给它的+或-数字来阻止它进入盒子)

玩家还需要能够在物体顶部跳上/跳下

以下是我迄今为止为此编写的代码:

function collides(player, platform){                 

                //return !(player.x > platform.x + platform.width || player.x + player.width  < platform.x || player.y > platform.y + platform.height || player.y + player.height < platform.y);
                if(player.x + player.width > platform.x && !(player.x > platform.x + platform.width) && !(player.y + player.height < platform.y) && !(player.y > platform.y + platform.height)){
                    player.velocity_x = -1;
                }
                if(player.x < platform.x + platform.width && !(player.x < platform.x)  && !(player.y + player.height < platform.y) && !(player.y > platform.y + platform.height)){
                    player.velocity_x = 1;
                }
                if(player.y + player.height < platform.y && player.x + player.width > platform.x && player.x < platform.x + platform.width && (player.y < platform.y + platform.height)){
                   //player.y = platform.y -41;
                    player.velocity_y = -1;
                   //jumping = false;
                }
                if(player.y < platform.y + platform.height && player.x + player.width > platform.x && player.x < platform.x + platform.width && !(player.y + player.height < platform.y)){
                    player.velocity_y = 1;
                }
            }
函数冲突(玩家、平台){
//返回!(player.x>platform.x+platform.width | player.x+player.widthplatform.y+platform.height | player.y+player.heightplayer.x&&!(player.x>platform.x+platform.width)&&!(player.y+player.heightplatform.y+platform.height)){
player.velocity_x=-1;
}
如果(player.xplatform.y+platform.height)){
player.velocity_x=1;
}
if(player.y+player.heightplatform.x&&player.xplatform.x&&player.x
只是澄清一下,您的轴指向哪个方向?是正y向下还是正y向上?我猜正x是对的right@TheCrzyMan正y在下降,正X在右边,你应该处理第五种情况:“英雄”在平台的正确距离,它的y速度为零。(除非有跳跃)下一个问题。为什么
jumping=false
被引用?它不是为了告诉某种物理逻辑它不应该掉下来吗?@游戏炼金术士你能告诉我更多关于第五个案例的情况吗?如果它被满足了,应该怎么做?