求教各位大神,我想将收集星星的程序改为自动收集星星时不知怎么的玩家就卡边上不动了。
程序是有操控状态跟随机状态的,随机状态下是玩家自动随一定轨迹移动,遇到星星则收集,然后我增加了一段自动收集星星的代码后就卡住了。不知是代码哪里出错了,求指正,谢谢。
状态如下:
添加自动收集代码前
上传中…
添加自动收集代码后
上传中…
上传中…
代码如下:
//随机移动
sjyd:function(){
this.dqjd=this.node.getRotation();
this.dqhd=this.dqjd/180*Math.PI;
//目标食物(star)组
var gswpos=Grobal.swpos;
this.bjdpos=this.node.position;
//自动捕捉
if(gswpos.length>0){
var swpos=gswpos[0].position;
if((swpos.x>Grobal.back.width/2)||(swpos.x<-Grobal.back.width/2)||(swpos.y>Grobal.back.height/2)||(swpos<-Grobal.height/2)){
gswpos.shift();
return;
}
this.dqswpos=swpos;
var lzcj=cc.pSub(this.dqswpos,this.bjdpos);
this.lzjl=cc.pDistance(this.dqswpos,this.node.position)
this.dqhd = Math.atan2(lzcj.x,lzcj.y);
this.dqjd =this.dqhd*(180/Math.PI);
this.node.setRotation(this.dqjd);
this.lzjl=cc.pDistance(this.node.position,this.dqswpos);
if(gswpos.lenght<=0){
this.dqjd=cc.randomMinus1To1()*88;
}
}
//正常移动捕捉
if(this.node.x>=Grobal.back.width/2){
this.node.x=Grobal.back.width/2;
this.xzjd=-this.dqjd;
this.xzhd=(this.xzjd)/180*Math.PI;
}
if(this.node.x<=-Grobal.back.width/2){
this.node.x=-Grobal.back.width/2;
this.xzjd=-this.dqjd;
this.xzhd=(this.xzjd)/180*Math.PI;
}
if(this.node.y>=Grobal.back.height/2){
this.node.y=Grobal.back.height/2;
if(this.dqjd>=0){
this.xzjd=180-this.dqjd;
}else if(this.dqjd<=0) {this.xzjd=(-180-(this.dqjd));}
if(this.dqjd===180||this.dqjd===0||this.xzjd===180||this.xzjd===0||this.dqjd===90||this.xzjd===90){
this.xzjd=cc.randomMinus1To1()*88;
}
this.xzhd=(this.xzjd)/180*Math.PI;
}
if(this.node.y<=-Grobal.back.height/2){
this.node.y=-Grobal.back.height/2;
if(this.dqjd>=0){
this.xzjd=180-this.dqjd;
}
else if(this.dqjd<=0){this.xzjd=(-180-(this.dqjd));}
if(this.dqjd===180||this.dqjd===0||this.xzjd===180||this.xzjd===0||this.dqjd===90||this.xzjd===90){
this.xzjd=cc.randomMinus1To1()*88;
}
this.xzhd=(this.xzjd)/180*Math.PI;
}
if(!this.xzjd) { this.node.setRotation(this.dqjd);
this.node.x += this.speed * Math.sin(this.dqhd);
this.node.y += this.speed * Math.cos(this.dqhd);
}
if(this.xzjd){
if(this.dqjd===this.xzjd){
this.node.setRotation(this.dqjd);
this.node.x += this.speed * Math.sin(this.dqhd);
this.node.y += this.speed * Math.cos(this.dqhd);
}else if(this.dqjd!==this.xzjd){
this.node.setRotation(this.xzjd);
this.node.x += this.speed * Math.sin(this.xzhd);
this.node.y += this.speed * Math.cos(this.xzhd);
}
}
},