Сообщение от warren buffet
|
Прощаю. Теперь вопрос почему Володька сбрил усы куда ты дел отступы?
Если проблема реальная, попробуй вернуть отступы в исходный код и без помощи эльфов объяснить, что не работает и как работать должно. )
|
Должно работать так: есть координаты объектов (платформ) в массивах map.x,map.y,map.w,map.h. Если между платформой и игроком ЕСТЬ соприкосновение - меняется y персонажа. если нет - он падает. Пока что переделал, и это почти работает(где ошибка и почему "ПОЧТИ" знаю).
<!DOCTYPE html>
<html >
<head>
<canvas id = "app" > Your browser doesnt support the Canvas.Please update it to actual version. </canvas>
</head>
<body>
<script defend src = "js2d.js"></script>
<script defend src = "key.js"></script>
<script defend src = "engine.js"> </script>
<script>
/*load game script*/
'use strict'
var game = {
width: 800,
height: 600,
scene: 'loader'
};
/*in this variable i contian all game configurations*/
/*and load canvas*/
var c = document.getElementById('app');
var ctx = c.getContext('2d');
c.width = game.width;
c.height = game.height;
/*load image*/
var loader_first = new Image();
var loader_second = new Image();
var loader_third = new Image();
loader_first.src = "http://vkapilessons.esy.es/1.png";
loader_second.src = "http://vkapilessons.esy.es/2.png";
loader_third.src = "http://vkapilessons.esy.es/3.png";
var isLoad = false;
/*create the loader*/
if (game.scene == 'loader') {
window.onload = function getLoader() {
ctx.drawImage(loader_first, 0, 0, game.width, game.height);
setTimeout(
function() {
ctx.clearRect(0, 0, game.width, game.height);
ctx.drawImage(loader_second, 0, 0, game.width, game.height);
}, 900);
setTimeout(
function() {
ctx.clearRect(0, 0, game.width, game.height);
ctx.drawImage(loader_third, 0, 0, game.width, game.height);
isLoad = true;
player.draw();
}, 1600);
};
};
/*change gamescene*/
if (isLoad) {
game.scene = 'gamelesson'
};
/*global obj player with player.config*/
var player = {
x: 150,
y: 250,
width: 50,
height: 50,
startX: 150,
endX: 450,
/*player behv.*/
isRight: false,
isLeft: false,
isJump: false,
isDestroy: false,
speed: 1,
draw: function drawPlayer() {
clearCanvas(0, 0, game.width, game.height);
draw(player.x, player.y, player.width, player.height);
map.draw();
},
};
/*in this function we create the player-move*/
function movePlayerLeft() {
clearCanvas(player.x, player.y, player.width, player.height);
player.x -= player.speed;
player.draw()
camera('right');
};
function movePlayerRight() {
clearCanvas(player.x, player.y, player.width, player.height);
player.x += player.speed;
player.draw();
camera('left');
};
var isJump = 0;
function playerJump() {
if (isJump == 0) {
setTimeout(function() {
clearCanvas(player.x, player.y, player.width, player.height);
player.y -= 10;
player.draw();
isJump = 1;
}, 100);
setTimeout(function() {
clearCanvas(player.x, player.y, player.width, player.height);
player.y += 10;
player.draw();
isJump = 0;
}, 300);
};
};
/*big func to clear or draw canvas easy*/
function clearCanvas(x, y, width, height) {
ctx.clearRect(x, y, width, height);
};
function draw(x, y, width, height) {
ctx.fillRect(x, y, width, height);
};
/*in game loop we give keyCode and dofunc*/
startGame(
function() {
if (isKeyDown('D')) {
movePlayerRight();
};
if (isKeyDown('A')) {
movePlayerLeft();
};
if (isKeyDown('LEFT')) {
movePlayerLeft();
};
if (isKeyDown('RIGHT')) {
movePlayerRight();
};
if (isKeyDown('W')) {
if (isJump == 0) {
playerJump();
};
};
if (isKeyDown('UP')) {
if (isJump == 0) {
playerJump();
};
};
if (player.x == 0 || player.x < 0) {
clearCanvas(player.x, player.y, player.width, player.height);
player.x = 0;
draw(player.x, player.y, player.width, player.height);
};
});
var map = {
x: [150, 250, 350],
y: [290, 250, 290],
w: [100, 100, 100],
h: [40, 40, 40],
draw: function() {
for (var i = map.x.length; i != -1; i--) {
ctx.fillRect(map.x[i], map.y[i], map.w[i], map.h[i]);
}
}
};
function camera(place) {
if (place == 'right') {
if (player.x > 0) {
for (var i = map.x.length; i != -1; i--) {
map.x[i]++;
player.draw();
};
} else {
clearCanvas(player.x, player.y, player.width, player.height);
player.x = 0;
draw(player.x, player.y, player.width, player.height);
};
} else {
if (player.x < 630) {
for (var i = map.x.length; i != -1; i--) {
map.x[i]--;
player.draw();
};
} else {
clearCanvas(player.x, player.y, player.width, player.height);
player.x = 630;
draw(player.x, player.y, player.width, player.height);
};
}
};
var trycount = 0;
function collision() {
if (isJump == 0) {
for (var i = 0; i <= map.x.length; i++) {
if (player.x <= map.x[i] + map.w[i] &&
player.x + player.width >= map.x[i] &&
player.y <= map.y[i] + map.h[i] &&
player.height + player.y >= map.y[i]) {
console.log('cd');
i = map.x.lenght + 1;
clearCanvas(player.x, player.y, player.width, player.height);
map.draw();
player.y = map.y[i] - map.h[i] - 10;
draw(player.x, player.y, player.width, player.height);
} else {
trycount++
if (trycount == map.x.length) {
fall();
trycount = 0;
};
};
};
};
};
function fall() {
var fall = setInterval(function() {
clearCanvas(player.x, player.y, player.width, player.height);
player.y += 5
draw(player.x, player.y, player.width, player.height);
map.draw();
if (player.y > 900) {
clearInterval(fall);
};
}, 1);
};
</script>
</body>
</html>