背景:
cc.Class({
extends: cc.Component,
properties: {
//左边‘-’按钮
leftBtn:cc.Node,
//右边‘+’按钮
rightBtn:cc.Node,
//背景图像
BackGround:cc.Node,
//炮台节点
batteryList:{
default:[],
type:cc.Node,
},
//子弹预制
bulletList:{
default:[],
type:cc.prefab,
},
},
onLoad: function () {
//炮台角度
this.currentBatteryRotation = 0;
//当前炮台
this.currentBattery = this.batteryList[0];
//当前炮台类型
this.currentBatteryIndex = 0;
//炮台冷却时间
this.batteryTime = 40;
//当前冷却时间
this.currentBatteryTime = 1;
//是否可以发射子弹
this.isCanBattery = true;
//添加选择炮台事件
this.addBatteryEvent();
//点击开火事件
this.addFireEvent();
},
/*选择炮台*/
addBatteryEvent:function(){
/*加减按钮的事件*/
this.leftBtn.on(cc.Node.EventType.TOUCH_END,function(event){
this.onBattery(false);
},this);
this.rightBtn.on(cc.Node.EventType.TOUCH_END,function(event){
this.onBattery(true);
},this);
},
onBattery:function(value){
if(value === false) this.currentBatteryIndex--;
else this.currentBatteryIndex++;
if(this.currentBatteryIndex < 0) this.currentBatteryIndex = 2;
else if(this.currentBatteryIndex > 2) this.currentBatteryIndex = 0;
for(var i = 0; i < 3; i++) this.batteryList[i].active = false;
this.currentBattery = this.batteryList[this.currentBatteryIndex];
this.currentBattery.active = true;
this.currentBattery.rotation = this.currentBatteryRotation;
this.currentBattery.getComponent(cc.Animation).play("battery"+(this.currentBatteryIndex+1));
},
update:function(dt){
this.currentBatteryTime++;
if(this.currentBatteryTime >= this.batteryTime) this.isCanBattery = true;
},
/*点击开火事件*/
addFireEvent:function(){
this.backGround.on(cc.Node.EventType.TOUCH_END,function(event){
if(this.isCanBattery===false) return;
//点击屏幕改变炮台角度
this.changeBattery(event);
//生成子弹
this.makeBattery();
},this);
},
/*修改炮台角度*/
changeBattery:function(){
//转换为世界坐标哦
var WorldPoint=this.currentBattery.parent.convertToWorldSpaceAR(this.currentBattery.position);
//通过点击屏幕,获取点击角度属性,并转换给炮台
this.currentBatteryRotation=this.getDegree(WorldPoint,event.touch._point);
this.currentBattery.rotation=this.currentBatteryRotation;
//当前冷却时间
this.currentBatteryTime=1;
//修改成不可发射状态
this.isCanBattery=false;
},
/*获取角度属性*/
getDegree:function(p1,p2){
//p1是当前炮台,p2是点击屏幕的坐标
//计算角度
var o=p2.x-p1.x;
var a=p2.y-p1.y;
var degree=math.atan(o/a)*180/math.PI;
if(a<0) degree=(o<0)?180+math.abs(degree):180-math.abs(degree);
return degree;
},
/*生成子弹*/
makeBattery:function(){
//播放炮台动画
this.currentBattery.getComponent(cc.Animation).play();
//子弹初始速度
var speed=6;
//炮台的角度
var degree=this.currentBattery.rotation;
//角度转弧度
var radians=cc.degreeToRadians(degree);
//算出X,Y方向的偏移量
var point=cc.pForAngle(radians);
var vx=this.currentBattery.x+50*point.y;
var vy=this.currentBattery.y+50*point.x;
//预制出子弹对象
var bullet =Global.getClassAndAdd(this.bulletList[this.currentBatteryIndex],
'bullet',this.node,vx,vy);
//设置子弹对象属性
bullet.tClass.init(this.currentBatteryIndex,speed*point.y,speed*point.x,degree);
},
});
子弹:
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
init: function (type,sx,sy,degree) {
this.bulletType=type;
this.node.rotation=degree;
this.speedx=sx;
this.speedy=sy;
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if(Global.isOutOfScreen(this.node)){
this.isRemove=true;
Global.destroyNode(this.node);
}
else{
this.node.x+=this.speedx;
this.node.y+=this.speedy;
}
},
});
window.Global={
//显示的节点
getClassAndAdd:function(showNode,className,parent,vx,vy){
var tempNode=cc.instantiate(showNode);
var tempClass=tempNode.getComponent(className);
parent.addChild(tempNode);
tempNode.x=vx;
tempNode.y=vy;
return {tNode:tempNode,tClass:tempClass};
},
/*判断是否被移出*/
isOutOfScreen:function(node){
var w=cc.winSize.width>>1,h=cc.winSize.height>>1,
nw=node.width>>1,nh=node.height>>1;
return (node.x<-w-nw||
node.x>w+nw||
node.y<-h-nh||
node.y>h+nh);
},
/*删除节点*/
destroyNode:function(){
if(node&&node.parent){
node.destroy();
node.parent.removeChild(node);
}
},
};
cc.Class({
extends: cc.Component,
properties: {
},
onLoad: function () {
this.backGround=this.node.getComponent("BackGround");
},
});