Skip to content

Instantly share code, notes, and snippets.

updateSize: function(width, height){
// get current center
var centerX = this.pos.x + this.size.x/2,
centerY = this.pos.y + this.size.y/2;
// set new width / height
this.size.x = width;
this.size.y = height;
MyGame = new ig.Game.extend({
levelToLoad: level1,
// levelName ex: test3
changeLevel: function(levelName){
// prepend and capitalize
var levelObjName = 'Level' + levelName.charAt(0).toUpperCase() + levelName.slice(1),
levelPath = 'game.levels.' + levelName;
delete ig.modules['dom.ready']; // reset dom event handler
var delta = this.lifeSpanTimer.delta();
color.r = Math.floor(delta.map(0, 1, this.startColor.r, this.endColor.r ));
color.g = Math.floor(delta.map(0, 1, this.startColor.g, this.endColor.g ));
color.b = Math.floor(delta.map(0, 1, this.startColor.b, this.endColor.b ));
color.a = Math.floor(delta.map(0, 1, this.startColor.a, this.endColor.a ));
ctx.fillStyle = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ',' + color.a + ')';
// create impact collision listener
var listener = new b2.ContactListener();
listener.Add = callback;
listener.Remove = callback;
listener.Persist = callback;
listener.Result = callback;
// attach to box2d world
ig.BackgroundMap.inject({
drawFull: function(context) {
var ctx = context = context || ig.system.context,
scale = ig.system.scale,
tileWidthScaled = Math.floor(this.tilesize * scale),
tileHeightScaled = Math.floor(this.tilesize * scale),
tile;
ctx.save();
ctx.scale(scale, scale);
for( var y = 0; y < this.height; y++) {
// add tile
ig.game.addItem({
_layer : 'bg',
update: function(){
var mainMap = ig.game.getMapByName('main'),
tileSize = mainMain.tilesize,
ctx = ig.game.bgCanvasContext,
x = tileSize * x * ig.system.scale,
y = tileSize * y * ig.system.scale;
var mX = ig.input.mouse.x,
mY = ig.input.mouse.y,
spawnX = this.pos.x + this.size.x/2,
spawnY = this.pos.y + this.size.y/2,
r = Math.atan2(mY - spawnY, mX - spawnX),
directionX = Math.cos(r) * 50,
directionY = Math.sin(r) * 50;
var distance = this.distanceTo(this.target),
d = this.angleTo(this.target),
steerX = Math.cos(d),
steerY = Math.sin(d),
newX = (this.target.pos.x + distance / this.target.vel.x) - this.pos.x,
newY = (this.target.pos.y + distance / this.target.vel.y) - this.pos.y,
// keep speed 10 or less
speed = Math.min(distance, 30) / 6;
this.vel.x += steerX * speed;
this.vel.y += steerY * speed;
// also tried this slightly different way behaves different but the hard turn problem remains
var mass = 1,
maxSpeed = 1000,
maxSteer = 200,
distance = this.distanceTo(this.target),
d = this.angleTo(this.target),
steerX = Math.cos(d) * this.speed - this.vel.x,
steerY = Math.sin(d) * this.speed - this.vel.y;
setVelocity: function () {
var difX = ((ig.input.mouse.x - (this.size.x / 2)) - this.pos.x).round();
var difY = ((ig.input.mouse.y - (this.size.y / 2)) - this.pos.y).round();
var max = 100,
maxX = (difX > 0)? max : -max,
maxY = (difY > 0)? max : -max;