lufy's legend

标题: LHitList类 [打印本页]

作者: yorhomwang    时间: 2013-5-6 13:39
标题: LHitList类
我看lufylegend差一个功能,那就是返回碰撞列表。
如果加了这个功能就可以搞一个泡泡龙一类的游戏。
因此我帮忙封装了一下,代码如下:
  1. function LHitList(objectA,sprite,type){
  2.         if(!objectA)trace("The first param was not assigned.");
  3.         if(!sprite)trace("The second param was not assigned.");
  4.         if(!type)type = "TestRect";
  5.         var s = this;
  6.         s.objectA = objectA;
  7.         s.objectB = null;
  8.         s.sprite = sprite;
  9.         s.ishit = false;
  10.         s.list = [];
  11.         s.type = type;
  12. }
  13. LHitList.prototype.getList = function(){
  14.         var s = this;
  15.         var n = s.sprite.childList.length;
  16.         if(s.type != "TestRect" && s.type != "TestArc"){       
  17.                 trace("Type is wrong. It must be 'TestRect' or 'TestArc'.");
  18.         }else{
  19.                 while(n--){
  20.                         s.objectB = s.sprite.childList[n];
  21.                         if(s.type == "TestRect"){
  22.                                 s.ishit = LGlobal.hitTest(s.objectA,s.objectB);
  23.                         }else if(s.type == "TestArc"){
  24.                                 s.ishit = LGlobal.hitTestArc(s.objectA,s.objectB);
  25.                         }
  26.                         if(s.ishit == true && s.objectB != s.objectA){
  27.                                 s.list[s.list.length] = s.sprite.childList[n];
  28.                         }
  29.                 }
  30.                 return s.list;
  31.         }
  32. }
  33. LHitList.prototype.clearList = function(){
  34.         var s = this;
  35.         s.list = [];
  36. }
复制代码
用法如下:
LHitList(objectA,sprite,type)

参数:
objectA:碰撞区域A
sprite:一个sprite对象,用它的成员判断是否和碰撞区域A是否碰撞
type:碰撞类型,现在只有'TestRect'和 'TestArc'两种

方法:
getList:取出碰撞列表
clearList:清楚碰撞列表


测试地址:http://www.cnblogs.com/yorhom/articles/3062583.html
作者: 战胜自我    时间: 2014-1-23 11:21


   路过 看看。





欢迎光临 lufy's legend (http://lufylegend.com/forum/) Powered by Discuz! X2.5