游戏分析
![4933701-2f166c7f69e2e5aa.png](https://i-blog.csdnimg.cn/blog_migrate/9dac55a2f83dcd2a200f0ca0458ba399.png)
游戏分析
场景布局
相关资源
![4933701-58efe6e38814ba05.png](https://i-blog.csdnimg.cn/blog_migrate/a664bbd75467b547ffae5bc39ccea07b.png)
资源
开始界面(Welcome)
![4933701-f6e5a57d44f9843d.png](https://i-blog.csdnimg.cn/blog_migrate/7d69705793a859a3f4c69f61104ee338.png)
Welcome
主界面(Main)
![4933701-eb8c9ab76833e8b5.png](https://i-blog.csdnimg.cn/blog_migrate/9d6363dacd7c89f5139c3ca087e40c85.png)
Main
结束界面(Gameover)
![4933701-f1fac270a254434d.png](https://i-blog.csdnimg.cn/blog_migrate/afebea18897dc1302c2bcf90885084e6.png)
Gameover
主角设置
主角跳动
# Main.js
cc.Class({
extends: cc.Component,
properties: {
//主角
player:{
default:null,
type:cc.Node
},
//跳跃高度
jumpHeight:30,
//墙壁宽度
wallWidth:80,
},
//主角跳动,在左边则向右移动,在右边则向左边移动。
playerMoveLeft:function(){
var playerY = this.player.getPositionY();
var position = -this.node.width/2 + this.wallWidth;
//判断主角当前旋转角度
if(this.player.rotationY == 0){
//向右微小移动
var go1 = cc.moveTo(0.1, cc.p(position + this.jumpHeight, playerY));
var go2 = cc.moveTo(0.1, cc.p(position, playerY));
var goSequence = cc.sequence(go1, go2);
//在右边跳动,执行动作序列
this.player.runAction(goSequence);
}else{
//向左移动
var goLeft = cc.moveTo(0.2, cc.p(position, playerY));
this.player.rotationY = 0;//向左翻转0
this.player.runAction(goLeft);
}
},
playerMoveRight:function(){
var playerY = this.player.getPositionY();
var position = this.node.width/2 - this.wallWidth;
//判断主角是从左向右跳动还是一直在右边跳动
if(this.player.rotationY == 180){
//向右微小移动
var go1 = cc.moveTo(0.1, cc.p(position - this.jumpHeight, playerY));
var go2 = cc.moveTo(0.1, cc.p(position, playerY));
var goSequence = cc.sequence(go1, go2);
//在右边跳动,执行动作序列
this.player.runAction(goSequence);
}else{
//在左边向右边跳动
var goRight = cc.moveTo(0.2, cc.p(position, playerY))
this.player.rotationY = 180;//向右翻转180
this.player.runAction(goRight);
}
},
//监听鼠标键盘事件
setInputControl:function(){
var self = this;
var listener = {
event:cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan:function(ele, evt){
//cc.log('began',ele,evt);
//获取玩家点击坐标
var target = evt.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(ele.getLocation());
if(locationInNode.x > self.node.width/2){
cc.log(locationInNode.x);
//点击屏幕右边,向右移动
self.playerMoveRight();
}else{
//点击屏幕左边,向左移动。
self.playerMoveLeft();
}
//cc.log(locationInNode);
},
onTouchMoved:function(ele, evt){
cc.log('moved',ele,evt);
},
onTouchEnd:function(ele, evt){
cc.log('end',ele,evt);
}
};
//注册到事件管理器中
cc.eventManager.addListener(listener, self.node);
},
onLoad () {
this.setInputControl();
//初始化主角起始位置
this.player.setPosition(-this.node.width/2+this.wallWidth, this.node.height/2-180);
}
});
错误处理
cc._EventListenerTouchOneByOne.checkAvailable(): Invalid EventListenerTouchOneByOne!
-----------------------------------------------------------------------------------------------------------------
//监听鼠标键盘事件
setInputControl:function(){
var self = this;
var listener = {
event:cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan:function(ele, evt){
cc.log('began',ele,evt);
},
onTouchMoved:function(ele, evt){
cc.log('moved',ele,evt);
},
onTouchEnd:function(ele, evt){
cc.log('end',ele,evt);
}
};
//注册到事件管理器中
cc.eventManager.addListener(listener, self.node);
},