CocosCreator项目学习系列<三>坦克大战项目的学习<25/12/2017>
知识点总结:
1.TileMap的使用精髓;
2.Joystick虚拟摇杆的整合单脚本;
3.面向组件JavaScript编程风格的学习;
4.一些陌生API的学习;
5,低耦合编程思想的借鉴;
6.脱离Cocos物理引擎实现的碰撞侦测.
工程下载:http://forum.cocos.com/t/topic/42340
一,游戏效果演示:
二,层级管理器界面和资源管理器布局介绍:
三,上齐所有脚本,TiledMap地图的坑作者并没有填完,然后场景选择转换的那个也请忽视:(自定义的全局对象cc.gameData cc.gameRes)
StartScript脚本:(标题画面)
cc.Class({
extends: cc.Component,
onLoad: function () {
//全局数据
if (!cc.globalData) {
cc.globalData = {};
}
},
loadChoiceScene: function () {
cc.director.loadScene("ChoiceScene");
},
});
TiledMapData脚本:(TiledMap数据类型定义)
var _tileType = cc.Enum({
tileNone: 0,
tileGrass: 1,
tileSteel: 2,
tileWall: 3,
tileRiver: 4,
tileKing: 5
});
//gid从1开始
var _gidToTileType = [
_tileType.tileNone,
_tileType.tileNone, _tileType.tileNone, _tileType.tileGrass, _tileType.tileGrass, _tileType.tileSteel, _tileType.tileSteel,
_tileType.tileNone, _tileType.tileNone, _tileType.tileGrass, _tileType.tileGrass, _tileType.tileSteel, _tileType.tileSteel,
_tileType.tileWall, _tileType.tileWall, _tileType.tileRiver, _tileType.tileRiver, _tileType.tileKing, _tileType.tileKing,
_tileType.tileWall, _tileType.tileWall, _tileType.tileRiver, _tileType.tileRiver, _tileType.tileKing, _tileType.tileKing,
_tileType.tileKing, _tileType.tileKing, _tileType.tileNone, _tileType.tileNone, _tileType.tileNone, _tileType.tileNone,
_tileType.tileKing, _tileType.tileKing, _tileType.tileNone, _tileType.tileNone, _tileType.tileNone, _tileType.tileNone
];
module.exports = {
tileType: _tileType,
gidToTileType: _gidToTileType
};
CityScript脚本:(相当于GameManager)
var TankType = require("TankData").tankType;
cc.Class({
extends: cc.Component,
properties: {
//地图
curMap: cc.TiledMap,
//摇杆
yaogan: cc.Node,
//子弹预制体
bullet: cc.Prefab,
//坦克预制体
tank: {
default: null,
type: cc.Prefab,
},
//最大数量
maxCount: 5,
//出生地
bornPoses: {
default: [],
type: cc.Vec2,
},
//坦克皮肤
spriteFrames: {
default: [],
type: cc.SpriteFrame,
},
//坦克移动速度
tankSpeeds: {
default: [],
type: cc.Float,
},
//坦克子弹发射间隔时间
tankFireTimes: {
default: [],
type: cc.Float,
},
//坦克血量
tankBloods: {
default: [],
type: cc.Integer,
},
},
// use this for initialization
onLoad: function () {
cc.director.setDisplayStats(true);
//获取摇杆控制组件
this._joystickCtrl = this.yaogan.getComponent("JoystickCtrl");
//获取地图 TiledMap 组件
this._tiledMap = this.curMap.getComponent('cc.TiledMap');
},
start: function(err){
if(err){
return;
}
//默认角度
this.curAngle = null;
var self = this;
//注册监听事件
this.registerInputEvent();
//引入地图数据
this._tiledMapData = require("TiledMapData");
//获取地图尺寸
this._curMapTileSize = this._tiledMap.getTileSize();
this._curMapSize = cc.v2(this._tiledMap.node.width,this._tiledMap.node.height);
//地图墙层
this.mapLayer0 = this._tiledMap.getLayer("layer_0");
//初始化对象池(参数必须为对应脚本的文件名)
this.bulletPool = new cc.NodePool("BulletScript");
var initBulletCount = 20;
for(var i=0; i<initBulletCount; ++i){
var bullet = cc.instantiate(this.bullet);
this.bulletPool.put(bullet);
}
this.tankPool = new cc.NodePool("TankScript");
for(var i=0; i<this.maxCount; ++i){
var tank = cc.instantiate(this.tank);
this.tankPool.put(tank);
}
if(!cc.gameData){
cc.gameData = {};
}
//初始化
cc.gameData.teamId = 0;
//临时
cc.gameData.single = true;
//地图内坦克列表
cc.gameData.tankList = [];
//地图内子弹列表
cc.gameData.bulletList = [];
//获取组件
this.tankNode = cc.find("/Canvas/Map/tank");
//加入player
this.player = this.addPlayerTank();
//获取坦克控制组件
this._playerTankCtrl = this.player.getComponent("TankScript");
//启动定时器,添加坦克
this.schedule(this.addAITank,3,cc.macro.REPEAT_FOREVER,1);
},
//注册输入事件
registerInputEvent: function () {
var self = this;
this._joystickCtrl.addJoyStickTouchChangeListener(function (angle) {
if(angle == self.curAngle &&
!self._playerTankCtrl.stopMove ){
return;
}
self.curAngle = angle;
if(angle!=null){
//开始前进
self._playerTankCtrl.tankMoveStart(angle);
}else {
//停止前进
self._playerTankCtrl.tankMoveStop();
}
});
//按键按下
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,
function (event) {
var angle = null;
switch(event.keyCode) {
case cc.KEY.w:
angle = 90;
break;
case cc.KEY.s:
angle = 270;
break;
case cc.KEY.a:
angle = 180;
break;
case cc.KEY.d:
angle = 0;
break;
}
if(event.keyCode == cc.KEY.k){
this.fireBtnClick();
}else {
self._playerTankCtrl.tankMoveStop();
}
if(angle!=null){
//开始前进
self._playerTankCtrl.tankMoveStart(angle);
}
}, this);
//按键抬起
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,
function (event){
//停止前进
if(event.keyCode != cc.KEY.k){
self._playerTankCtrl.tankMoveStop();
}
}, this);
},
//碰撞检测
collisionTest: function(rect, bullet){
//判断是否碰到地图边界
if (rect.xMin <= -this._curMapSize.x/2 || rect.xMax >= this._curMapSize.x/2 ||
rect.yMin <= -this._curMapSize.y/2 || rect.yMax >= this._curMapSize.y/2){
return true;
}
//判断是否撞墙
//将坐标转换为地图坐标系
var MinY = this._curMapSize.y/2 - rect.yMin;
var MaxY = this._curMapSize.y/2 - rect.yMax;
var MinX = this._curMapSize.x/2 + rect.xMin;
var MaxX = this._curMapSize.x/2 + rect.xMax;
//获取四个角的顶点
var LeftDown = cc.v2(MinX, MinY);
var RightDown = cc.v2(MaxX, MinY);
var LeftUp = cc.v2(MinX, MaxY);
var RightUp = cc.v2(MaxX, MaxY);
//获取四条边的中心点
var MidDown = cc.v2(MinX+(MaxX-MinX)/2, MinY);
var MidUp = cc.v2(MinX+(MaxX-MinX)/2, MaxY);
var MidLeft = cc.v2(MinX, MinY+(MaxY-MinY)/2);
var MidRight= cc.v2(MaxX, MinY+(MaxY-MinY)/2);
//检测碰撞
return this._collisionTest([LeftDown,RightDown,LeftUp,RightUp,
MidDown,MidUp,MidLeft,MidRight],
bullet);
},
//内部碰撞检测方法
_collisionTest: function(points, bullet){
var point = points.shift()
var gid = this.mapLayer0.getTileGIDAt(cc.v2(parseInt(point.x / this._curMapTileSize.width),parseInt(point.y / this._curMapTileSize.height)));
if (this._tiledMapData.gidToTileType[gid] != this._tiledMapData.tileType.tileNone &&
this._tiledMapData.gidToTileType[gid] != this._tiledMapData.tileType.tileGrass){
if(bullet && this._tiledMapData.gidToTileType[gid] == this._tiledMapData.tileType.tileWall){
this.mapLayer0.removeTileAt(cc.v2(parseInt(point.x / this._curMapTileSize.width),parseInt(point.y / this._curMapTileSize.height)));
}
return true;
}
if(points.length>0){
return this._collisionTest(points, bullet);
}else{
return false;
}
},
//加入玩家坦克
addPlayerTank: function(team) {
if(this.tankPool.size()>0){
var tank = this.tankPool.get();
tank.getComponent(cc.Sprite).spriteFrame = this.spriteFrames[this.spriteFrames.length-1];
tank.position = this.bornPoses[this.bornPoses.length-1];
//获取坦克控制组件
var tankCtrl = tank.getComponent("TankScript");
//设置坦克属性
tankCtrl.tankType = TankType.Player;
tankCtrl.speed = this.tankSpeeds[this.tankSpeeds.length-1];
tankCtrl.fireTime = this.tankFireTimes[this.tankFireTimes.length-1];
tankCtrl.blood = this.tankBloods[this.tankBloods.length-1];
tankCtrl.die = false;
if(!team){
if(cc.gameData.single){
//单机版
tankCtrl.team = 0;
}else {
//大乱斗
tankCtrl.team = ++cc.gameData.teamId;
}
}else {
//组队
tankCtrl.team = team;
}
tank.parent = this.tankNode;
//加到列表
cc.gameData.tankList.push(tank);
return tank;
}
return null;
},
//加入AI
addAITank: function(dt, team) {
if(this.tankPool.size()>0){
var tank = this.tankPool.get();
var index = parseInt(Math.random()*3, 10);
//获取坦克控制组件
var tankCtrl = tank.getComponent("TankScript");
//设置坦克属性
tank.getComponent(cc.Sprite).spriteFrame = this.spriteFrames[index];
tank.position = this.bornPoses[index];
tankCtrl.tankType = index;
tankCtrl.speed = this.tankSpeeds[index];
tankCtrl.fireTime = this.tankFireTimes[index];
tankCtrl.blood = this.tankBloods[index];
tankCtrl.die = false;
if(!team){
if(cc.gameData.single){
//单机版
tankCtrl.team = 1;
}else {
//大乱斗
tankCtrl.team = ++cc.gameData.teamId;
}
}else {
//组队
tankCtrl.team = team;
}
if(index == 0){
tank.rotation = 90;
}else if(index == 1){
tank.rotation = 180;
}else if(index == 2){
tank.rotation = 270;
}
if(tankCtrl.collisionTank(tank.getBoundingBox())){
for(var i=0; i<this.bornPoses.length-1; i++){
tank.position = this.bornPoses[i];
if(!tankCtrl.collisionTank(tank.getBoundingBox())){
break;
}
}
}
tank.parent = this.tankNode;
//加到列表
cc.gameData.tankList.push(tank);
}
},
tankBoom: function(tank) {
tank.parent = null;
tank.getComponent("TankScript").die = true;
this.tankPool.put(tank);
if(cc.gameData.single && tank.getComponent("TankScript").team == 0){
cc.director.loadScene("StartScene");
}
},
//开火按钮点击
fireBtnClick: function(){
if(this._playerTankCtrl.startFire(this.bulletPool)){
//播放射击音效
cc.audioEngine.play(this._playerTankCtrl.shootAudio, false, 1);
}
},
//销毁时调用
onDestroy: function () {
this.unschedule(this.addAITank,this);
},
});
TankData脚本:(坦克枚举)
var _tankType = cc.Enum({
Normal: 0,
Speed: 1,
Armor: 2,
Player: 3
});
module.exports = {
tankType: _tankType
};
TankScript脚本:(坦克脚本)
var TankType = require("TankData").tankType;
cc.Class({
extends: cc.Component,
properties: {
//坦克类型
tankType: {
default: TankType.Normal,
type: TankType
},
//速度
speed: 20,
//子弹
bullet: cc.Prefab,
//发射子弹间隔时间
fireTime: 0.5,
//血量
blood: 1,
//所属组织
team: 0,
//爆炸动画
blast: cc.Prefab,
//射击音效
shootAudio: {
default: null,
url: cc.AudioClip,
},
die: false,
},
// use this for initialization
onLoad: function () {
//获取组件
this._cityCtrl = cc.find("/CityScript").getComponent("CityScript");
this.bulletNode = cc.find("/Canvas/Map/bullet");
},
start: function () {
//初始是停止状态的
this.stopMove = true;
//偏移量
this.offset = cc.v2();
if (this.tankType != TankType.Player) {
var self = this;
//添加AI
var callback = cc.callFunc(function () {
var angles = [0, 90, 180, 270];
var index = parseInt(Math.random() * 4, 10);
self.tankMoveStart(angles[index]);
self.startFire(self._cityCtrl.bulletPool);
}, this);
var seq = cc.sequence(cc.delayTime(0.3), callback, cc.delayTime(1));
this.node.runAction(cc.repeatForever(seq));
}
},
//添加坦克移动动作
tankMoveStart: function (angle) {
this.node.rotation = 90 - angle;
if (angle == 0 || angle == 180 || angle == 90) {
this.offset = cc.v2(Math.floor(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else if (angle == 270) {
this.offset = cc.v2(Math.ceil(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else {
this.offset = cc.v2(Math.cos(Math.PI / 180 * angle),
Math.sin(Math.PI / 180 * angle));
}
this.stopMove = false;
},
//移除坦克移动动作
tankMoveStop: function () {
this.stopMove = true;
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if (!this.stopMove) {
var boundingBox = this.node.getBoundingBox();
var rect = cc.rect(boundingBox.xMin + this.offset.x * this.speed * dt * 1.5,
boundingBox.yMin + this.offset.y * this.speed * dt * 1.7,
boundingBox.size.width,
boundingBox.size.height);
if (this._cityCtrl.collisionTest(rect) //检测与地图的碰撞
||
this.collisionTank(rect)
) {
this.tankMoveStop();
} else {
this.node.x += this.offset.x * this.speed * dt;
this.node.y += this.offset.y * this.speed * dt;
}
}
if (this.stopFire) {
this.fireTime -= dt;
if (this.fireTime <= 0) {
this.stopFire = false;
}
}
},
//判断是否与其他坦克碰撞
collisionTank: function (rect) {
for (var i = 0; i < cc.gameData.tankList.length; i++) {
var tank = cc.gameData.tankList[i]
if (this.node === tank) {
continue;
}
var boundingBox = tank.getBoundingBox();
if (cc.rectIntersectsRect(rect, boundingBox)) {
return true;
}
}
return false;
},
//开火
startFire: function (bulletPool) {
if (this.stopFire) {
return false;
}
this.stopFire = true;
this.fireTime = 0.5;
var bullet = null;
if (bulletPool.size() > 0) {
bullet = bulletPool.get(bulletPool);
} else {
bullet = cc.instantiate(this.bullet);
}
//设置子弹位置,角度
bullet.rotation = this.node.rotation;
var pos = this.node.position;
var angle = 90 - this.node.rotation;
var offset = cc.v2(0, 0);
if (angle == 0 || angle == 180 || angle == 90) {
offset = cc.v2(Math.floor(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else if (angle == 270) {
offset = cc.v2(Math.ceil(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else {
offset = cc.v2(Math.cos(Math.PI / 180 * angle),
Math.sin(Math.PI / 180 * angle));
}
bullet.position = cc.pAdd(pos, cc.v2(10 * offset.x, 10 * offset.y));
bullet.getComponent("BulletScript").bulletMove();
bullet.parent = this.bulletNode;
//子弹标记
bullet.tag = this.team;
//加到列表
cc.gameData.bulletList.push(bullet);
return true;
},
//爆炸
boom: function () {
var blast = cc.instantiate(this.blast);
blast.parent = this.node.parent;
blast.position = this.node.position;
var anim = blast.getComponent(cc.Animation);
anim.play();
this._cityCtrl.tankBoom(this.node);
},
});
BulletScript脚本:(子弹脚本)
var TankType = require("TankData").tankType;
cc.Class({
extends: cc.Component,
properties: {
speed: 20
},
// use this for initialization
onLoad: function () {
this._cityCtrl = cc.find("/CityScript").getComponent("CityScript");
},
//对象池get获取对象是会调用此方法
reuse: function (bulletPool) {
this.bulletPool = bulletPool; // get 中传入的子弹对象池
},
//子弹移动
bulletMove: function () {
//偏移
var angle = 90 - this.node.rotation;
if (angle == 0 || angle == 180 || angle == 90) {
this.offset = cc.v2(Math.floor(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else if (angle == 270) {
this.offset = cc.v2(Math.ceil(Math.cos(Math.PI / 180 * angle)),
Math.floor(Math.sin(Math.PI / 180 * angle)));
} else {
this.offset = cc.v2(Math.cos(Math.PI / 180 * angle),
Math.sin(Math.PI / 180 * angle));
}
},
//子弹爆炸
bulletBoom: function () {
this.node.parent = null;
this.bulletPool.put(this.node);
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
//移动
this.node.x += this.offset.x * this.speed * dt;
this.node.y += this.offset.y * this.speed * dt;
//检测碰撞
var rect = this.node.getBoundingBox();
if (this._cityCtrl.collisionTest(rect, true) ||
this.collisionTank(rect)) {
//子弹爆炸
this.bulletBoom();
}
},
//判断与坦克碰撞
collisionTank: function (rect) {
for (var i = 0; i < cc.gameData.tankList.length; i++) {
var tank = cc.gameData.tankList[i]
var tankCtrl = tank.getComponent("TankScript");
if (tankCtrl.team == this.node.tag || tankCtrl.die) {
//同一队不互相伤害
continue;
}
var boundingBox = tank.getBoundingBox();
if (cc.rectIntersectsRect(rect, boundingBox)) {
if (--tankCtrl.blood <= 0) {
tankCtrl.boom();
}
return true;
}
}
return false;
},
});
JoystickCtrl脚本:(虚拟摇杆)
var TouchType = cc.Enum({
DEFAULT: 0,
FOLLOW: 1
});
var DirectionType = cc.Enum({
FOUR: 0,
EIGHT: 1,
ALL: 2
});
cc.Class({
extends: cc.Component,
properties: {
joystickBar: {
default: null,
type: cc.Node
}, //控杆
joystickBG: {
default: null,
type: cc.Node
}, //控杆背景
radius: 0, //半径
touchType: {
default: TouchType.DEFAULT, //触摸类型
type: TouchType
},
directionType: {
default: DirectionType.ALL, //方向类型
type: DirectionType
},
//当前角度
curAngle: {
default: 0,
visible: false
},
//当前距离
distance: {
default: 0,
visible: false
}
},
// use this for initialization
onLoad: function () {
if (this.radius == 0) {
this.radius = this.joystickBG.width / 2
}
this.registerInput()
this.distance = 0
this.curAngle = 0
this.initPos = this.node.position
this.node.opacity = 50
},
addJoyStickTouchChangeListener: function (callback) {
this.angleChange = callback;
},
registerInput: function () {
var self = this;
// touch input
this._listener = cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function (touch, event) {
return self.onTouchBegan(touch, event)
},
onTouchMoved: function (touch, event) {
self.onTouchMoved(touch, event)
},
onTouchEnded: function (touch, event) {
self.onTouchEnded(touch, event)
}
}, self.node);
},
onTouchBegan: function (touch, event) {
//如果触摸类型为FOLLOW,则摇控杆的位置为触摸位置,触摸开始时候现形
if (this.touchType == TouchType.FOLLOW) {
var touchPos = this.node.parent.convertToNodeSpaceAR(touch.getLocation())
this.node.setPosition(touchPos);
return true;
} else {
//把触摸点坐标转换为相对与目标的模型坐标
var touchPos = this.node.convertToNodeSpaceAR(touch.getLocation())
//点与圆心的距离
var distance = cc.pDistance(touchPos, cc.p(0, 0));
//如果点与圆心距离小于圆的半径,返回true
if (distance < this.radius) {
if (distance > 20) {
this.node.opacity = 255
this.joystickBar.setPosition(touchPos);
//更新角度
this._getAngle(touchPos)
}
return true;
}
}
return false;
},
onTouchMoved: function (touch, event) {
//把触摸点坐标转换为相对与目标的模型坐标
var touchPos = this.node.convertToNodeSpaceAR(touch.getLocation())
//点与圆心的距离
var distance = cc.pDistance(touchPos, cc.p(0, 0));
//如果点与圆心距离小于圆的半径,控杆跟随触摸点
if (this.radius >= distance) {
if (distance > 20) {
this.node.opacity = 255;
this.joystickBar.setPosition(touchPos);
//更新角度
this._getAngle(touchPos)
} else {
this.node.opacity = 50
//摇杆恢复位置
this.joystickBar.setPosition(cc.p(0, 0));
this.curAngle = null;
//调用角度变化回调
if (this.angleChange) {
this.angleChange(this.curAngle);
}
}
} else {
//触摸监听目标
var x = Math.cos(this._getRadian(touchPos)) * this.radius;
var y = Math.sin(this._getRadian(touchPos)) * this.radius;
if (touchPos.x > 0 && touchPos.y < 0) {
y *= -1;
} else if (touchPos.x < 0 && touchPos.y < 0) {
y *= -1;
}
this.joystickBar.setPosition(cc.p(x, y));
//更新角度
this._getAngle(touchPos)
}
},
onTouchEnded: function (touch, event) {
this.node.opacity = 50
//如果触摸类型为FOLLOW,离开触摸后隐藏
if (this.touchType == TouchType.FOLLOW) {
this.node.position = this.initPos
}
//摇杆恢复位置
this.joystickBar.setPosition(cc.p(0, 0));
this.curAngle = null
//调用角度变化回调
if (this.angleChange) {
this.angleChange(this.curAngle);
}
},
//计算角度并返回
_getAngle: function (point) {
this._angle = Math.floor(this._getRadian(point) * 180 / Math.PI);
if (point.x > 0 && point.y < 0) {
this._angle = 360 - this._angle;
} else if (point.x < 0 && point.y < 0) {
this._angle = 360 - this._angle;
} else if (point.x < 0 && point.y == 0) {
this._angle = 180;
} else if (point.x > 0 && point.y == 0) {
this._angle = 0;
} else if (point.x == 0 && point.y > 0) {
this._angle = 90;
} else if (point.x == 0 && point.y < 0) {
this._angle = 270;
}
this._updateCurAngle()
return this._angle;
},
//计算弧度并返回
_getRadian: function (point) {
var curZ = Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2));
if (curZ == 0) {
this._radian = 0;
} else {
this._radian = Math.acos(point.x / curZ);
}
return this._radian;
},
//更新当前角度
_updateCurAngle: function () {
switch (this.directionType) {
case DirectionType.FOUR:
this.curAngle = this._fourDirections();
break;
case DirectionType.EIGHT:
this.curAngle = this._eightDirections();
break;
case DirectionType.ALL:
this.curAngle = this._angle
break;
default:
this.curAngle = null
break;
}
//调用角度变化回调
if (this.angleChange) {
this.angleChange(this.curAngle);
}
},
//四个方向移动(上下左右)
_fourDirections: function () {
if (this._angle >= 45 && this._angle <= 135) {
return 90
} else if (this._angle >= 225 && this._angle <= 315) {
return 270
} else if (this._angle <= 225 && this._angle >= 180 || this._angle >= 135 && this._angle <= 180) {
return 180
} else if (this._angle <= 360 && this._angle >= 315 || this._angle >= 0 && this._angle <= 45) {
return 0
}
},
//八个方向移动(上下左右、左上、右上、左下、右下)
_eightDirections: function () {
if (this._angle >= 67.5 && this._angle <= 112.5) {
return 90
} else if (this._angle >= 247.5 && this._angle <= 292.5) {
return 270
} else if (this._angle <= 202.5 && this._angle >= 180 || this._angle >= 157.5 && this._angle <= 180) {
return 180
} else if (this._angle <= 360 && this._angle >= 337.5 || this._angle >= 0 && this._angle <= 22.5) {
return 0
} else if (this._angle >= 112.5 && this._angle <= 157.5) {
return 135
} else if (this._angle >= 22.5 && this._angle <= 67.5) {
return 45
} else if (this._angle >= 202.5 && this._angle <= 247.5) {
return 225
} else if (this._angle >= 292.5 && this._angle <= 337.5) {
return 315
}
},
onDestroy: function () {
cc.eventManager.removeListener(this._listener);
}
});
NoTouchScript脚本:(输入事件相关)
cc.Class({
extends: cc.Component,
// use this for initialization
onLoad: function () {
var self = this;
// touch input
this._listener = cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function (touch, event) {
//截获事件
event.stopPropagation();
return true;
},
onTouchMoved: function (touch, event) {
//截获事件
event.stopPropagation();
},
onTouchEnded: function (touch, event) {
//截获事件
event.stopPropagation();
}
}, self.node);
//按键按下
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,
function (event) {
//截获事件
event.stopPropagation();
}, this);
//按键抬起
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,
function (event) {
//截获事件
event.stopPropagation();
}, this);
},
});
BlastScript脚本:(特效相关)
cc.Class({
extends: cc.Component,
playFinish: function () {
this.node.parent = null;
},
});
ChoiceScript脚本:(场景切换)
cc.Class({
extends: cc.Component,
properties: {
curLevelLabel: cc.Label,
},
// use this for initialization
onLoad: function () {
cc.gameData = {};
cc.gameData.curLevel = 1;
this.updateLevelLabel();
},
onPlay: function () {
var self = this;
cc.loader.onProgress = function (completedCount, totalCount, item) {
console.log(completedCount + "/" + totalCount);
};
cc.director.preloadScene("CityScene" + cc.gameData.curLevel, function (assets, error) {
//跳转到游戏界面
cc.director.loadScene("CityScene" + cc.gameData.curLevel);
});
},
onUp: function () {
if (cc.gameData.curLevel - 1 <= 0) {
return;
}
cc.gameData.curLevel -= 1;
this.updateLevelLabel();
},
onNext: function () {
if (cc.gameData.curLevel + 1 > 20) {
return;
}
cc.gameData.curLevel += 1;
this.updateLevelLabel();
},
updateLevelLabel: function () {
this.curLevelLabel.string = "Round " + cc.gameData.curLevel;
},
});
AssetsLoadScript脚本:(资源加载相关)
cc.Class({
extends: cc.Component,
properties: {
numLabel: cc.Label,
},
// use this for initialization
onLoad: function () {
var self = this;
var urls = [{
id: "tank",
url: cc.url.raw("resources/tank.plist")
}];
cc.LoadingItems.create(cc.loader, urls, function (completedCount, totalCount, item) {
var progress = (100 * completedCount / totalCount).toFixed(2);
cc.log(progress + '%');
self.numLabel.string = Math.abs(progress) + '%';
console.log("==========" + item.url);
}, function (errors, items) {
if (errors) {
for (var i = 0; i < errors.length; ++i) {
cc.log('Error url: ' + errors[i] + ', error: ' + items.getError(errors[i]));
}
} else {
console.log(items.totalCount);
}
})
},
});