HTML and CSS Reference
In-Depth Information
stage.bind('gold',gold,'update');
xp = stage.insert(new Q.Stat({ text: "xp", x: Q.width-200, y: 10
}));
stage.bind('xp',xp,'update');
}));
Q.scene('level1',new Q.Scene(function(stage) {
Q.stageScene('hud',1);
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');
});
player.bind('health',stage,function() {
Q.stage(1).trigger('health',player);
});
Q.stage(1).trigger('health',player);
player.bind('gold',stage,function(amount) {
player.p.gold += amount;
Q.stage(1).trigger('gold',{ amount: player.p.gold });
});
player.bind('xp',stage,function(amount) {
player.p.xp += amount;
Q.stage(1).trigger('xp',{ amount: player.p.xp });
});
}));
The level1 scene triggers events on the
hud
scene by calling the
Q.stage(1)
method to trigger the call.
In the
hud
each of the three interface elements are created and then their update methods are bound to events
on the stage.
Keeping the
hud
stage and the main level stage separate from each other makes it easy to reuse the
hud
for
multiple levels and keeps each of the individual pieces smaller.
If you reload the game, you should now accumulate gold and attack random (although all equally difficult)
enemy sprites. See
Figure 13-2
.