问题好像比较复杂下面贴代码,我实在改不出来,改了一星期,web可以用,用cocos 打包APK就报错
var SCORE=0;
var gameLayer = cc.LayerColor.extend({
checkCollide:false,
length:0,
zx:null,
radius:20,
score:0,
LV:1,
ctor:function (gamesceneinfo) {
//////////////////////////////
// 1. super init first
this._super();
GC.copy(gamesceneinfo,this);
this.allBall=[];
this.playerBall=[];
this.oneP=cc.p(0,0);
GC.setSize(cc.winSize);
var size = cc.winSize;
this.setColor(cc.color(147,147,147,255));//背景色
var score=new cc.LabelTTF("积分 "+SCORE+"","",30);
this.addChild(score);
score.setPosition(GC.w-5,GC.h-15);
score.setAnchorPoint(1,1);
this.score=score;
var lv=new cc.LabelTTF("关卡 "+this.LV+"","",30);
this.addChild(lv);
lv.setPosition(0,GC.h-15);
lv.setAnchorPoint(0,1);
this.lv=lv;
//转轴中心
var zx=this.createBall(this.radius*4);
var zxp=GC.z;
zxp.y+=100;
zx.setPosition(zxp);
var rot= cc.rotateBy(gamesceneinfo.rotationSpeed,360);
var fore= cc.repeatForever(rot);
zx.runAction(fore);
this.addChild(zx);
this.zx=zx;
//关卡文字
var LVL=cc.LabelTTF.create(gamesceneinfo.LV+"","",60);
LVL.setPosition(zx.getPosition());
LVL.setColor(cc.color(0,0,0,255));
this.addChild(LVL);
var y=zxp.y-230;
this.oneP=y;
for(var i=1;i<=gamesceneinfo.playerBall;i++){
var coball=this.createBall(this.radius,i);
coball.x=GC.w_2;
coball.y=y-(this.radius*2+4)*i;
this.addChild(coball);
this.playerBall.push(coball);
}
var meigeangle=parseInt(360/gamesceneinfo.collideBall);
var length=200;
this.lenght=length;
for(var i=0;i<gamesceneinfo.collideBall;i++){
var ma=GC.mathAngle(i*meigeangle);
var coball=this.createBall(this.radius);
coball.x=ma.first*length;
coball.y=ma.second*length;
this.addBallInZX(coball);
}
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled : this.onTouchCancelled
}, this);
this.scheduleUpdate();
return true;
}
,onTouchBegan:function(touch, event){
var target =event.getCurrentTarget();
if(target.playerBall.lenght<=0)
return;
var lastp=cc.p(0,0);
for(var i=0;i<target.playerBall.length;i++){
var ball = target.playerBall[i];
if(i==0) {
lastp=ball.getPosition();
var bp = target.zx.convertToNodeSpace(cc.p(GC.w_2, target.zx.y - target.lenght));
ball.setPosition(bp);
// cc.log(ball)
ball.removeFromParent();
//target.addBallInZX(ball);
/* target.allBall.push(ball);
target.zx.addChild(ball);
target.zx.drawnode.drawSegment(cc.p(0, 0), cc.p(ball.x,ball.y), 1,cc.color(255, 255, 255, 255));//线的颜色
for(var a=0;a<target.allBall.length-1;a++){
var q = target.allBall[a];
var p=q.getPosition();
if(cc.pDistance(p,ball.getPosition())<target.radius*2){
target.gameOver(ball);
return;
}
}
if(target.playerBall.length<=1){
target.gameWin();
} */
}
else{
var nowp=ball.getPosition();
ball.runAction(cc.moveTo(0.1,lastp));
// ball.setPosition();
lastp=nowp;
}
}
target.playerBall.splice(0,1);
return true;
},
onTouchMoved:function(touch, event){
},
onTouchEnded:function(touch, event){
},
onTouchCancelled:function(touch, event){
},
addBallInZX:function(coball){
coball.retain(); // 提出来
this.allBall.push(coball);
this.zx.addChild(coball);
coball.scheduleUpdate();
this.zx.drawnode.drawSegment(cc.p(0, 0), cc.p(coball.x,coball.y), 1,cc.color(255, 255, 255, 255));//线的颜色
for(var i=0;i<this.allBall.length-1;i++){
this.allBall[i].retain();
var p=this.allBall[i].getPosition(); //b报错的地方 得不到本地对象,但是web可以用cc.log打印出来
if(cc.pDistance(p,coball.getPosition())<this.radius*2){
this.gameOver(coball);
return;
}
}
if(this.playerBall.length<=1){
this.gameWin();
}
},
update:function(){
if(!this.checkCollide)
return;
},
gameOver:function(target){
this.zx.pause();
this.setColor(cc.color(255,0,0,255));//失败背景色
cc.eventManager.removeAllListeners();
var action=cc.sequence(
cc.scaleTo(0.2,1.3),
cc.scaleTo(0.2,1),
cc.scaleTo(0.2,1.3),
// cc.scaleTo(0.5,1),
cc.callFunc(function(){
var tar=new cc.LayerColor(cc.color(155,155,155,205));//失败背景色
this.addChild(tar);
var scorestart=new cc.MenuItemFont("继续",function(){
if(SCORE<5)
return;
this.toLV(this.LV);
},this);
scorestart.setColor(cc.color.BLACK);
var restart=new cc.MenuItemFont("重新开始",function(){
this.toLV();
SCORE = 0;
},this);
restart.setColor(cc.color.BLACK);
var menu=new cc.Menu(scorestart,restart);
this.addChild(menu);
menu.alignItemsVerticallyWithPadding(20);
},this)
);
target.runAction(action);
},
toLV:function(LV){
cc.director.runScene(new gameScene(LV));
},
gameWin:function(){
this.setColor(cc.color(0,255,0,255));//背景颜色
var allballs=this.allBall;
var self=this;
this.zx.update=function(){
this.drawnode.clear();
this.drawnode.drawDot(cc.p(0,0), self.radius*4,cc.color(255, 255, 255, 255));//球
for(var i=0;i<allballs.length;i++){
var ball=allballs[i];
ball.x*=1.05;
ball.y*=1.05;
this.drawnode.drawSegment(cc.p(0, 0), cc.p(ball.x,ball.y), 2,cc.color(255, 255, 255, 255));
}
};
this.zx.scheduleUpdate();
this.scheduleOnce(function(){
this.LV++;
SCORE+=this.addScore;
this.score.setString(SCORE+"");
this.toLV(this.LV);
}.bind(this),2);
cc.log("gameWin");
},
createBall:function(width,num,fontSize){
var self=this;
var node=new cc.Node();
node.drawnode=new cc.DrawNode();
node.addChild(node.drawnode);
//drawnode.drawCircle(cc.p(0,0),widht, angle, segments, drawLineToCenter, lineWidth, color)
node.drawnode.drawDot(cc.p(0,0), width,cc.color(255, 255, 255, 255));//球的颜色
node.update=function(dt){
};
if(num) {
var LVL = cc.LabelTTF.create(num + "", "", 20);
LVL.setPosition(0, 0);
LVL.setColor(cc.color(0, 0, 0, 255));//数字的颜色
node.addChild(LVL);
node.update=function(dt){
LVL.setRotation(360-self.zx.getRotation());
}
}
return node;
}
});
var gameScene=cc.Scene.extend({
ctor:function(LV){
this._super();
var gameinfo=gameSceneInfos.create(LV);
var game=new gameLayer(gameinfo);
this.addChild(game);
}
});
