Skip to content

Instantly share code, notes, and snippets.

@mLautz
Last active December 13, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mLautz/4963830 to your computer and use it in GitHub Desktop.
Save mLautz/4963830 to your computer and use it in GitHub Desktop.
Basic Demo for ImpactJS of screen scrolling with arrow keys. Utilizes the map-size plugin to simplify getting the map size.
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font',
'plugins.map-size',
'game.levels.scrollDemoLevel'
)
.defines(function(){
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
init: function() {
// Initialize your game here; bind keys etc.
ig.input.bind(ig.KEY.UP_ARROW, 'up');
ig.input.bind(ig.KEY.RIGHT_ARROW, 'right');
ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
ig.input.bind(ig.KEY.LEFT_ARROW, 'left');
this.loadLevel(LevelScrollDemoLevel);
this.rightBorder = ig.game.backgroundMaps[0].pxWidth - ig.system.width;
this.bottomBorder = ig.game.backgroundMaps[0].pxHeight - ig.system.height;
this.screen.x = ig.system.width / 2;
this.screen.y = ig.system.height / 2;
},
update: function() {
if(ig.input.state('left') && !this.screen.x<=0){
if(this.screen.x > 30){
this.screen.x -= 30;
}else{
this.screen.x = 0;
}
}
if(ig.input.state('right')){
if(this.screen.x < this.rightBorder - 30){
this.screen.x += 30;
}
else{
this.screen.x = this.rightBorder;
}
}
if(ig.input.state('up') && !this.screen.y<=0){
if(this.screen.y > 30){
this.screen.y -= 30;
}else{
this.screen.y = 0;
}
}
if(ig.input.state('down')){
if(this.screen.y < this.bottomBorder - 30){
this.screen.y += 30;
}
else{
this.screen.y = this.bottomBorder;
}
}
this.parent();
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
}
});
// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 320, 240, 2 );
});
ig.module(
'plugins.map-size'
)
.requires(
'impact.map'
)
.defines(function(){
ig.Map.inject({
//found at http://impactjs.com/forums/help/how-to-find-map-size-in-pixels
init: function( tilesize, data ) {
this.parent( tilesize, data );
this.pxWidth = this.width * tilesize;
this.pxHeight = this.height * tilesize;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment