Browserify friendly sprite loader compatible with BrowserQuest sprite format
var loader = require'spritey/loader';var crel = require'crel';var Sprite = require'spritey/sprite';var sprites =require'../sprite/clotharmor.json'require'../sprite/sword1.json'require'../sprite/sword2.json'require'../sprite/axe.json'require'../sprite/goblin.json'require'../sprite/deathknight.json'require'../sprite/crab.json'maploader'sprite/2' scale: 2 stance: 'idle_right' ;var objects = ;var canvas = crel'canvas' width: 500 height: 500 ;var context = canvasgetContext'2d';var shell = require'game-shell';var spawnTimer;clearTimeoutspawnTimer;spawnTimer = setTimeoutobjectspush// clone the sprite so each can have different framessprite: sprites0 sprites2x: 0y: 48;50;shellbind'spawn' 'space';shellbind'walk_left' 'left' 'A'shellbind'walk_right' 'right' 'D'shellbind'walk_up' 'up' 'W'shellbind'walk_down' 'down' 'S'shellon'init'console.log'initialized';shellelementappendChildcanvas;;shellon'tick'var active = objectsobjectslength - 1;if shellwasDown'spawn'spawn;if ! activereturn;// clear the canvas, inefficient, but well it's a democontextclearRect0 0 canvaswidth canvasheight;if shellwasDown'walk_left'activex -=1;activespritewalk_left;if shellwasDown'walk_right'activex += 1;activespritewalk_right;if shellwasDown'walk_up'activey -= 1;activespritewalk_up;if shellwasDown'walk_down'activey += 1;activespritewalk_down;objectsforEachobjspritedrawcontext objx objy;;;spawn;
You can run this example using beefy:
git clonecd spriteynpm installbeefy examples/loader.js
It's important to understand that BrowserQuest sprites are designed to be used within a tiling 2d map engine, and thus have been designed to work with various tile sizes. As outlined in the BrowserQuest wiki, generally three versions of the sprites have been created:
The loader example above demonstrates loading images for the
2 series images
which are designed to align against 32x32 map tiles.
var loader = require'spritey/loader';
var Sprite = require'spritey/Sprite';
Copyright (c) 2014 Damon Oehlman email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.