基于touchmove事件模拟水果忍者

本文介绍如何使用移动JS的touchmove事件在移动浏览器中实现水果忍者的功能,通过编写一段代码成功模拟游戏效果。
  1. 在前面一篇博文中,我曾经说过可以使用移动JS的touchmove事件在移动浏览器中模拟水果忍者的实现。这几天我尝试写了一段代码,成功实现了这一效果。
  2. 代码如下
  3. <!DOCTYPE html PUBLIC "-

  4. //W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" /><!-- 锁定页面,禁止放大和缩小 -->  
  9. <title>切水果</title>  
  10. <script type="text/javascript">  
  11. Array.prototype.remove = function(obj) {  
  12.     for (i in this) {  
  13.         if (this[i] === obj) {  
  14.             this.splice(i, 1);  
  15.             break;  
  16.         }  
  17.     }  
  18. }  
  19.   
  20. var colors = new Array("#0f3bf5", "#13ff61", "#ffc000", "0bf1ff", "#720bff", "#000000");  
  21.   
  22. function BasicObject(x, y, order) {  
  23.     this.x = x;  
  24.     this.y = y;  
  25.     this.order = isNaN(order) ? 0 : order;  
  26.       
  27.     this.addTo = function(array) {  
  28.         array.push(this);  
  29.         array.sort(function(a, b) {return a.order - b.order;});  
  30.     }  
  31.       
  32.     this.removeFrom = function(array) {  
  33.         array.remove(this);  
  34.     }  
  35. }  
  36.   
  37. function Fruit(x, y, order) {  
  38.     BasicObject.call(this, x, y, order);  
  39.     this.color = colors[parseInt(Math.random() * 1000) % 7];  
  40.     this.speed = -250;  
  41.     this.r = 30;  
  42.       
  43.     this.draw = function(context) {  
  44.         context.save();  
  45.         context.beginPath();  
  46.         context.fillStyle = this.color;  
  47.         context.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);  
  48.         context.fill();  
  49.         context.restore();  
  50.     }  
  51.       
  52.     this.update = function(context, array, dt) {  
  53.         this.y += this.speed * dt;  
  54.         this.speed += 80 * dt;  
  55.           
  56.         if (this.y > context.canvas.height + this.r) {  
  57.             this.removeFrom(array);  
  58.         }  
  59.     }  
  60. }  
  61. Fruit.prototype = new BasicObject();  
  62.   
  63. function Engin() {  
  64.     var canvas = document.getElementById("canvas");  
  65.     var ctx = canvas.getContext("2d");  
  66.       
  67.     var buffer = document.createElement("canvas");  
  68.     buffer.width = canvas.width;  
  69.     buffer.height = canvas.height;  
  70.     bufCtx = buffer.getContext("2d");  
  71.       
  72.     var list = new Array();  
  73.       
  74.     var ltime = new Date().getTime();  
  75.     const FPS = 30;  
  76.     var timer = 0;  
  77.       
  78.     this.update = function() {  
  79.         var ctime = new Date().getTime();  
  80.         var dt = (ctime - ltime) / 1000;  
  81.         ltime = ctime;  
  82.           
  83.         ctx.clearRect(0, 0, canvas.width, canvas.height);  
  84.         bufCtx.clearRect(0, 0, buffer.width, buffer.height);  
  85.           
  86.         for (i in list) {  
  87.             if (list[i].update) {  
  88.                 list[i].update(bufCtx, list, dt);  
  89.             }  
  90.         }  
  91.           
  92.         for (i in list) {  
  93.             if (list[i].draw) {  
  94.                 list[i].draw(bufCtx);  
  95.             }  
  96.         }  
  97.           
  98.         timer += dt;  
  99.         if (timer > 1) {  
  100.             new Fruit((Math.random() * 1000) % canvas.width, canvas.height + 6).addTo(list);  
  101.             timer = 0;  
  102.         }  
  103.           
  104.         ctx.drawImage(buffer, 0, 0);  
  105.     }  
  106.       
  107.     this.start = function() {  
  108.         document.addEventListener("touchmove", this.cut, false); // 注册touch move事件处理方法为this.cut  
  109.         setInterval(this.update, 1000 / FPS);  
  110.     }  
  111.       
  112.     this.cut = function(event) {  
  113.         event.preventDefault(); // 禁止浏览器默认touch move事件,一般为页面拖拽  
  114.         // 获取当前手指位置  
  115.         var x = event.changedTouches[0].pageX - 4;  
  116.         var y = event.changedTouches[0].pageY - 4;  
  117.         // 与数组中的水果对象逐一比对,手指位置如果在水果范围内,则水果被切到,标记为红色  
  118.         for (var i = 0; i < list.length; i++) {  
  119.             if ((list[i].x - x) * (list[i].x - x) + (list[i].y - y) * (list[i].y - y) < list[i].r * list[i].r) {  
  120.                 list[i].color = "#e61717";  
  121.             }  
  122.         }  
  123.     }  
  124. }  
  125.   
  126. window.onload = function() {  
  127.     new Engin().start();  
  128. }  
  129. </script>  
  130. </head>  
  131.   
  132. <body>  
  133.     <canvas id="canvas" width="300px" height="450px">  
  134.         <p>Your browser does not support the canvas element!</p>  
  135.     </canvas>  
  136. </body>  
  137. </html>  

代码依旧延续的是我一直使用的canvas处理架构,并且在Engin类中添加了对touchmove事件的处理:当touchmove事件所包含的手指数组中的元素的位置处于当前某个水果范围中时,将该水果标记为红色。由于本人不太擅长美工,所以水果一律用非红色的圆形代替,切到的水果用红色的圆形代替。

以上这段代码可以在Android 2.2以上系统的支持HTML5的浏览器中测试。本人使用的是小米手机(Android 2.3)的默认浏览器进行测试,效果良好,只是在手指快速滑动的时候会有较大的手指坐标的丢失。这是Android系统本身的问题,在本人之前分享的博文(http://select.yeeyan.org/view/213582/202991)中有所提及。相信在iOS中测试的效果会更佳。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值