Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
var loaded = 0;
var allImgs = 3;
function imgonload() {
loaded++;
if (loaded == allImgs) {
afterLoad();
}
}
var img1 = new Image();
img1.src = 'img1.png';
img1.onload = imgonload;
var img2 = new Image();
img2.src = 'img2.png';
img2.onload = imgonload;
var img3 = new Image();
img3.src = 'img3.png';
img3.onload = imgonload;
var spr1, spr2, spr3;
function afterLoad() {
spr1 = new Sprite(img1, 3);
spr2 = new Sprite(img2, 3);
spr3 = new Sprite(img3, 3);
}
images: [
{ url: 'images/en_blue.png' },
{ url: 'images/en_white.png' },
{ url: 'images/en_red.png' }
],
loadCount: 0,
load: function() {
var self = this,
max = this.images.length;
for (var i = max; i--;) {
this.images[i].img = new Image();
this.images[i].img.onload = function () {
self.loadCount++;
if (self.loadCount == max) {
self.loadComplete();
}
}
this.images[i].img.src = this.images[i].url;
}
}
new function () {
var engine, app, element, mouse;
LibCanvas.extract();
engine = new TileEngine({
size: new Size(49, 9),
cellSize: new Size(7, 7),
cellMargin: new Size(1, 1),
defaultValue: 1
}).setMethod({
1 : '#000', 2 : '#300', 3 : '#009', 4 : '#060',
5 : '#fff', 6 : '#ff3', 7 : '#f9f', 8 : '#3ff'
});
[ ' @ @ @@@ @@@ @@ @ @ @ @ @@ @@@ '
, ' @ @ @ @ @ @ @ @ @@ @ @ @ @ @ @ '
, ' @ @ @@@ @ @@@@ @ @ @ @ @ @@@@ @@ '
, ' @ @ @ @ @ @ @ @ @ @@ @ @ @ @ @ '
, ' @@@ @ @@@ @@@ @ @ @ @ @ @ @ @@@ '
].forEach(function (line, y) {
if (line.indexOf('@') == -1) return;
for (var x = line.length; x--;) if (line[x] == '@') {
engine.getCellByIndex(new Point(x, y + 2)).value = 5;
}
});
app = new App({ size: engine.countSize(), simple: true });
element = TileEngine.Element.app( app, engine );
mouse = new Mouse(app.container.bounds);
new App.MouseHandler({ mouse: mouse, app: app })
.subscribe( element );
new TileEngine.Mouse( element, mouse ).events.add({
over: function (cell) {
var
next,
current = cell.value,
source = current < 5 ? [ 1,2,3,4 ] : [ 5,6,7,8 ];
do {
next = source.random;
} while (next == current);
cell.value = next;
},
click: function (cell) {
atom.trace(cell.point);
}
});
};
Создание спрайтового движка