美文网首页工作生活
cocos creator制作一个小游戏【飞机大战】

cocos creator制作一个小游戏【飞机大战】

作者: 游戏开发大表哥 | 来源:发表于2019-07-01 14:29 被阅读0次

简介:飞机大战是很多人的童年的回忆, 也是许多学习cocos creator的学习者做的第一个完整的游戏, 下面就看一看这个飞机大战是如何做的

首先还是看一看场景的层级结构图 

以及mian结点上的脚本组件 

cc.Class({

extends: cc.Component,

properties:()=>({

pause: cc.Button,

btnSprite: {

default: [],

type: cc.SpriteFrame,

tooltip:'暂停按钮不同状态的图片',

},

bomb: cc.Node,

gameMusic: {

default:null,

type: cc.AudioSource

},

useBombClip: cc.AudioClip,

enemyGroup: {

default:null,

type:require('enemyGroup'),

},

hero: {

default:null,

type:require('hero'),

},

ufoGroup: {

default:null,

type:require('ufoGroup'),

},

bulletGroup: {

default:null,

type:require('bulletGroup'),

},

scoreDisplay:cc.Label,

bombNoDisplay:cc.Label,

}),

// use this for initialization

onLoad:function(){

this.score =0;

this.bombNo =0;

this.scoreDisplay.string =this.score;

this.bombNoDisplay.string =this.bombNo;

this.eState = D.commonInfo.gameState.start;

this.enemyGroup.startAction();

this.bulletGroup.startAction();

this.ufoGroup.startAction();

this.bomb.on('touchstart',this.bombOnclick,this);

this.gameMusic.play();

},

bombOnclick:function(){

varbombNoLabel =this.bomb.getChildByName('bombNo').getComponent(cc.Label);

varbombNo =parseInt(bombNoLabel.string);

if(bombNo>0){

bombNoLabel.string = bombNo-1;

this.removeEnemy();

cc.audioEngine.playEffect(this.useBombClip,false);

}else{

console.log('没有子弹');

}

},

// called every frame, uncomment this function to activate update callback

//update: function (dt) {

//},

//暂停按钮点击事件 

pauseClick:function(){//暂停 继续

if(this.eState == D.commonInfo.gameState.pause){

this.resumeAction();

this.eState = D.commonInfo.gameState.start;

}elseif(this.eState == D.commonInfo.gameState.start) {

this.pauseAction();

this.eState = D.commonInfo.gameState.pause;

}

},

//游戏继续

resumeAction:function(){

this.enemyGroup.resumeAction();

this.bulletGroup.resumeAction();

this.ufoGroup.resumeAction();

this.hero.onDrag();

this.gameMusic.resume();

this.pause.normalSprite =this.btnSprite[0];

this.pause.pressedSprite =this.btnSprite[1];

this.pause.hoverSprite =this.btnSprite[1];

},

//游戏暂停

pauseAction:function(){

this.enemyGroup.pauseAction();

this.bulletGroup.pauseAction();

this.hero.offDrag();

this.gameMusic.pause();

this.ufoGroup.pauseAction();

this.pause.normalSprite =this.btnSprite[2];

this.pause.pressedSprite =this.btnSprite[3];

this.pause.hoverSprite =this.btnSprite[3];

},

//增加分数

gainScore:function(scoreno){

this.score += scoreno;

//更新 scoreDisplay Label 的文字

this.scoreDisplay.string =this.score.toString();

},

//get分数

getScore:function(){

returnparseInt(this.scoreDisplay.string);

},

//分数写到本地( 当前分 最高分 历史记录)

updateScore:function(){

varcurrentScore =this.scoreDisplay.string;

varscoreData = {

'score':currentScore,

'time': D.common.timeFmt(newDate(),'yyyy-MM-dd hh:mm:ss'),

};

varpreData = cc.sys.localStorage.getItem('score');

varpreTopScore = cc.sys.localStorage.getItem('topScore');

if(!preTopScore ||parseInt(preTopScore)

cc.sys.localStorage.setItem('topScore', currentScore);

}

if(!preData){

preData = [];

preData.unshift(scoreData);

}else{

preData =JSON.parse(preData);

if(!(preDatainstanceofArray)){

preData = [];

}

preData.unshift(scoreData);

}

cc.sys.localStorage.setItem('currentScore', currentScore);

cc.sys.localStorage.setItem('score',JSON.stringify(preData));

},

//炸弹清除敌机

removeEnemy:function(){

this.enemyGroup.node.removeAllChildren();

},

//接到炸弹

getUfoBomb:function(){

if(parseInt(this.bombNoDisplay.string)<3){//多于三个炸弹就不累加

this.bombNoDisplay.string +=1;

}

},

//游戏结束

gameOver:function(){

this.pauseAction();

this.updateScore();

cc.director.loadScene('end');

},

});

以及资源 

这个游戏的视频教程关注后回复“飞机大战”可以领取点击链接加入群聊【cocos/unity交流群】

相关文章

网友评论

    本文标题:cocos creator制作一个小游戏【飞机大战】

    本文链接:https://www.haomeiwen.com/subject/uqpgcctx.html