Okay please bear with me here, I have a duck which has a honking animation. This honk attack should make the enemy pest control ‘die’ or more or less gets destroyed.
How do I put the logic wherein the instance of the honk destroys other sprites? The only 2d tutorials I’ve found only leads to sprites colliding with each other.
If anyone knows the specific know-how to this would be greatly appreciated!
I think the easiest way is to use the collision method in a script on your enemy entity. If your enemy has a collision start with your duck, you can start checking for a key press and destroy the entity when needed. If there is a collision end you stop checking for the key press.
So if I were attempting to do this, I would bake this into a separate function that checks both the direction and the distance from the enemy and acts accordingly. You could use a bounding box to check if any enemies are in range and then pass your goingRight variable to the function to see if you’re facing the right way. Then if both of these are true, you play an animation or destroy the sprite.
The problem is that I’ve made a trigger event that whenever the player sprite touches an enemy, the player loses a life and teleports you back to the starting position.
Trigger.prototype.onTriggerEnter = function(entity) {
if (healthcounter >= 1) {
var position = entity.getPosition();
entity.rigidbody.teleport(-1.437, -0.814, 0.417);
healthcounter --;
}
Your enemy will need a rigidbody. That means that it would indeed no longer function as a trigger. You will have to change a few things in your setup to get both functions working.
Maybe you also can a use a raycast as alternative. Remove the enemy that is hit by this raycast when the action key is pressed. The code for that is really simple, but I’m not sure anymore if a rigidbody is needed for this.
In the end it’s also not a problem to lose the trigger functionality of the enemy. You just have to change the on trigger event to an on collision event.
Sorry I got lost again. Researching for examples of raycasting led me to several demos but all of them uses the mouse as the starting point for the raycast. Because of my limited knowledge in scripting, how do I convert their code to a) my sprite becomes the starting point of the raycast, and b) fire at a limited range?
I think I can take advantage of raycasting using tags so that it knows if the rigidbody is an enemy and not accidentally destroy everything in its path.
// inside if statement of key press
if (goingRight) {
var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastRight.getPosition());
}
else {
var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastLeft.getPosition());
}
if (result && result.entity.tags.has("Enemy")){
result.entity.destroy();
}
The code definitely works and the enemies do in fact get destroyed by only the set distance by the 2 child entities. Although I’m quite bothered with the code editor as it shows these:
onGround = true;
if(goingRight) {
this.entity.sprite.play("DuckIdleRight");
var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastRight.getPosition());
}
else{
this.entity.sprite.play("DuckIdleLeft");
var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastLeft.getPosition());
}
if (result && result.entity.tags.has("Enemy")){
result.entity.destroy();
}
}
Here is the code again in its entirety. Should I be worried about it? But it works though.