HTML and CSS Reference
In-Depth Information
This code sets up the engine and the scene, and then it loads some assets and sets up some animations. All
these setup pieces should look familiar from previous chapters and the example earlier in this chapter.
This time the player has a number of animations for each direction, and a second set of simple animations
for the nefarious blob character are added as well.
The three sprites will be added in each of the following sections.
Creating the Enemy
The enemy sprite (aka “the blob”) is just a sprite that moves back and forth on a platform and changes direction
when it runs into a wall. To do this it needs to listen for hit.tile events and flip direction when it encounters
that event. It also needs to damage the player whenever it runs into him, which it can do by listening for the
hit.sprite event.
Replace the stub for the Q.Enemy class with the code in Listing 18-9 .
Listing 18-9: The Q.Enemy class
Q.Enemy = Q.Sprite.extend({
init:function(props) {
this._super(_(props).extend({
sheet: 'blob',
sprite: 'blob',
rate: 1/15,
type: 2,
collisionMask: 5,
health: 50,
speed: 100,
direction: 'left'
}));
this.bind('damage',this,'damage');
this.bind('hit.tile',this,'changeDirection');
this.bind('hit.sprite',this,'hurtPlayer');
this.add('animation, 2d')
.collisionPoints()
},
changeDirection: function(col) {
if(col.direction == 'left') {
this.p.direction = 'right';
} else if(col.direction == 'right') {
this.p.direction = 'left';
}
},
hurtPlayer: function(col) {
if(col.p.x < this.p.x) {
col.p.x -= 10;
col.damage(5);
 
 
Search WWH ::




Custom Search