捕鱼优化实现方案
利用一个定时器 更换图集name
核心代码 buyu.zip (10.1 KB)
app 性能不如 微信浏览器 (各位大大优化下)
var THIS = null;
cc.Class({
extends: cc.Component,
properties: {
tujiflash:cc.SpriteAtlas,
tiaoshu:cc.Prefab,
paodan:cc.SpriteAtlas,
PUTONG:cc.Prefab,
},
// use this for initialization
onLoad: function () {
cc.game.setFrameRate(60);
THIS = this;
this.geduan = 1;
/*鱼画布鱼塘*/
this.YUTANG = cc.find("Canvas/yuzhong");
/**/
this.ZIDAN = cc.find("Canvas/zidanc");
this.BINGDONG = false;
/*炮台的节点*/
this.PAOTAI = [
cc.find("Canvas/paotai/pao1"),
cc.find("Canvas/paotai/pao2"),
cc.find("Canvas/paotai/pao3"),
cc.find("Canvas/paotai/pao4")
];
/*座位*/
this.ZUOWEI = {
1:"0",
2:1,
3:2,
4:3
};
/*画布炮台xy*/
this.PAOXY = [
{x:346,y:-336}, //0
{x:-315,y:-336}, //1
{x:-315,y:336}, //2
{x:346,y:336}, //3
];
this.YUZHENSHU = {
32:11
};
this.UID = 3;
this.ZUOBIAO = this.ZUOWEI[this.UID];
this.FAPAO = false;
/*旋转角度*/
this.chudian = function(event){
var newVec2 = cc.find("Canvas/paotai").convertTouchToNodeSpaceAR(event);
var vec2 = THIS.PAOTAI[THIS.ZUOBIAO] ;
var jiaodu = THIS.getAngle(newVec2.x,newVec2.y,vec2.x,vec2.y);
if(THIS.ZUOBIAO < 2){
if(jiaodu > 0 && jiaodu < 80){
}else if(jiaodu > 278 ){
}else if( jiaodu < 278 && jiaodu > 220){
jiaodu = 278;
}else if(jiaodu > 80){
jiaodu = 80;
}
cc.find("paihaos",vec2).rotation = -jiaodu;
}else{
if(jiaodu >260){
jiaodu= 260;
}else if(jiaodu < 95){
jiaodu= 95;
}
jiaodu -= 180;
cc.find("paihaos",vec2).rotation = jiaodu;
}
THIS.FAPAO = true;
};
cc.find("Canvas/chufa").on('touchstart',this.chudian );
cc.find("Canvas/chufa").on('touchmove',this.chudian );
cc.find("Canvas/chufa").on('touchend',function(event){
THIS.FAPAO = false;
});
//this.jiayu();
/*鱼的动画*/
this.schedule(function() {
this.yudonghua();
}, 0.1);
/*子弹动画快速处理*/
this.schedule(function() {
/*测试加鱼*/
this.jiayu();
if(THIS.FAPAO){
/*开跑数据*/
THIS.kaipao();
}
}, 0.2);
/*x[-640,640] y[-360,360] */
this.schedule(function() {
/*鱼群清理*/
var flash = this.ZIDAN.children;
for(var x = 0; x< flash.length ;x ++){
var yudian = flash[x];
if(yudian){
if(yudian.x > 680 || yudian.x < -680 || yudian.y< -400|| yudian.y > 400){
yudian.destroy();
}
}
}
},0.5);
},kaipao(){
/*开炮*/
var vec2 =THIS.PAOTAI[THIS.ZUOBIAO] ;
this.zidanfei(
{
"uid":this.UID,
"name":"dan1_"+this.weiyischeng(),
"type":5,
"jiaodu":cc.find("paihaos",vec2).rotation
}
);
this.zidanfei(
{
"uid":2,
"name":"dan1_"+this.weiyischeng(),
"type":5,
"jiaodu":cc.find("paihaos",vec2).rotation
}
);this.zidanfei(
{
"uid":1,
"name":"dan1_"+this.weiyischeng(),
"type":5,
"jiaodu":cc.find("paihaos",vec2).rotation
}
);
this.zidanfei(
{
"uid":4,
"name":"dan1_"+this.weiyischeng(),
"type":5,
"jiaodu":cc.find("paihaos",vec2).rotation
}
);
},weiyischeng(){
/*测试唯一生成*/
var timestamp = Date.parse(new Date());
timestamp = timestamp;
return timestamp+parseInt(Math.random()*999 - 1);
},zidanfei(data){
/*创建子弹*/
// console.log(data);
if(data.uid && this.ZUOWEI[data.uid ]){
/*位置编号*/
var weihao = this.ZUOWEI[data.uid ];
var vec2 = this.PAOTAI[weihao];
cc.find("paihaos",vec2).rotation =data.jiaodu;
cc.find("paihaos",vec2).runAction (cc.sequence(
cc.scaleTo(0.05, 1, 0.9),
cc.scaleTo(0.05, 1, 1),
//cc.scaleTo(0.05, 1, 1),
));
/*位置动画xy*/
var weixy = this.PAOXY[weihao];
var zidans = cc.instantiate(this.PUTONG);
zidans.name =(data.name);
zidans.parent = this.ZIDAN;//this.YUTANG;
zidans.type =data.type;
zidans.xunhuan = 1;
zidans.weihao = weihao;
zidans.group = "zidan";
zidans.tag = 0;
zidans.x = weixy.x;
zidans.y = weixy.y;
/* x[-640,640] y[-360,360] */
var jiaodu = data.jiaodu;
if(weihao > 1){
jiaodu = -(data.jiaodu-180);
var hudu = (2*Math.PI / 360) * (-jiaodu-270);
var x= zidans.x + Math.cos(hudu) * 1580;
var y = zidans.y + Math.sin(hudu) * 1580;
zidans.runAction(cc.moveTo(2,x,y ));
}else{
var hudu = (2*Math.PI / 360) * (-jiaodu-270);
var x= zidans.x + Math.cos(hudu) * 1280;
var y = zidans.y + Math.sin(hudu) * 1280;
zidans.runAction(cc.moveTo(1.5,x,y ));
}
zidans.rotation = jiaodu;
}
},getAngle(px,py,mx,my){//获得人物中心和鼠标坐标连线,与y轴正半轴之间的夹角
var x = Math.abs(px-mx);
var y = Math.abs(py-my);
var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
var cos = y/z;
var radina = Math.acos(cos);//用反三角函数求弧度
var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度
if(mx>px&&my>py){//鼠标在第四象限
angle = 180 - angle;
}
if(mx==px&&my>py){//鼠标在y轴负方向上
angle = 180;
}
if(mx>px&&my==py){//鼠标在x轴正方向上
angle = 90;
}
if(mx<px&&my>py){//鼠标在第三象限
angle = 180+angle;
}
if(mx<px&&my==py){//鼠标在x轴负方向
angle = 270;
}
if(mx<px&&my<py){//鼠标在第二象限
angle = 360 - angle;
}
return angle;
},zidandonghua(){
var flash = this.ZIDAN.children;
cc.find("Canvas/label").getComponent(cc.Label).string = " >>> "+flash.length;
for(var mm = 0; mm <flash.length ;mm++ ){
var yu = flash[mm];
var tuid = 11;
if(yu.xunhuan == 1){
tuid = 4;
var xiatu = yu.tag+1;
if(tuid < xiatu){
xiatu = 0;
}
yu.tag = xiatu;
var tiujis = this.paodan._spriteFrames["bullet_"+yu.type+"_"+xiatu];
cc.find("right",yu) .getComponent(cc.Sprite).spriteFrame =
cc.find("left",yu) .getComponent(cc.Sprite).spriteFrame =
cc.find("up",yu) .getComponent(cc.Sprite).spriteFrame =tiujis;
}else{
tuid = 7;
var xiatu = yu.tag+1;
if(tuid < xiatu){
//删除子弹
yu.destroy();
continue;
}
yu.tag = xiatu;
var tiujis = this.paodan._spriteFrames["pnet_e_"+yu.type+"_"+xiatu];
cc.find("right",yu) .getComponent(cc.Sprite).spriteFrame = cc.find("left",yu) .getComponent(cc.Sprite).spriteFrame = cc.find("up",yu) .getComponent(cc.Sprite).spriteFrame =tiujis;
}
}
}, yudonghua(lx){
var flash = this.YUTANG.children;
cc.find("Canvas/label1").getComponent(cc.Label).string = " <<< "+flash.length;
if(this.BINGDONG) return ;
for(var mm = 0; mm <flash.length ;mm++ ){
var yu = flash[mm];
var tuid = 11;
var xiatu = yu.tag +1;
if(tuid < xiatu){
xiatu = 0;
}
yu.tag = xiatu;
var YUTU = cc.find("yuer",yu);
if(YUTU){
YUTU.getComponent(cc.Sprite).spriteFrame = this.tujiflash._spriteFrames["fish_"+yu._name+"_"+xiatu];
}
var yingzi = cc.find("yingzi",yu);
if(yingzi){
yingzi.getComponent(cc.Sprite).spriteFrame = this.tujiflash._spriteFrames["fish_"+yu._name+"_"+xiatu];
}
}
},jiayu(){
/*鱼添加到 鱼塘里面*/
for(var mm = 0; mm < 2 ; mm++ ){
var linshi = parseInt(Math.random()*18);
var preChoumaNode = new cc.Node(linshi);
var ptujis = new cc.Node("yuer");
var sp = ptujis.addComponent(cc.Sprite);
var yingzi = new cc.Node("yingzi");
var spyingzi = yingzi.addComponent(cc.Sprite);
////////
spyingzi.spriteFrame = this.tujiflash._spriteFrames["fish_"+linshi+"_0"];
yingzi.color = cc.Color.BLACK;
yingzi.x = -30;
yingzi.opacity = 80;
yingzi.parent = preChoumaNode;
/////////////
preChoumaNode.group = "yuer";
var buyus = preChoumaNode.addComponent(cc.BoxCollider);
preChoumaNode.addComponent("ColliderListener");
ptujis.parent = preChoumaNode;
preChoumaNode.tag =-1;
preChoumaNode.x = 839;
preChoumaNode.y = -231;
preChoumaNode.rotation =-90;
preChoumaNode.anchorY = 0;
sp.spriteFrame = this.tujiflash._spriteFrames["fish_"+linshi+"_0"];
preChoumaNode.parent = this.YUTANG;
buyus.size.width = ptujis.width/3;
buyus.size.height = ptujis.height/1.5;
buyus.offset = {x:0,y:0};
var y = linshi+parseInt(Math.random()*1600 - 800);;
var times = parseInt(Math.random()*20 + 5);
var finish = cc.callFunc(function(dxx,dsfds){
dsfds.destroy();
}, this,preChoumaNode);
if(mm > 1000) mm = 100;
preChoumaNode.runAction( cc.sequence ( cc.moveTo(16, cc.p(-842, y)) , finish ) );
}
},update(){
this.zidandonghua();
}
/* x[-640,640] y[-360,360] */
});