0、这是层级管理器:
new node是一个空节点,mm是人物,block是障碍物。
这是场景:
1、给人物mm,物体block,分别添加物理刚体RigidBody和碰撞体PhysicalBoxCollider。
(1)mm重力给到0,因为我们是俯视2d,背景就是地面,用不到重力。
记得禁止mm旋转。
勾选这个,即可在场景编辑器中编辑碰撞器
(2)将碰撞器大小改为这样,下面的block也是这样。
(绿色框是Collider)
(因为我做的是俯视2D,所以这么做,正视图2D或其他,结合实际情况考虑)
(3)block设为静态,它一般是地图中的房子、树木等等不能移动的物体。
2、给人物挂载控制脚本:
// Learn TypeScript:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Vec2)
speed: cc.Vec2 = cc.v2(200, 200);
isA: boolean = false;
isD: boolean = false;
isW: boolean = false;
isS: boolean = false;
onLoad() {
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
// this.schedule(this.moveto);
}
onKeyDown(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
this.isA = true;
// this.node.setPosition(cc.v2(this.node.getPosition().x - this.speed.x, this.node.getPosition().y));
break;
case cc.macro.KEY.d:
this.isD = true;
break;
case cc.macro.KEY.w:
this.isW = true;
break;
case cc.macro.KEY.s:
this.isS = true;
break;
}
}
onKeyUp(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
this.isA = false;
// this.node.setPosition(cc.v2(this.node.getPosition().x - this.speed.x, this.node.getPosition().y));
break;
case cc.macro.KEY.d:
this.isD = false;
break;
case cc.macro.KEY.w:
this.isW = false;
break;
case cc.macro.KEY.s:
this.isS = false;
break;
}
}
update(dt: number) {
this.moveto();
// cc.log('x ' + this.isA + this.isD);
// cc.log('y ' + this.isW + this.isS);
// cc.log('x ' + this.node.getComponent(cc.RigidBody).linearVelocity.x);
// cc.log('y ' + this.node.getComponent(cc.RigidBody).linearVelocity.y);
}
//角色移动控制
moveto() {
let velocity = this.node.getComponent(cc.RigidBody).linearVelocity;
if (!this.isA && !this.isD) {
velocity.x = 0;
} else {
velocity.x = (this.isA ? -this.speed.x : 0) + (this.isD ? this.speed.x : 0);
}
if (!this.isW && !this.isS) {
velocity.y = 0;
} else {
velocity.y = (this.isW ? this.speed.y : 0) + (this.isS ? -this.speed.y : 0);
}
// if (velocity.x == 0 && velocity.y == 0) {
// this.node.getComponent(sp.Skeleton).setAnimation(0, 'idle', true);
// } else {
// this.node.getComponent(sp.Skeleton).setAnimation(0, 'walk2', true);
// }
//翻转
if (velocity.x < 0) {
this.node.scaleX = 1;
} else if (velocity.x > 0) {
this.node.scaleX = -1;
}
this.node.getComponent(cc.RigidBody).linearVelocity = velocity;
}
onDestroy() {
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
}
}
设置一下速度:
3、第二步之后,角色还不能动,因为还要开启物理系统:
给根节点添加脚本:
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property({ tooltip: "是否调试模式" })
private isDebug: boolean = false;
@property(cc.Node)
player: cc.Node = null;
@property(cc.Node)
blocks: cc.Node = null;
onLoad() {
cc.director.getPhysicsManager().enabled = true;//启用物理系统
//是否启用绘制调试
if (this.isDebug) {
cc.director.getPhysicsManager().debugDrawFlags =
cc.PhysicsManager.DrawBits.e_aabbBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit;
} else {
cc.director.getPhysicsManager().debugDrawFlags = 0;
}
}
update(dt) {
if (this.player && this.blocks) {
// 获取节点 A 和节点 B 在世界坐标系下的位置
// let posA = this.player.convertToNodeSpaceAR(this.player.position);
// let posB = this.blocks.convertToNodeSpaceAR(this.blocks.position);
let posA = this.player.position;
let posB = this.blocks.position;
if (posA.y > posB.y) {
this.player.zIndex = this.blocks.zIndex - 1;
} else {
this.player.zIndex = this.blocks.zIndex + 1;
}
cc.log('a' + posA.y);
cc.log('b' + posB.y);
}
}
}
把mm和block拉过去。根节点脚本的update里面写的是判断mm和block的位置,当mm走到block前面来时,它们的渲染顺序发生改变(即mm走到前面来,就会挡住block)。
4、点击运行,即可用wsad控制mm移动:
并且mm走到前面来时会挡住block。