十二 手游开发神器 cocos2d-x editor 之游戏暂停悬浮层
2014-01-25 15:36
417 查看
进入游戏主场景,游戏需要临时暂停、重新选关、重新玩等等,所以玩家点击暂停按钮,弹出一个选择悬浮层,这一节我们来实现;
效果如下:
点击右上角游戏暂停;
返回回到开始界面;
代码下载:http://www.kuaipan.cn/file/id_25348935635744972.htm?source=1
首先创建一个PauseLayer.ccbx,设计如下图,包括一个精灵和三个按钮,一个返回按钮、重新开始按钮、选关按钮;
给每一个MenuItem安排点击事件,指定target,图片资源;
打开MainLayer.ccbx,添加暂停按钮;
打开MainLayer.js,首先给暂停按钮事件;
然后给返回和重新开始安排点击事件;
MainLayer,js全部代码;
点击运行,效果图在最上面;
下一篇文章 我会介绍cocos2d-x editor的选关界面 笔者(李元友)
资料来源:cocos2d-x editor
效果如下:
点击右上角游戏暂停;
返回回到开始界面;
代码下载:http://www.kuaipan.cn/file/id_25348935635744972.htm?source=1
首先创建一个PauseLayer.ccbx,设计如下图,包括一个精灵和三个按钮,一个返回按钮、重新开始按钮、选关按钮;
给每一个MenuItem安排点击事件,指定target,图片资源;
打开MainLayer.ccbx,添加暂停按钮;
打开MainLayer.js,首先给暂停按钮事件;
MainLayer.prototype.onPauseClicked = function () { //点击暂停游戏 this.pausedLayer = cc.BuilderReader.loadAsNodeFrom("", "PauseLayer", this); this.pausedLayer.setPosition(cc.p(0, 0)); this.pausedLayer.setZOrder(200); this.rootNode.addChild(this.pausedLayer); this.paused = true; cc.AudioEngine.getInstance().stopMusic(); cc.Director.getInstance().pause(); }
然后给返回和重新开始安排点击事件;
MainLayer.prototype.onRenewClicked = function () { //返回到开始界面 cc.Director.getInstance().resume(); cc.log("onRenewClicked"); cc.BuilderReader.runScene("", "StartLayer"); } MainLayer.prototype.onReplayClicked = function () { //新游戏 cc.Director.getInstance().resume(); cc.log("onReplayClicked"); cc.BuilderReader.runScene("", "MainLayer"); }
MainLayer,js全部代码;
//
// CleanerScoreScene class
//
var MainLayer = function () {
cc.log("MainLayer")
this.scoreLabel = this.scoreLabel || {};
this.monster = this.monster || {};
this.score = 123;
};
MainLayer.prototype.onDidLoadFromCCB = function () {
if (sys.platform == 'browser') {
this.onEnter();
}
else {
this.rootNode.onEnter = function () {
this.controller.onEnter();
};
}
this.rootNode.schedule(function (dt) {
this.controller.onUpdate(dt);
});
this.rootNode.onExit = function () {
this.controller.onExit();
};
this.rootNode.onTouchesBegan = function (touches, event) {
this.controller.onTouchesBegan(touches, event);
return true;
};
this.rootNode.onTouchesMoved = function (touches, event) {
this.controller.onTouchesMoved(touches, event);
return true;
};
this.rootNode.onTouchesEnded = function (touches, event) {
this.controller.onTouchesEnded(touches, event);
return true;
};
this.rootNode.setTouchEnabled(true);
};
MainLayer.prototype.onEnter = function () {
var flowerParticle = cc.ParticleSystem.create("Resources/particles/flower.plist");
flowerParticle.setAnchorPoint(cc.p(0.5, 0.5));
flowerParticle.setPosition(cc.p(60, 160));
flowerParticle.setPositionType(1);
this.monster.addChild(flowerParticle);
cc.AudioEngine.getInstance().playMusic("Resources/sounds/bg_music.mp3", true);
}
MainLayer.prototype.monsterMove = function (x, y) {
this.monster.stopAllActions();
cc.AnimationCache.getInstance().addAnimations("Resources/snow_frame.plist");//添加帧动画文件
var action0 = cc.Sequence.create(cc.MoveTo.create(5, cc.p(x, y))); //向前移动
var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("monster")); //获取帧动画
var action1 = cc.Repeat.create(actionFrame, 90000);
var action2 = cc.Spawn.create(action0, action1); //同步动画
this.monster.runAction(action2);
}
MainLayer.prototype.createParticle = function (name, x, y) {
var particle = cc.ParticleSystem.create("Resources/particles/" + name + ".plist");
particle.setAnchorPoint(cc.p(0.5, 0.5));
particle.setPosition(cc.p(x, y));
particle.setPositionType(1);
particle.setDuration(3);
this.rootNode.addChild(particle);
}
MainLayer.prototype.onUpdate = function (dt) {
this.score += dt;
this.scoreLabel.setString(Math.floor(this.score));
}
MainLayer.prototype.onExitClicked = function () {
cc.log("onExitClicked");
}
MainLayer.prototype.onExit = function () {
cc.log("onExit");
}
MainLayer.prototype.onRenewClicked = function () { //返回到开始界面 cc.Director.getInstance().resume(); cc.log("onRenewClicked"); cc.BuilderReader.runScene("", "StartLayer"); } MainLayer.prototype.onReplayClicked = function () { //新游戏 cc.Director.getInstance().resume(); cc.log("onReplayClicked"); cc.BuilderReader.runScene("", "MainLayer"); }
/*MainLayer.prototype.onReturnClicked = function () { //返回游戏
cc.log("onReturnClicked");
if (this.paused) {
if (this.pausedLayer) {
this.pausedLayer.removeFromParent();
this.pausedLayer = null;
}
cc.Director.getInstance().resume();
this.paused = false;
}
}*/
MainLayer.prototype.onPauseClicked = function () { //点击暂停游戏 this.pausedLayer = cc.BuilderReader.loadAsNodeFrom("", "PauseLayer", this); this.pausedLayer.setPosition(cc.p(0, 0)); this.pausedLayer.setZOrder(200); this.rootNode.addChild(this.pausedLayer); this.paused = true; cc.AudioEngine.getInstance().stopMusic(); cc.Director.getInstance().pause(); }
MainLayer.prototype.onTouchesBegan = function (touches, event) {
var loc = touches[0].getLocation();
}
MainLayer.prototype.onTouchesMoved = function (touches, event) {
cc.log("onTouchesMoved");
}
MainLayer.prototype.onTouchesEnded = function (touches, event) {
cc.log("onTouchesEnded");
var loc = touches[0].getLocation();
cc.AudioEngine.getInstance().playEffect("Resources/sounds/bomb.mp3", false);
this.monsterMove(loc.x, loc.y);
this.createParticle("around", loc.x, loc.y);
}
点击运行,效果图在最上面;
下一篇文章 我会介绍cocos2d-x editor的选关界面 笔者(李元友)
资料来源:cocos2d-x editor
相关文章推荐
- 十二 手游开发神器 cocos2d-x editor 之游戏暂停悬浮层
- 十四-2 手游开发神器 cocos2d-x editor 之串联游戏流程
- 十四 手游开发神器 cocos2d-x editor 之串联游戏流程
- 四 手游开发神器 cocos2d-x editor 游戏框架介绍
- 四 手游开发神器 cocos2d-x editor 游戏框架介绍
- 十四 手游开发神器 cocos2d-x editor 之串联游戏流程 推荐
- 手游开发神器 cocos2d-x editor 教程聚合和代码下载
- 十一 手游开发神器 cocos2d-x editor 之音乐和音效
- 三 手游开发神器 cocos2d-x editor 之基础工具 intellij idea
- 十一 手游开发神器 cocos2d-x editor 之音乐和音效
- 三 手游开发神器 cocos2d-x editor 之基础工具 intellij idea
- 九 手游开发神器 cocos2d-x editor 之粒子效果(ParticleEditor)
- 三 手游开发神器 cocos2d-x editor 之基础工具 intellij idea
- 六 手游开发神器 cocos2d-x editor 之场景切换
- 九 手游开发神器 cocos2d-x editor 之粒子效果(ParticleEditor)
- 六 手游开发神器 cocos2d-x editor 之场景切换
- 八 手游开发神器 cocos2d-x editor 之动画和帧动画
- 八 手游开发神器 cocos2d-x editor 之动画和帧动画
- 手游开发神器cocos2d-x editor for lua 之配置lua环境和运行hellolua
- 五 手游开发神器 cocos2d-x editor 之贴图(TexturePacker)