Witam
Mam problem mam nadzieję że ktoś z was mógłby mi pomóc. Otóż mam prostą platformówkę ale mam problem z kolizjami wykrywa kolizje dla X ale dla Y już nie (IMG:
style_emoticons/default/sad.gif) . Więc wchodzi przez ściany.
Załączam kod.
<body onkeydown="keyDown(event)" onkeyup="keyUp(event)" ></body> <canvas id="graphics" width="800" height="600" style="position: absolute; top: 0; left: 0; background: red;"></canvas>
// VARIBLES
var gameCanvas = document.getElementById("graphics");
var grafx = gameCanvas.getContext("2d"); //or gameCanvas
var player = new object("mario_right.png", 300, 100, 50, 100);
var block = [];
function build(x, y, xStart, yStart) {
for (var z = 0; z < y; z++) {
xStart2 = xStart
for (var i = 0; i < x; i++) {
xStart += 50;
block[block.length] = new object("block.jpg", xStart, yStart, 50, 50);
}
xStart = xStart2;
yStart += 50;
}
}
build(5, 8, 350, 300);
build(5, 5, 0, 400);
var isLeft = false;
var isRight = false;
var prevKey = false;
var isSpace = false;
player.gravity = 20;
player.weight = 0.1;
//EVENTS
function keyDown(e) {
if (String.fromCharCode(e.keyCode) === "%")
isLeft = true; // player.x -= 5;
if (String.fromCharCode(e.keyCode) === "'")
isRight = true; // player.x += 5;
if (String.fromCharCode(e.keyCode) === " ")
isSpace = true; // player.x += 5;
}
function keyUp(e) {
if (String.fromCharCode(e.keyCode) === "%")
isLeft = false; // player.x -= 5;
if (String.fromCharCode(e.keyCode) === "'")
isRight = false; // player.x += 5;
if (String.fromCharCode(e.keyCode) === " ")
isSpace = false; // player.x += 5;
}
// Main Loop
mainLoop();
function mainLoop() {
// PRE VARRIABLE ADJUSTMENTS
//for(var i=0 ; i<=maxBlock ; i++)
// block[i].x += -player.Velocity_x;
for (var i = 0; i < block.length; i++) {
block[i].x += -player.Velocity_x;
//player.x += player.Velocity_x;
}
player.y += player.Velocity_y;
// LOGIC
if (isLeft)
player.Velocity_x = -5;
if (isRight)
player.Velocity_x = 5;
if (!isLeft && !isRight)
player.Velocity_x = 0;
if (player.Velocity_y < player.gravity)
player.Velocity_y += player.weight;
for (var i = 0; i < block.length; i++) {
if (player.isColliding(block[i]) && player.y + player.height < block[i].y + player.Velocity_y) {
player.y = block[i].y - player.height;
player.Velocity_y = 0;
}
}
if (isSpace && player.Velocity_y === 0) {
player.Velocity_y = -5;
}
if (player.y > 700) {
alert('Game Over');
player.y = 100;
player.x = 100;
}
// IMAGE CHANGE
if ((isRight && !isLeft) || (!isRight && isLeft)) { // by postac nie znikala
if (isLeft && prevKey === false) {
player.Sprite.src = "mario_left.png";
prevKey = true;
}
if (isRight && prevKey === true) {
player.Sprite.src = "mario_right.png";
prevKey = false;
}
}
// POST VARRIABLE ADJUSTMENTS
// RENDERING
// czyszczenie obrazu
grafx.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
// rysowanie postaci
grafx.drawImage(player.Sprite, player.x, player.y);
// rysowanie blokow
for (var i = 0; i < block.length; i++) {
grafx.drawImage(block[i].Sprite, block[i].x, block[i].y);
}
setTimeout(mainLoop, 1000 / 60);
}
function object(img, x, y, width, height) {
this.Sprite = new Image();
this.Sprite.src = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.Previous_x;
this.Previous_y;
this.Velocity_x = 0;
this.Velocity_y = 0;
this.gravity = 0;
this.weight = 0;
// wytworzenie kolizji z obiektami. <---------------------- KOLIZJE
this.isColliding = function(obj) {
if (this.x > obj.x + obj.width)
return false;
if (this.x + this.width < obj.x)
return false;
if (this.y > obj.y + obj.height)
return false;
if (this.y + this.height < obj.y)
return false;
return true;
};
}