您好所有誰正在閱讀這篇文章 在試圖讓馬里奧類型的遊戲,但我有一些碰撞錯誤 也許有人能向我解釋什麼是不正確的,如何解決它JavaScript的碰撞檢測錯誤
碰撞部分那裏雖然是硬編碼值 我不能正確跳上框很難解釋,如果它不是硬只緩存js小提琴鏈接!
上箭頭的空間控制跳躍問題是跳躍 遊戲邏輯
var hero = {
X: 200,
Y: 450,
gravity: 0.05,
gravitySpeed: 0,
speed: 2,
height: 50,
width: 50
}
var box = {
X: 300,
Y: 450,
height: 50,
width: 50
}
var map = {
gravity: 0.5,
groundY: 350
}
var optiones = {
crashWith: function (otherobj) {
var myleft = hero.X;
var myright = hero.X + (50);
var mytop = hero.Y;
var mybottom = hero.Y + (50);
var otherleft = box.X;
var otherright = box.X + (50);
var othertop = otherobj.y;
var otherbottom = box.Y + (50);
var crash = true;
//Hard coded values
var Y = 450,
X = 300,
w = 50,
h = 50
// not tuching
if ((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright)) {
crash = false;
// console.log("dont tuch")
hero.Y += hero.gravitySpeed;
hero.gravitySpeed += hero.gravity;
} else {
// collision rules
if (mytop >= othertop && othertop < otherbottom) {
if (hero.X <= X) {
hero.X = X - hero.width
} else if (hero.X > X) {
hero.X = X + hero.width
}
} else if (hero.X >= X && hero.X <= X + w) {
if (hero.Y <= Y) {
hero.Y = Y - hero.height
} else if (hero.Y >= Y) {
hero.Y = Y + hero.height
}
}
}
這裏雖然是一個完整的代碼看或播放白衣它JS小提琴 js fiddle
您的代碼段似乎有效,問題究竟是什麼? – DBS
我可以去thro元素當我跳 –
替換框與otherobj –