版本:3.4.2
物体信息如下。
脚本如下,却没有事件触发,先感谢回答。有 demo 也行
@ccclass('Player')
export class Player extends Component {
@property(CCFloat)
private speed: number = 7;
@property(Node)
private background: Node;
private rigidBody: RigidBody2D;
onLoad() {
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
input.on(Input.EventType.TOUCH_START, this.fire.bind(this), this.background);
this.rigidBody = this.getComponent(RigidBody2D);
console.log('Loading...');
}
start() {
const collider = this.getComponent(Collider2D);
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
// 注册全局碰撞回调函数
if (PhysicsSystem2D.instance) {
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called once when two colliders begin to contact
console.log('onBeginContact');
}
onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called once when the contact between two colliders just about to end.
console.log('onEndContact');
}
onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called every time collider contact should be resolved
console.log('onPreSolve');
}
onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called every time collider contact should be resolved
console.log('onPostSolve');
}
fire(event: EventTouch) {
const targetLocation = event.getUILocation();
const position = this.node.position;
const forceDir = targetLocation.subtract(new Vec2(position.x, position.y)).normalize();
this.rigidBody.linearVelocity = forceDir.multiplyScalar(this.speed);
}
onDestroy() {
input.off(Input.EventType.TOUCH_START, this.fire, this.background);
}
onCollisionEnter(other: any, self: any) {
console.log('onCollisionEnter');
}
}
1 个帖 - 1 位参与者