附上Player控制代码
cc.Class({
extends: cc.Component,
properties: {
// Player跳跃高度
jumpheight: 0,
// Player跳跃持续时间
jumpDuration: 0,
//Player状态
state: ‘Run’,
//初始蓄力跳跃为关闭状态
is_gather_strength: false,
speed: cc.v2(0, 0),//速度
},
onLoad: function () {
this.node.on('touchstart', function (event) {//当触摸事件发生的时候
this.jumpheight = 0;//设置跳跃高度为0
this.is_gather_strength = true;//设置可以蓄力跳跃
this.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
}, this);
this.node.on('touchend', function (event) {//当触摸事件结束的时候
this.is_gather_strength = false;//设置不可以蓄力跳跃
this.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
if (this.jumpheight > 300) {//如果高度大于1000
this.jumpheight = 300;//让跳跃高度等于1000
}
}, this);
this.collisionY = 0;
this.prePosition = cc.v2();
this.preStep = cc.v2();
this.touchingNumber = 0;
},
onEnable: function () {
cc.director.getCollisionManager().enabled = true;//开启碰撞检测
cc.director.getCollisionManager().enabledDebugDraw = true;//开启碰撞体框架
},
onDisable: function () {
cc.director.getCollisionManager().enabled = false;//关闭碰撞检测
cc.director.getCollisionManager().enabledDebugDraw = false;//关闭碰撞体框架
},
//Player跑
run: function () {
this.getComponent(cc.Animation).play('Cat_Run');//播放Player跑步动画
this.state = 'Run';//设置Player的状态为跑步
},
// Player跳跃
jump: function () {
if (this.state == 'Run') {//如果Player的状态是跑步的话
this.state = 'Jump';//设置Player的状态为跳跃
this.getComponent(cc.Animation).stop();//停止播放跑步的动画
this.getComponent(cc.Animation).play('Cat_Jump');//播放Player跳跃动画
// 开始执行动作 顺序执行 用跳跃的方式移动到指定的距离
this.node.runAction(cc.sequence(cc.jumpBy(this.jumpDuration, cc.p(0, 0), this.jumpheight, 1),//跳跃持续时间和跳跃的次数
cc.callFunc(function () {
this.run();//Player继续跑步
}, this)));
}
},
start() {
},
//如果进入碰撞体的时候
onCollisionEnter: function (other, self) {
//如果碰到的是障碍物
if (other.tag == 2 && self.tag == 1) {
cc.director.loadScene('Over');//加载game over的场景
this.getComponent(cc.Animation).stop();//停止播放动画
}
else if (other.tag == 3 && self.tag == 1) {
}
this.touchingNumber++;
//1st step
var otherAabb = other.world.aabb;
var otherPreAabb = other.world.preAabb.clone();
var selfAabb = self.world.aabb;
var selfPreAabb = self.world.preAabb.clone();
//2nd step
selfPreAabb.y = selfAabb.y;
otherPreAabb.y = otherAabb.y;
if (cc.Intersection.rectRect(selfPreAabb, otherPreAabb)) {
if (this.speed.y < 0 && (selfPreAabb.yMax > otherPreAabb.yMax)) {
this.node.y = otherPreAabb.yMax - this.node.parent.y;
this.getComponent(cc.Animation).stop();//停止播放跑步的动画
this.getComponent(cc.Animation).play('Cat_Jump');//播放Player跳跃动画
jumpheight = 0;
this.collisionY = -1;
}
else if (this.speed.y > 0 && (selfPreAabb.yMin < otherPreAabb.yMin)) {
this.node.y = otherPreAabb.yMin - selfPreAabb.height - this.node.parent.y;
this.collisionY = 1;
}
this.speed.y = 0;
other.touchingY = true;
}
},
onCollisionExit: function (other) {
this.touchingNumber--;
if (other.touchingY) {
other.touchingY = false;
other.collisionY = 0;
}
},
update(dt) {
this.prePosition.x = this.node.x;
this.prePosition.y = this.node.y;
this.preStep.x = this.speed.x * dt;
this.preStep.y = this.speed.y * dt;
this.node.x += this.speed.x * dt;
this.node.y += this.speed.y * dt;
}
});
