Help: reactivate a switch without reloading the map

Started by froggy77, December 07, 2014, 01:15:05 PM

Previous topic - Next topic
Hello,

I don't understand how to activate/reactivate a simple switch without reloading the map.
I works for crystal blocks, I would like the same behavior for a "light switch" to turn on / turn off the light and I would select my own sprites.

It's the same problem for the switch in the "test map" called Outside B2 (Village) of the tutorial. If I want to disappear again the ladder of the map, how should I do?
Once enabled, the switch (crystal) remains in yellow and can not be hit with the sword.


local map = ...
local game = map:get_game()

function light_switch:on_activated()

  light_tile:set_visible(false)
end

function light_switch:on_inactivated()

  light_tile:set_visible(true)
end


One solution is to deactivate the switch as soon as it is activated. So, when it is activated (in switch:on_activated()), you call switch:set_activated(false), change something in its sprite and trigger whatever mechanism you want.
When the player hits it again, since it was deactivated, the switch:on_activated() is executed again.

From the point of view of the player, the switch has two states. From the point of view of the engine, it is always deactivated.

Thank you Christopho  :)
It may be possible to improve the script, but it works. I agree that you correct if you see an error.

local map = ...
local game = map:get_game()

function light_switch:on_activated()

  if light_tile:is_visible() then
light_tile:set_visible(false)
   else
light_tile:set_visible(true)
  end
  light_switch:set_activated(false)
end

function light_switch:on_inactivated()

  light_switch:get_sprite():set_animation("activated")
end