How to specify custom attack sprite

Started by Starlock, December 29, 2016, 06:07:52 PM

Previous topic - Next topic
I'm trying to create a boss that can only be hurt when attacked by a thrown item with a specific sprite but I'm not sure how to call the thrown items sprite in the enemy code.

Code ( lua) Select
function enemy:on_custom_attack_received(attack, sprite)
  if attack == "thrown_item" and attack:get_sprite() == "entities/boss3block" then
  enemy:hurt(1)
  enemy:remove_life(1)
  end
end


This was my attempt but it gives me an error that the get_sprite() is calling a nil value

Try using the sprite value that is given in the function.
Quote from: http://www.solarus-games.org/doc/1.5/lua_api_enemy.html#lua_api_enemy_on_custom_attack_receivedenemy:on_custom_attack_received(attack, sprite)

Called when this enemy receives an attack with a custom effect.

This function is called if you have set consequence of the attack to "custom". You have to define what happens, for example hurting the enemy, making a special reaction, etc.

attack (string): The attack that was received: "sword", "thrown_item", "explosion", "arrow", "hookshot", "boomerang" or "fire".
sprite (sprite): The sprite of this enemy that receives the attack, or nil if the attack does not come from a pixel-precise collision.

So to run the function I would do,
Code (lua) Select
function enemy:on_custom_attack_received(attack, sprite)
  if attack == "thrown_item"
    and sprite:get_animation_set() == "entities/boss3block" then
    enemy:hurt(1)
    enemy:remove_life(1)
  end
end
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

December 29, 2016, 10:05:45 PM #2 Last Edit: December 29, 2016, 10:17:11 PM by MetalZelda
As the documentation says, sprite refers to a sprite of the enemy, not the thrown item, you have to get the thrown item by yourself, in my project, while battling king dodongo for example, the room is only filled with bombs, but he can swallow jars, bushes ...

And you just have to call enemy:hurt(1), enemy:remove_life is handled by hurt()

Yet, I admit that a attack:get_sprite() for this type of function would be very helpful, I don't know how this is doable