Author Topic: Help: reactivate a switch without reloading the map  (Read 1584 times)

froggy77

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Help: reactivate a switch without reloading the map
« on: December 07, 2014, 01:15:05 pm »
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.

Code: [Select]
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


Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1176
    • View Profile
Re: Help: reactivate a switch without reloading the map
« Reply #1 on: December 07, 2014, 05:23:25 pm »
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.

froggy77

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: Help: reactivate a switch without reloading the map
« Reply #2 on: December 07, 2014, 10:46:35 pm »
Thank you Christopho  :)
It may be possible to improve the script, but it works. I agree that you correct if you see an error.

Code: [Select]
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