Не проходит проверка if.
Здравствуйте!
Есть вот такой код: var target = { nodes: [], add: function (x, y, w, h, c) { var tmp = new _Target(x, y, w, h, c); this.nodes.push(tmp); }, create: function (map) { var dX = Math.ceil(width / 6), dY = Math.ceil(height / 6); for (var t1 = 0; t1 < 6; t1++) { if (t1 % 2 == 0) continue; for (var t2 = 0; t2 < 6; t2++) { if (t2 % 2 == 0) continue; if (t2 == 3 && t1 == 3) continue; var dx = dX * t1, dy = dY * t2; this.add(dx - map.width / 2, dy - map.height / 2, map.width, map.height, this.color); } } }, init: function (c) { this.color = c; }, draw: function () { for (en in this.nodes) { this.nodes[en].draw(); } }, }; var _Target = function (x, y, w, h, c) { this.x = x; this.y = y; this.width = w; this.height = h; this.color = c; }; _Target.prototype.draw = function () { drawRect(this.x, this.y, this.width, this.height, this.color); }; var map = { color: randomColor(), width: 30, height: 30, tiles: [ [1, 0, 0], [1, 0, 1], [0, 0, 1] ] }; // ВОТ ТУТ НЕ ПРОХОДИТ ПРОВЕРКА! for (var i1 in map.tiles) { for (var i2 in map.tiles[1]) { var tile = map.tiles[i1][i2]; if (tile == 0) target.init(map.color); if (tile == 1) target.init(player.color); } }; Проблема в том, что проверки нет, всегда выполняется только первое условие. Рисуются квадраты одинакового цвета, а надо определение по цифрам из массива tiles. Ещё так пробовал: create: function (map) { var dX = Math.ceil(width / 6), dY = Math.ceil(height / 6); for (var t1 = 0; t1 < 6; t1++) { if (t1 % 2 == 0) continue; for (var t2 = 0; t2 < 6; t2++) { if (t2 % 2 == 0) continue; if (t2 == 3 && t1 == 3) continue; for (var i1 in map.tiles) { for (var i2 in map.tiles[1]) { var tile = map.tiles[i1][i2]; var dx = dX * t1, dy = dY * t2; if (tile == 1) { this.add(dx - map.width / 2, dy - map.height / 2, map.width, map.height, map.color); } if (tile == 0) { this.add(dx - map.width / 2, dy - map.height / 2, map.width, map.height, player.color); } } } } } }, Так же выполняется только первое условие и никогда второе. :cray: Вот функция рандомколор: var randomColor = function () { var colors = ['red', 'blue', 'black', 'white', 'green', 'yellow', 'purple']; var rndColor = Math.floor(Math.random() * colors.length); return colors[rndColor]; }; Хотя она и не нужна, по большому счёту. |
Цитата:
|
Цитата:
Так: for (var i1 = 0; i1 < map.tiles.length; i1++) { for (var i2; i2 < map.tiles[1].length; i2++) { var tile = map.tiles[i1][i2]; if (tile == 0) target.init(map.color); if (tile == 1) target.init(player.color); } }; Всё то же самое, если, допустим, ставлю в map просто "" будет выполнятся только второе условие. |
drakulawz,
строка 2 очень странная и что такое player.color? |
Сейчас вот так сделал:
var map = { color: randomColor(), width: 30, height: 30, tiles: [ [1, 0, 0], [1, 0, 1], [0, 0, 1] ] }; for (var i1 = 0; i1 < map.tiles.length; i1++) { for (var i2; i2 < map.tiles[1].length; i2++) { var tile = map.tiles[i1][i2]; if (tile == 0) target.init(map.color); if (tile == 1) target.init(player.color); } }; create: function (map) { var dX = Math.ceil(width / 6), dY = Math.ceil(height / 6); for (var t1 = 0; t1 < 6; t1++) { if (t1 % 2 == 0) continue; for (var t2 = 0; t2 < 6; t2++) { if (t2 % 2 == 0) continue; if (t2 == 3 && t1 == 3) continue; var dx = dX * t1, dy = dY * t2; this.add(dx - map.width / 2, dy - map.height / 2, map.width, map.height, this.color); //вот тут меняю this на map появляются рисунки, ставлю randomColor() рисунки есть и разноцветные но, опять таки не по цифрам, пишу player естественно они все цвета плеера но то условие через цикл не работает... } } }, Квадраты вообще пропали.:-? |
Цитата:
var player = { level: 1, hp: 3, width: 50, height: 50, x: 320, y: 240, step: 2, score: 0, color: randomColor(), draw: function () { drawRect(this.x, this.y, this.width, this.height, this.color); }, move: function () { if (isKeyDown('UP')) { this.y = Math.max(this.y - this.step, 0); } if (isKeyDown('DOWN')) { this.y = Math.min(this.y + this.step, height - this.height); } if (isKeyDown('LEFT')) { this.x = Math.max(this.x - this.step, 0); } if (isKeyDown('RIGHT')) { this.x = Math.min(this.x + this.step, width - this.width); } }, init: function (x, y) { this.x = x; this.y = y; }, collision: function () { for (var i in target.nodes) { var enemy = target.nodes[i]; if (isCollision(this.x, this.y, this.width, this.height, enemy.x, enemy.y, enemy.width, enemy.height)) { target.destroy(i); } } } }; |
Цитата:
|
drakulawz,
for (var i1 = 0; i1 < map.tiles.length; i1++) { for (var i2 = 0; i2 < map.tiles[i1].length; i2++) { var tile = map.tiles[i1][i2]; if (tile == 0) target.init(map.color); if (tile == 1) target.init(player.color); } }; |
Вот ещё порядок выполнения скриптов с комментариями, что да как, может я здесь что-то не так подключил:
<script src="scripts/jquery-3.3.1.js"></script> //для полной загрузки страницы, что бы выполнялся скрипт: scripts/script.js; <script src="scripts/engine.js"></script> //здесь выполняется игровой цикл, ничего, напрямую связанного с проблемой; <script src="scripts/key.js"></script> //здесь функции движения игрока; <script src="scripts/graph.js"></script> // глобальные переменные и функции по отрисовке всех "фигур", здесь так же находится функция randomColor; <script src="scripts/area.js"></script> //просто разметка канваса на поля; <script src="scripts/player.js"></script> //выше скинул весь (пока) скрипт; <script src="scripts/target.js"></script> //по сути, так же, весь скрипт скинул выше (самый первый); <script src="scripts/script.js"></script> //здесь производится инициализация всех функций из выше подключённых скриптов и объект map так же здесь. |
Цитата:
|
Часовой пояс GMT +3, время: 17:45. |