updated 修改游戏碰撞事件

This commit is contained in:
郑浩乐 2016-01-08 11:22:01 +08:00
parent 8848cbf001
commit 1401c8c861
2 changed files with 8 additions and 12 deletions

View File

@ -242,10 +242,9 @@ function Game(id,options){
return item; return item;
}; };
//获取对象列表 //获取对象列表
Stage.prototype.getItemsByType = function(){ Stage.prototype.getItemsByType = function(type){
var types = Array.prototype.slice.call(arguments,0);
var items = this.items.filter(function(item){ var items = this.items.filter(function(item){
if(types.indexOf(item.type)>-1){ if(item.type==type){
return item; return item;
} }
}); });

View File

@ -106,16 +106,13 @@
update:function(){ update:function(){
if(this.map){ if(this.map){
var stage = this; var stage = this;
var items = this.getItemsByType(1,2); var player = this.getItemsByType(1)[0];
var hash = {}; //当前对象位置分布 var items = this.getItemsByType(2);
items.forEach(function(item){ items.forEach(function(item){
var key = 'x'+item.coord.x+'y'+item.coord.y; //坐标的标识 var dx = item.x-player.x;
if(hash[key]){ var dy = item.y-player.y;
if(hash[key]!=item.type){ //如果NPC与玩家相遇 if(dx*dx+dy*dy<750){
stage.status = 2; stage.status = 2;
}
}else{
hash[key] = item.type;
} }
}); });
} }