HTML and CSS Reference
In-Depth Information
this.insert(new Q.Fountain({ x:x, y:y }));
return TILE.FLOOR;
},
monster: function(x,y) {
var frame = Math.floor(Math.random()*64);
this.insert(new Q.Enemy({ x:x, y:y, frame:frame }));
return TILE.FLOOR;
}
});
Although the code is long, each of the sprite methods is the same. It just creates a sprite of the wanted type,
adds it to the stage, and then returns the floor tile that should be underneath the sprite. The
stairs
sprite is
special because it marks the place where the player should start the level, and that starting position is stored in
the
startX
and
startY
properties.
The
unfog
method also deserves a mention. Its job is to take a square of tiles and sprites around the player
and make them visible by unhiding them as the play approaches. This allows the level to be slowly exposed as
the player moves around. This method needs to be triggered in the camera component for it to work, so add the
highlighted line to that component:
Q.register('camera', {
added: function() {
this.entity.bind('moved',this,'track');
},
track: function() {
var p = this.entity.p,
stage = this.entity.parent;
stage.centerOn(p.x, p.y);
stage.level.unfog(p.tileX,p.tileY);
}
});
Next, the spot where the player is created needs to be updated to use the level's
startX
and
startY
posi-
tion. Modify the level1 scene creation method to read
Q.scene('level1',new Q.Scene(function(stage) {
if(Q.width > 600 || Q.height > 600) {
stage.rescale(2);
}
stage.level = stage.insert(new Q.Level("level1.txt",stage));
stage.add('transition');
var player = stage.insert(new Q.Player({ x: stage.level.startX ,
y: stage.level.startY
}));
player.camera.track();
player.bind('removed',stage,function() {
Q.stageScene('level1');
});
}));