验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
Languages: JavaScript
Categories: 其他
Latest update 2018-12-06 22:43
Snippet 1 Snippet 2
pipei.js
Raw Copy
// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
MatchBackground_Audio: {
default: null,
type: cc.AudioClip
},
player0TXT: {
default: null,
type: cc.Label
},
win0TXT: {
default: null,
type: cc.Label
},
lost0TXT: {
default: null,
type: cc.Label
},
player1TXT: {
default: null,
type: cc.Label
},
win1TXT: {
default: null,
type: cc.Label
},
lost1TXT: {
default: null,
type: cc.Label
},
},
onLoad: function () {
this.timer = 0;
this.current = cc.audioEngine.play(this.MatchBackground_Audio, false,1.0);
var player0TXT = this.player0TXT;
var win0TXT = this.win0TXT;
var lost0TXT = this.lost0TXT;
var player1TXT = this.player1TXT;
var win1TXT = this.win1TXT;
var lost1TXT = this.lost1TXT;
var network = require("NetworkUtils");
var repeat = 1;
this.schedule(function() {
network.send(
'http://120.79.90.91/os/index.php?s=/game/user/match',
{
userId:userId,
},
function(receiveData){
var self=this;
console.log(receiveData);
var a=JSON.parse(receiveData);
console.log(a);
if(a.state==0){
window.player0;
window.player0=a.data.player0;
window.player1;
window.player1=a.data.player1;
window.chessbordData;
window.chessbordData=a.data.initData;
player0TXT.string=a.data.player0;
player1TXT.string=a.data.player1;
cc.director.loadScene('Game');
}
if(a.state==1){
// errorTXT.string = a.data;
console.log("zzzzzzzz");
}
},
null
);
}, 5);
network.send(
'http://120.79.90.91/os/index.php?s=/game/user/getUserInfo',
{
userId:this.player0,
},
function(receiveData){
var self=this;
var b=JSON.parse(receiveData);
console.log(b);
if(b.state==0){
win0TXT.string=b.data.win;
lost0TXT.string=b.data.lost;
}
else if(b.state==1){
console.log(b.data);
}
},
null
);
network.send(
'http://120.79.90.91/os/index.php?s=/game/user/getUserInfo',
{
userId:this.player1,
},
function(receiveData){
var self=this;
var c=JSON.parse(receiveData);
console.log(c);
if(c.state==0){
win0TXT.string=c.data.win;
lost0TXT.string=c.data.lost;
}
else if(c.state==1){
console.log(c.data);
}
},
null
);
},
update: function (dt) {
// 每帧更新计时器,超过限度还没有生成新的星星
// 就会调用游戏失败逻辑
// if (this.timer > 2.5) { //定时器
// this.pipei();
// return;
// }
// this.timer += dt;
},
pipei: function () {
cc.director.loadScene('Game');
},
onDestroy: function () {
cc.audioEngine.stop(this.current);
},
start () {
},
});
chessborder.js
Raw Copy
cc.Class({
extends: cc.Component,
properties: {
chessgridCanvas:{
default: null,
type: cc.Node
},
chessgridPrefab:{
default: null,
type: cc.Prefab
},
chessmanCanvas:{
default: null,
type: cc.Node
},
chessmanPrefab:{
default: null,
type: cc.Prefab
},
currentPlay: {
default: null,
type: cc.Node
},
OpenChess_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess7_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess6_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess5_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess4_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess3_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess2_Audio: {
default: null,
type: cc.AudioClip
},
AttackChess1_Audio: {
default: null,
type: cc.AudioClip
},
BeAttackChess_Audio: {
default: null,
type: cc.AudioClip
},
RedBossComein_Audio: {
default: null,
type: cc.AudioClip
},
RedBossTalk_Audio: {
default: null,
type: cc.AudioClip
},
BlackBossComein_Audio: {
default: null,
type: cc.AudioClip
},
BlackBossTalk_Audio: {
default: null,
type: cc.AudioClip
},
GameBackground_Audio: {
default: null,
type: cc.AudioClip
},
MoveChess_Audio: {
default: null,
type: cc.AudioClip
},
},
// use this for initialization
onLoad: function () {
this.current = this.playBackgroundSound(this.GameBackground_Audio);
this.chessgrids=[];
for (var row = 0; row < 4; row++) {
this.chessgrids[row]=[];
for (var col = 0; col < 8; col++) {
var chessgrid = cc.instantiate(this.chessgridPrefab);
this.chessgrids[row][col]=chessgrid;
chessgrid.y=row*97-97;
chessgrid.x=col*102-102;
chessgrid.row=row;
chessgrid.col=col;
chessgrid.on(cc.Node.EventType.TOUCH_END, this.onChessgridTouched, this);
this.chessgridCanvas.addChild(chessgrid);
}
}
this.resetChessbordData();
this.chessmans=[];
for (row = 0; row < 4; row++) {
this.chessmans[row]=[];
for (col = 0; col < 8; col++) {
var chessman = cc.instantiate(this.chessmanPrefab);
this.chessmans[row][col]=chessman;
chessman.y=this.chessgrids[row][col].y;
chessman.x=this.chessgrids[row][col].x;
chessman.row=row;
chessman.col=col;
this.chessmanCanvas.addChild(chessman);
}
}
this.currentPlayComponent=this.currentPlay.getComponent("chessman");
this.currentPlayId=1;
this.currentPlayComponent.setChessman("b7");
this.selectedChessgrid=null;
this.selectedChessman=null;
this.selectedChessmanComponent=null;
this.selectedChessmanValue=0;
this.currentChessgrid=null;
this.currentChessman=null;
this.currentChessmanComponent=null;
this.currentChessmanValue=0;
this.canOperate=true;
},
onChessgridTouched:function(event){
if(!this.canOperate)
return;
var chessgrid=event.target;
if (this.selectedChessgrid===null) {
var chessmanValue=this.getChessmanValue(chessgrid.row,chessgrid.col);
if (this.isOpen(chessmanValue)&&(chessmanValue&8)/8!=this.currentPlayId)
return;
this.selectChessgrid(chessgrid);
return;
}
var self=this;
var selectedChessmanDirection;
var currentChessmanDirection;
this.currentChessgrid=chessgrid;
this.currentChessmanValue=this.getChessmanValue(this.currentChessgrid.row,this.currentChessgrid.col);
//open
if(this.selectedChessgrid==this.currentChessgrid && (this.currentChessmanValue&48)==48){
this.canOperate=false;
this.currentChessman=this.chessmans[this.currentChessgrid.row][this.currentChessgrid.col];
this.currentChessmanComponent=this.currentChessman.getComponent("chessman");
self.playOpenSound(this.OpenChess_Audio);
this.currentChessmanComponent.playAnimation(0,function(){
var chessmanId='b';
if((self.currentChessmanValue&8)==8)
chessmanId='r';
chessmanId=chessmanId+(self.currentChessmanValue&7);
self.currentChessmanComponent.setChessman(chessmanId);
if ((self.currentChessmanValue&15)==15) {
console.log("The RedKing is come in!");
cc.audioEngine.pauseAll();
cc.audioEngine.play(self.RedBossComein_Audio, false,0.5);
}
if ((self.currentChessmanValue&15)==7) {
console.log("The BlackKing is come in!");
cc.audioEngine.pauseAll();
cc.audioEngine.setCurrentTime(self.BlackBossComein_Audio, 1);
cc.audioEngine.play(self.BlackBossComein_Audio, false,0.5);
}
self.unselectChessgrid();
self.canOperate=true;
self.switchCurrentPlay();
});
this.setChessmanValue(this.currentChessgrid.row,this.currentChessgrid.col,this.currentChessmanValue-16);
// this.selectedChessgrid=null;
this.currentChessgrid=null;
return;
}
// attack
this.selectedChessmanValue = this.getChessmanValue(this.selectedChessgrid.row,this.selectedChessgrid.col);
if (this.isOpen(this.selectedChessmanValue) && this.isOpen(this.currentChessmanValue) && (this.selectedChessmanValue&8)!=(this.currentChessmanValue&8)) {
// next to && canAttack
if (this.isNextTo(this.selectedChessgrid,this.currentChessgrid) && this.canAttack(this.selectedChessmanValue,this.currentChessmanValue)) {
this.canOperate=false;
this.currentChessman=this.chessmans[this.currentChessgrid.row][this.currentChessgrid.col];
this.currentChessmanComponent=this.currentChessman.getComponent("chessman");
this.selectedChessman=this.chessmans[this.selectedChessgrid.row][this.selectedChessgrid.col];
this.selectedChessmanComponent=this.selectedChessman.getComponent("chessman")
selectedChessmanDirection=this.getDirection(this.selectedChessgrid,this.currentChessgrid);
currentChessmanDirection=this.getDirection(this.currentChessgrid,this.selectedChessgrid);
if ((this.selectedChessmanValue&7)==7) {
self.playOpenSound(this.AttackChess7_Audio);
}
if ((this.selectedChessmanValue&7)==6) {
self.playOpenSound(this.AttackChess6_Audio);
}
if ((this.selectedChessmanValue&7)==5) {
self.playOpenSound(this.AttackChess5_Audio);
}
if ((this.selectedChessmanValue&7)==4) {
self.playOpenSound(this.AttackChess4_Audio);
}
if ((this.selectedChessmanValue&7)==3) {
self.playOpenSound(this.AttackChess3_Audio);
}
if ((this.selectedChessmanValue&7)==2) {
self.playOpenSound(this.AttackChess2_Audio);
}
if ((this.selectedChessmanValue&7)==1) {
self.playOpenSound(this.AttackChess1_Audio);
}
this.selectedChessmanComponent.playAnimation(1+selectedChessmanDirection,function(){
self.selectedChessmanComponent.move(selectedChessmanDirection,self.currentChessgrid.x,self.currentChessgrid.y,function(){
self.selectedChessmanComponent.reset();
self.canOperate=true;
self.unselectChessgrid();
self.currentChessgrid=null;
self.switchCurrentPlay();
});
});
//beAttack
this.currentChessmanComponent.playAnimation(4*(this.selectedChessmanValue&7)+5+currentChessmanDirection,function(){
self.currentChessman.destroy();
});
this.setChessmanValue(this.currentChessgrid.row,this.currentChessgrid.col,this.selectedChessmanValue);
this.setChessmanValue(this.selectedChessgrid.row,this.selectedChessgrid.col,0);
this.chessmans[this.currentChessgrid.row][this.currentChessgrid.col]=this.selectedChessman;
this.chessmans[this.selectedChessgrid.row][this.selectedChessgrid.col]=null;
return;
}
}
//move
if (this.isOpen(this.selectedChessmanValue) && this.currentChessmanValue===0 && this.isNextTo(this.selectedChessgrid,this.currentChessgrid)){
this.canOperate=false;
this.selectedChessman=this.chessmans[this.selectedChessgrid.row][this.selectedChessgrid.col];
this.selectedChessmanComponent=this.selectedChessman.getComponent("chessman");
selectedChessmanDirection=this.getDirection(this.selectedChessgrid,this.currentChessgrid)
self.playOpenSound(this.MoveChess_Audio);
this.selectedChessmanComponent.move(selectedChessmanDirection,self.currentChessgrid.x,self.currentChessgrid.y,function(){
self.selectedChessmanComponent.reset();
self.unselectChessgrid();
self.currentChessgrid=null;
self.canOperate=true;
self.switchCurrentPlay();
});
this.setChessmanValue(this.currentChessgrid.row,this.currentChessgrid.col,this.selectedChessmanValue);
this.setChessmanValue(this.selectedChessgrid.row,this.selectedChessgrid.col,0);
this.chessmans[this.currentChessgrid.row][this.currentChessgrid.col]=this.selectedChessman;
this.chessmans[this.selectedChessgrid.row][this.selectedChessgrid.col]=null;
return;
}
this.unselectChessgrid();
},
selectChessgrid:function(chessgrid){
chessgrid.opacity=255;
this.selectedChessgrid=chessgrid;
},
unselectChessgrid:function(){
this.selectedChessgrid.opacity=0;
this.selectedChessgrid=null;
},
playOpenSound: function (Audio) {
// 调用声音引擎播放声音
cc.audioEngine.play(Audio, false,1.0);
},
playBackgroundSound: function (Audio) {
cc.audioEngine.play(Audio, false,0.5);
},
stopSound: function (Audio) {
cc.audioEngine.stop(Audio);
},
setSelechessgrid:function(selectedChessgrid){
this.selectedChessmanComponent=this.selectedChessgrid.getComponent("chessman");
this.selectedChessmanComponent.Opacity=100;
this.selectedChessgrid=null;
},
getDirection:function(fromChessgrid,toChessgrid){
if (fromChessgrid.col==toChessgrid.col) {
if(fromChessgrid.row>toChessgrid.row)
return 0;
else
return 1;
}
else{
if(fromChessgrid.col>toChessgrid.col)
return 2;
else
return 3;
}
},
switchCurrentPlay:function(){
this.currentPlayId=(this.currentPlayId+1)%2;
if (this.currentPlayId===0)
this.currentPlayComponent.setChessman("b7");
else
this.currentPlayComponent.setChessman("r7");
},
canAttack:function(attackChessmanValue,beAttackChessmanValue){
attackChessmanValue&=7;
beAttackChessmanValue&=7;
if (attackChessmanValue===1 && beAttackChessmanValue===7)
return true;
if (attackChessmanValue===7 && beAttackChessmanValue===1)
return false;
return attackChessmanValue>=beAttackChessmanValue;
},
isOpen:function(chessmanValue){
return(chessmanValue&48)==32;
},
isNextTo:function(chessgrid1,chessgrid2){
return Math.abs(chessgrid1.row-chessgrid2.row)+Math.abs(chessgrid1.col-chessgrid2.col)==1;
},
getChessmanValue:function(row,col){
return this.chessbordData[row*8+col];
},
setChessmanValue:function(row,col,value){
this.chessbordData[row*8+col]=value;
},
resetChessbordData:function(){
this.chessbordData=eval("("+chessbordData+")");
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});

Comment list( 0 )

You need to Sign in for post a comment

Help Search