Create Timeout Object

From Director Online Wiki
Jump to: navigation, search

It has been quite a problem for many to figure the correct syntax for timeout objects since Director MX 2004 introduced the distinction of scriptExecutionStyle. So here's a simple handler to create timeout objects independent of the current scriptExecutionStyle. Thanks to Alex da Franca for these bits.

on mCreateTimeout theName, theDuration, theHandler, theTarget
  dto = timeout(theName)
  if ilk(dto) = #timeout then
    return dto.new(theDuration, theHandler, theTarget)
  else
    return timeout().new(theName, theDuration, theHandler, theTarget)
  end if
end

Simple example how to use the above in a sprite behavior:

property pTimeout
 
on beginSprite me
  pTimeout = mCreateTimeout("myTimeoutName", 1000, #doSomething, me)
end
 
on endSprite me
  pTimeout.forget()
end
 
on mCreateTimeout theName, theDuration, theHandler, theTarget
  dto = timeout(theName)
  if ilk(dto) = #timeout then
    return dto.new(theDuration, theHandler, theTarget)
  else
    return timeout().new(theName, theDuration, theHandler, theTarget)
  end if
end
 
on doSomething me
  -- do whatever you want to do when the timeout occurs
end