基于touchmove事件模拟水果忍者

122 篇文章 0 订阅
100 篇文章 0 订阅

在前面一篇博文中,我曾经说过可以使用移动JS的touchmove事件在移动浏览器中模拟水果忍者的实现。这几天我尝试写了一段代码,成功实现了这一效果。

代码如下:

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值