- The variables ex, ey and el should be local to the file, otherwise they are global and all quicksand instances share them.
- The collision test should only react to the hero, not to other entities. Your current code tries to move the hero every time any entity overlaps the quicksand.
- Your code is creating a new collision test at each frame (on_update). You should create the collision test only once. Creating a new movement at each frame is also wrong.
- Starting a movement on the hero is probably a bad idea because an entity cannot have several movements at the same time. If you start a movement, then the player's movement from the keyboard/joypad is lost. And as soon as the player changes the state (like by using the sword), your movement is lost and the keyboard/joypad movement is back. A better approach should be to directly change the hero coordinates towards the quicksand.
local entity = ...
local map = entity:get_map()
local hero = map:get_entity("hero")
local ex, ey, el
local timer
-- Quicksand: entity which slows the hero until
-- he finally falls in
function entity:on_created()
self:create_sprite("entities/quicksand")
self:set_size(32, 32)
self:set_origin(16, 16)
ex, ey, el = self:get_position()
self:get_sprite():set_animation("quicksand")
self:add_collision_test("overlapping", function(quicksand, other)
-- This callback will be repeatedly called while other is overlapping the quicksand
if other:get_type() ~= "hero"
return
end
local hero = other
-- Only do this in some specific states (in particular, don't do it while jumping, flying with the hookshot, etc.)
if hero:get_state() ~= "free" and hero:get_state() ~= "sword loading" ........(and maybe others)......... then
return
end
-- Move the hero towards the quicksand's center every 200 ms while he is overlapping it.
if timer == nil then
timer = sol.timer.start(self, 200, function()
hero:set_xy(........) -- Some coordinates one pixel towards the center of the quicksand
timer = nil -- This variable "timer" ensures that only one timer is running.
end)
end
end)
end
I did not test it, but this is an idea. I hope it can help!
(By the way, I still need to play your game! Sorry for the long time!)