var mosters_raw = [{ width: 600, height: 400, img: "moaster2.png", code: "globe", nx: 12, ny: 8, px: 3, py: 4 }, { width: 576, height: 384, img: "moster1.png", code: "globe", nx: 12, ny: 8, px: 3, py: 4 }]; var mosters = mosters_raw.map(function (x) { let aux = { up: getX(x, 3, 0, 0), down: getX(x, 0, 0, 0), left: getX(x, 1, 0, 0), right: getX(x, 2, 0, 0) } let res = { stop: aux, run: aux, attack: aux }; return res; }); function getX(moster, state, mosterx, mostery) { var width = moster.width / moster.nx; var height = moster.height / moster.ny; var result = []; for (let i = 0; i <= moster.px - 1; i++) { result.push(new PIXI.Rectangle((i + (moster.px * mosterx)) * width, (state + (moster.py * mostery)) * height, width, height)); } result.push(new PIXI.Rectangle((1 + (moster.px * mosterx)) * width, (state + (moster.py * mostery)) * height, width, height)); return result; }