Skip to content
Newer
Older
100644 73 lines (58 sloc) 1.48 KB
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>jQuery Meetup Game</title>
5 <link rel="stylesheet" href="game.css"/>
6 <script src="../js/jquery.js"></script>
7 <script src="game.js"></script>
8 <script>
9 $(function(){
10 var score = 0, life = 100;
11
1dc786d @jeresig Tweaking the game to use delegate instead of live.
authored
12 $("#game").delegate(".user", "hit", function(){
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
13 var data = $(this).data();
14 data.hits++;
15
16 if ( data.hits >= data.maxhits ) {
1dc786d @jeresig Tweaking the game to use delegate instead of live.
authored
17 $(this).stop().remove();
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
18 } else {
19 $(this).addClass("hit");
20 }
21
22 score += 100;
23 $("#score").text( score );
24 });
25
1dc786d @jeresig Tweaking the game to use delegate instead of live.
authored
26 $("#game").delegate(".user", "escape", function(){
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
27 var data = $(this).data();
28
29 life -= data.maxhits * 3;
30
31 if ( life < 0 ) {
32 life = 0;
33 }
34
35 $("#life").text( life );
36
37 if ( life <= 0 ) {
38 $("#stats").append(" <b class='kill'>You were killed by: " + data.user + "</b>");
39 $("#game").trigger("stop");
40 }
41
1dc786d @jeresig Tweaking the game to use delegate instead of live.
authored
42 $(this).remove();
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
43 });
44
45 $("#game").bind("start", function(){
46 score = 0;
47 life = 100;
48 $("#score").text( score );
49 $("#life").text( life );
50
51 $("input").hide();
52
0d50641 @jeresig Fixed typo in game.
authored
53 $("b.kill").fadeOut(1000, function(){
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
54 $(this).remove();
55 });
56 });
57
58 $("#game").bind("stop", function(){
59 $("input").show();
60 });
1dc786d @jeresig Tweaking the game to use delegate instead of live.
authored
61
62 $("input").click(function(){
63 $("#game").trigger("start");
64 });
4fa25d0 @jeresig Adding in the advanced jQuery talk and demo.
authored
65 });
66 </script>
67 </head>
68 <body>
69 <p id="stats"><input type="button" value="Start"/> Score: <strong id="score">0</strong> Life: <strong id="life">0</strong></p>
70 <div id="game"></div>
71 </body>
72 </html>
Something went wrong with that request. Please try again.