index.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="user-scalable=no, width=device-width, minimum-scale=1, maximum-scale=1" />
  6. <title>用画布实现动画</title>
  7. <link rel="stylesheet" type="text/css" href="./static/css/sytle.css">
  8. <script type="text/javascript" src="./static/js/hilo-standalone.min.js"></script>
  9. <img src="./static/images/fish.png" alt="" id="fish" style="display:none">
  10. </head>
  11. <body>
  12. <div id="game-container"></div>
  13. <div style="padding: 20px; text-align: center;">
  14. <button onclick="reset()">重播</button>
  15. </div>
  16. <script type="text/javascript" src="./static/js/demo.js"></script>
  17. <script type="text/javascript">
  18. var stage = new Hilo.Stage({
  19. renderType: renderType,
  20. container: gameContainer,
  21. width: stageWidth,
  22. height: stageHeight
  23. });
  24. //start stage ticker
  25. var ticker = new Hilo.Ticker(60);
  26. ticker.addTick(stage);
  27. ticker.start();
  28. //添加背景
  29. var bmp = new Hilo.Bitmap({
  30. image: './static/images/map.jpg',
  31. rect: [0, 0, stageWidth, stageHeight],
  32. x: 0,
  33. y: 0
  34. }).addTo(stage);
  35. var zidanImg = [];
  36. var zidan = [];
  37. var zidanStep = 0;
  38. for (i = 0; i < 3; i++) {
  39. zidanImg[i] = new Hilo.TextureAtlas({
  40. image: './static/images/zidan.png',
  41. width: 64,
  42. height: 64,
  43. frames: {
  44. frameWidth: 64,
  45. frameHeight: 64,
  46. numFrames: 1
  47. },
  48. sprites: {
  49. zidanImg: { from: 0, to: 0 }
  50. }
  51. });
  52. zidan[i] = new Hilo.Sprite({
  53. frames: zidanImg[i].getSprite('zidanImg'),
  54. x: 10 * (i + 1),
  55. y: 100 + 10 * (i + 1),
  56. interval: 10,
  57. timeBased: true,
  58. loop: true,
  59. onUpdate: function () {
  60. if (this.x < 200) {
  61. // console.log("#debug#🚀 ~ file: index.html:84 ~ init ~ this:", this.style)
  62. if (this.x > stage.width - this.pivotX) {
  63. this.x = 0;
  64. } else {
  65. var paoWuXianRes = paoWuXian(this.x + 3);
  66. // console.log("#debug#🚀 ~ file: index.html:88 ~ init ~ paoWuXianRes:", paoWuXianRes)
  67. this.x = paoWuXianRes.x;
  68. // this.y = paoWuXianRes.y;
  69. //this.x += 3;
  70. }
  71. } else {
  72. manStart = true;
  73. }
  74. }
  75. }).addTo(stage);
  76. //text wrapped in dom element
  77. }
  78. // 子弹
  79. //init texture atlas
  80. var atlas = new Hilo.TextureAtlas({
  81. image: './static/images/man.png',
  82. width: 100,
  83. height: 88,
  84. frames: {
  85. frameWidth: 100,
  86. frameHeight: 88,
  87. numFrames: 1
  88. },
  89. sprites: {
  90. fish: { from: 0, to: 0 }
  91. }
  92. });
  93. var manStart = false;
  94. var fish = new Hilo.Sprite({
  95. frames: atlas.getSprite('fish'),
  96. x: 0,
  97. y: 300,
  98. interval: 6,
  99. timeBased: true,
  100. loop: true,
  101. onUpdate: function () {
  102. if (manStart) {
  103. if (this.x < 200) {
  104. this.x += 3;
  105. this.y -= 3;
  106. }
  107. }
  108. }
  109. }).addTo(stage);
  110. console.log("#debug#🚀 ~ file: index.html:135 ~ fish:", fish)
  111. var man1 = new Hilo.TextureAtlas({
  112. image: './static/images/man1.png',
  113. width: 204,
  114. height: 375,
  115. frames: {
  116. frameWidth: 204,
  117. frameHeight: 375,
  118. numFrames: 13
  119. },
  120. sprites: {
  121. man: { from: 0, to: 12 }
  122. }
  123. });
  124. var man1Sprite = new Hilo.Sprite({
  125. frames: man1.getSprite('man'),
  126. x: 0,
  127. y: 100,
  128. interval: 20,
  129. timeBased: true,
  130. loop: true,
  131. onUpdate: function () {
  132. console.log("#debug#🚀 ~ file: index.html:160 ~ onUpdate:", this.x, this.y)
  133. if (this.x > 200) {
  134. this.x = 0;
  135. }
  136. this.x += 1;
  137. }
  138. }).addTo(stage);
  139. console.log("#debug#🚀 ~ file: index.html:163 ~ man1Sprite:", man1Sprite)
  140. function manStart() {
  141. manStart = true;
  142. }
  143. function reset() {
  144. manStart = false;
  145. for (i = 0; i < zidanImg.length; i++) {
  146. zidan[i].x = 10 * (i + 1);
  147. zidan[i].y = 100 + 10 * (i + 1);
  148. }
  149. fish.x = 0;
  150. fish.y = 300;
  151. }
  152. function paoWuXian(x) {
  153. // var y = 1 * x * x + 1 * x + 2;
  154. return {
  155. x, y: 100
  156. }
  157. }
  158. </script>
  159. </body>
  160. </html>