I’m trying to handle the collision of two sprites, i added BoxCollider2D to the sprites.
One sprite can jump (changing the y coordinate through animation), named "character ", the other is a static object, named “key”.
When I overlay one sprite on another, the handler works correctly, but if I put the “key” higher, so that the "character " can reach it from the jump, then nothing works.
code for the "character "
start () {
this.collider = this.node.getComponent(BoxCollider2D);
if( this.collider ) {
this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
onBeginContact( selfCollider: BoxCollider2D, otherCollider: BoxCollider2D, contact: IPhysics2DContact | null ) {
if( otherCollider.name == “key” ){
otherCollider.node.getComponent(“key”).destroyKey();
this.score++;
this.scoreLabel.string = "Score: " + this.score;
}
}
code for the “key”
destroyKey(){
console.log(“handle”);
}