eardrums
Member
- Joined
- May 27, 2010
- Messages
- 101
- Solutions
- 1
- Reaction score
- 11
Hello,
I'm trying to make a script that creates an animation that loops on step in and then stops on step out but I can't do get the event to stop on step out. it keeps looping even when i step off the tile. Anyone know what the issue is?
I'm trying to make a script that creates an animation that loops on step in and then stops on step out but I can't do get the event to stop on step out. it keeps looping even when i step off the tile. Anyone know what the issue is?
LUA:
local animationTimers = {} -- Table to store animation timers
-- Function to start the animation loop
function startAnimation(position)
-- Function to send the magic effect and reschedule itself
local function animate()
position:sendMagicEffect(244) -- Effect to indicate tile animation
-- Schedule the next animation
animationTimers[position] = addEvent(animate, 2000) -- Execute every 2 seconds
end
-- Start the animation loop
animate()
end
-- Function to stop the animation loop
function stopAnimation(position)
if animationTimers[position] then
stopEvent(animationTimers[position]) -- Stop the animation event
animationTimers[position] = nil -- Remove the reference to the event
end
end
-- Function triggered when a player steps on the tile
function onStepIn(creature, item, position, fromPosition)
if item:getActionId() == 171 then
local newTilePosition = Position(418, 1707, 7)
local newTileId = 31115
local newTileActionId = 172
-- Create the new tile with ID 31115 at the specified position
local newTile = Game.createItem(newTileId, 1, newTilePosition)
if newTile then
newTile:setActionId(newTileActionId)
end
-- Start looping animation
startAnimation(newTilePosition)
end
return true
end
-- Function triggered when a player steps off the tile
function onStepOut(creature, item, position, fromPosition)
if item:getActionId() == 171 then
local tilePosition = Position(418, 1707, 7)
local oldTileId = 31115
local newTileId = 31114
local actionIdToKeep = 172
-- Stop looping animation
stopAnimation(tilePosition)
-- Check if the tile with ID 31115 exists at the specified position and has the correct action ID
local tile = Tile(tilePosition):getItemById(oldTileId)
if tile and tile:getActionId() == actionIdToKeep then
-- Remove the tile with ID 31115 and create a new tile with ID 31114 but keep the action ID
tile:remove()
local newTile = Game.createItem(newTileId, 1, tilePosition)
if newTile then
newTile:setActionId(actionIdToKeep)
end
end
end
return true
end