游戏的制作就是在于开始界面还有死亡界面,另外再加上界面上按钮的设置功能。
说的简单一些吧,游戏开始的界面上需要包括的是开始游戏按钮,按下开始游戏就会跳入界面开始游戏。
首先我们设置一张背景图
var sp = new cc.Sprite(res.pic_4) //res.pic_4就是你的背景图
this.setPosition(cc.winSize.width>>1,cc.winSize.height>>1)
this.addChild(sp)```
```设置上方的开始按钮
var menuFont = new cc.MenuItemFont("START GAME", this.startGame,this)
menuFont.fontSize = 40 ;
menuFont.fontName = "Arial";
menuFont.setColor(cc.color(0,255,0,100))
menuFont.setPosition(-cc.winSize.width>>1,-cc.winSize.height>>1)
var menu = new cc.Menu(menuFont)
this.addChild(menu);
},
startGame : function(){
cc.director.pushScene(new cc.TransitionFlipAngular(1, new HelloWorldScene(),
cc.color(0,255,255)));
},
<div class="se-preview-section-delimiter"></div>
这里要注意的是,背景是重新建立的一个层,但是要显示在最前面,所以需要把这里设置成HelloWorldScene。在main里面的那里设置成这个类的名字,这样一点进去的时候就会显示这个开始界面。
接下来还可以设置游戏里面的背景,就是让背景动起来,这样会看起来有画面感一些,这里也需要照一张图片,然后用这一张变成两张来循环做到画面动的效果
sp1:null,
sp2:null,
moveSpeed:10,
ctor:function(res){
this._super()
this.sp1 = new cc.Sprite("res/beijing.png")
this.sp2 = new cc.Sprite("res/beijing.png")
this.addChild(this.sp1)
this.addChild(this.sp2)
this.sp1.setAnchorPoint(0,0)
this.sp1.setPosition(0,0)
this.sp2.setAnchorPoint(0,0)
this.sp2.setPosition(0, this.sp2.height)
this.scheduleUpdate()
},
update:function(){
this.sp1.y-= this.moveSpeed
this.sp2.y-= this.moveSpeed
if(this.sp1.y< -this.sp1.height){
this.sp1.y = this.sp2.y+this.sp2.height
}
if(this.sp2.y< -this.sp2.height){
this.sp2.y = this.sp1.y+this.sp2.height
}
},
<div class="se-preview-section-delimiter"></div>
在游戏里面设置一个返回键,这个要添加到舞台的时候要注意层级,需添加到道具层级的上方
var menuFont = new cc.MenuItemFont("BACK", this.startGame,this)
menuFont.fontSize = 40 ;
menuFont.fontName = "Arial";
menuFont.setColor(0,255,0,100)
menuFont.setPosition((cc.winSize.width>>1)-100,(cc.winSize.height>>1)-50)
var menu = new cc.Menu(menuFont)
this.addChild(menu);
<div class="se-preview-section-delimiter"></div>
游戏死亡界面的设置,这个层级也需要注意
var Lable_1 = new cc.LabelBMFont("GAME OVER",res.wenzi)
Lable_1.fontSize = 40;
Lable_1.setColor(0,255,0,100)
Lable_1.setPosition(cc.winSize.width>>1,cc.winSize.height>>1)
this.addChild(Lable_1)
Lable_1.visible = false;
this.over = cc.eventManager.addCustomListener(GAME_OVER,function(event){
Lable_1.visible = true;
})