This is part 7 in a series on making games with impact.js game library. For a list of the first 6 parts, scroll to the bottom of this post.
So far, we’ve set up our computer to have a local server and created a background layer. That’s the most boring possible game ever. It just sits there and does nothing.
You are going to need a playable character and as a result you are going to need to code a minimum of two scripts – a player.js and main.js. There are other scripts that impact creates for you, like the levels, so we’ll worry about those later. You also are going to need a sprite sheet, like the one below. This is something you need to provide for yourself.
Your impact library came with a folder named lib and inside that is a folder named game and in THAT folder is one named entities. Put your player.js script there. Today, we’re going to go through about half of the player.js code and I’ll explain the other half in the next post.
// The impact games module (ig) player entity requires the impact entity.js file ;
// That comes with your impact library, all you need to do is require it as shown. Now you are going to extend that entity ;
ig.module('game.entities.player')
.requires('impact.entity')
.defines(function () {
// You MUST name your entity the same as your javascript file name, but capitalized and preceded by the ;
// word Entity, also capitalized. So, player.js is EntityPlayer ;
EntityPlayer = ig.Entity.extend({
// Next, define the attributes, like the length (x) and height (y) of your character in pixels ;
// You can also give it health points, which it can lose when it hits something or gain when it eats ;
size: {x:75, y:100} ,
health: 100 ,
// Collides specifies what happens when your player runs into another entity;
// There are rabbits and berries in this game and I don't want the player bouncing off of them;
// Impact can be fixed, active, passive or lite;
// Fixed colliders stay where they are when they run into another entity ;
// We'll see later that the rabbits run away from my hunter - as they should ;
collides: ig.Entity.COLLIDES.FIXED,
// There are two types of entities, A and B , my player is an A ;
// When my player runs into a B entity it will check what it is supposed to do;
// This will vary by the entity - if a berry, it will gain health points. A buffalo? It dies ;
type: ig.Entity.TYPE.A,
checkAgainst:ig.Entity.TYPE.B,
// The name of this entity is the same as the Javascript file. Don't get cute here ;
// Name the animation sheet exactly that animSheet ;
name: "player",
animSheet: new ig.AnimationSheet('media/hunter_sprite2.png',75,100),
// Notice that all of these things - name, type, checkAgainst, etc. are properties of an impact game entity ;
// You need those exact names with that exact capitalization and notice that a , is required between properties ;
// THAT’S IT FOR TODAY. NEXT POST — FUNCTIONS! ;
init: function(x,y,settings){
this.parent(x,y, settings);
this.walksound = new ig.Sound(‘media/music/walksound.ogg’) ;
this.walksound_status = false ;
this.walksound.volume = 1 ;
this.addAnim(‘idle’,1,[0]);
this.addAnim(‘down’,0.1,[0,1,2,3,2,1,0]);
this.addAnim(‘left’,0.1,[4,5,6,7,6,5,4]);
this.addAnim(‘right’,0.1,[8,9,10,11,10,9,8]);
this.addAnim(‘up’,0.1,[12,13,14,15,14,13,12]);
if (this.direction == ‘right’){
this.vel.y = 0 ;
}
else if (this.direction == ‘left’){
this.vel.y = 0 ;
}
else if (this.direction == ‘up’){
this.vel.x = 0 ;
}
else if (this.direction == ‘down’){
this.vel.x = 0 ;
}
} ,
kill: function(){
ig.game.gameOver() ;
} ,
update: function(){
this.parent() ;
if(ig.input.state(‘up’)){
this.vel.y = -200;
this.currentAnim = this.anims.up;
this.lastPressed = ‘up’;
}
else if(ig.input.state(‘down’)){
this.vel.y = 200;
this.currentAnim = this.anims.down;
this.lastPressed = ‘down’;
}
else if(ig.input.state(‘left’)){
this.vel.x = -200;
this.currentAnim = this.anims.left;
this.lastPressed = ‘left’;
}
else if(ig.input.state(‘right’)){
this.vel.x = 200;
this.currentAnim = this.anims.right;
this.lastPressed = ‘right’;
}
else if ( ig.input.pressed(‘jump’) ) {
this.pos.y = (this.pos.y -= 200) > 0 ? this.pos.y – 200 : 0 ;
}
else {
this.vel.y = 0;
this.vel.x = 0;
this.currentAnim = this.anims.idle;
}
if (this.pos.y < 0) {this.pos.y = 10}
if (ig.input.pressed(‘attack’)){
ig.game.spawnEntity(‘EntitySnare’,this.pos.x,this.pos.y,{direction:this.lastPressed});
}
}
});
});
If this is just the intermission between levels we’re working on. Imagine how cool the whole Fish Lake game is? Available for Mac and Windows
If you missed the first six posts in this series, you can find them here:
Part 3 – Firing up the world master
Part 4 – Permission to make games