index.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. //舞台画布
  19. var canvasType = location.search.indexOf("dom") != -1 ? "div" : "canvas";
  20. var canvas = Hilo.createElement(canvasType);
  21. console.log("#debug#🚀 ~ file: index.vue:17 ~ init ~ init:开始啊", Hilo);
  22. var stage = new Hilo.Stage({
  23. renderType: renderType,
  24. container: gameContainer,
  25. width: stageWidth,
  26. height: stageHeight
  27. });
  28. console.log("#debug#🚀 ~ file: index.vue:24 ~ init ~ stage:", stage);
  29. var ticker = new Hilo.Ticker(60);
  30. ticker.addTick(stage);
  31. ticker.start();
  32. var font = "14px arial";
  33. var content = "Hello World! Hilo是一款HTML5 2D游戏引擎,欢迎使用。";
  34. var text = new Hilo.Text({
  35. font: font,
  36. text: content,
  37. lineSpacing: 0,
  38. width: 250,
  39. height: 100,
  40. x: 40,
  41. y: 50
  42. }).addTo(stage);
  43. /* var stage = new Hilo.Stage({
  44. renderType: renderType,
  45. container: gameContainer,
  46. width: stageWidth,
  47. height: stageHeight
  48. });
  49. //start stage ticker
  50. var ticker = new Hilo.Ticker(60);
  51. ticker.addTick(stage);
  52. ticker.start();
  53. //添加背景
  54. var bmp = new Hilo.Bitmap({
  55. image: './static/images/map.jpg',
  56. rect: [0, 0, stageWidth, stageHeight],
  57. x: 0,
  58. y: 0
  59. }).addTo(stage);
  60. var zidanImg = [];
  61. var zidan = [];
  62. var zidanStep = 0;
  63. for (i = 0; i < 3; i++) {
  64. zidanImg[i] = new Hilo.TextureAtlas({
  65. image: './static/images/zidan.png',
  66. width: 64,
  67. height: 64,
  68. frames: {
  69. frameWidth: 64,
  70. frameHeight: 64,
  71. numFrames: 1
  72. },
  73. sprites: {
  74. zidanImg: { from: 0, to: 0 }
  75. }
  76. });
  77. zidan[i] = new Hilo.Sprite({
  78. frames: zidanImg[i].getSprite('zidanImg'),
  79. x: 10 * (i + 1),
  80. y: 100 + 10 * (i + 1),
  81. interval: 10,
  82. timeBased: true,
  83. loop: true,
  84. onUpdate: function () {
  85. if (this.x < 200) {
  86. // console.log("#debug#🚀 ~ file: index.html:84 ~ init ~ this:", this.style)
  87. if (this.x > stage.width - this.pivotX) {
  88. this.x = 0;
  89. } else {
  90. var paoWuXianRes = paoWuXian(this.x + 3);
  91. // console.log("#debug#🚀 ~ file: index.html:88 ~ init ~ paoWuXianRes:", paoWuXianRes)
  92. this.x = paoWuXianRes.x;
  93. // this.y = paoWuXianRes.y;
  94. //this.x += 3;
  95. }
  96. } else {
  97. manStart = true;
  98. }
  99. }
  100. }).addTo(stage);
  101. //text wrapped in dom element
  102. }
  103. // 子弹
  104. //init texture atlas
  105. var atlas = new Hilo.TextureAtlas({
  106. image: './static/images/man.png',
  107. width: 100,
  108. height: 88,
  109. frames: {
  110. frameWidth: 100,
  111. frameHeight: 88,
  112. numFrames: 1
  113. },
  114. sprites: {
  115. fish: { from: 0, to: 0 }
  116. }
  117. });
  118. var manStart = false;
  119. var fish = new Hilo.Sprite({
  120. frames: atlas.getSprite('fish'),
  121. x: 0,
  122. y: 300,
  123. interval: 6,
  124. timeBased: true,
  125. loop: true,
  126. onUpdate: function () {
  127. if (manStart) {
  128. if (this.x < 200) {
  129. this.x += 3;
  130. this.y -= 3;
  131. }
  132. }
  133. }
  134. }).addTo(stage);
  135. function manStart() {
  136. manStart = true;
  137. }
  138. function reset() {
  139. manStart = false;
  140. for (i = 0; i < zidanImg.length; i++) {
  141. zidan[i].x = 10 * (i + 1);
  142. zidan[i].y = 100 + 10 * (i + 1);
  143. }
  144. fish.x = 0;
  145. fish.y = 300;
  146. }
  147. function paoWuXian(x) {
  148. // var y = 1 * x * x + 1 * x + 2;
  149. return {
  150. x, y: 100
  151. }
  152. } */
  153. </script>
  154. </body>
  155. </html>