Advertisement
Hendrix000007

dispatching events

Feb 22nd, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1.     ------------------------
  2.     --dispatchEvent
  3.     -----------------------
  4.     --[[
  5.     I have a scenario where i have a indicator that will change its image based on the score until the score is 1000 and the indicator shows full health in form of an image
  6.     --]]
  7.      
  8.     local scoreImg = display.newImage( "image.png" )
  9.      
  10.     local myListener = function( event )
  11.             if event.name == "health" and event.type == "low" then
  12.                     scoreImg.image = "image2.png"
  13.             end
  14.             return scoreImg
  15.     end
  16.      
  17.     image:addEventListener( "myEventType", myListener )
  18.      
  19.     -- in side an if closure where I check the score I put i.e.
  20.      
  21.     ---------Just an example of use inside a function ----------------
  22.      local function checkScore()
  23.         if ( score > 500 and score < 750 ) then
  24.                 local event = { name="health", type="low" }
  25.                 scoreImg:dispatchEvent( event )
  26.         elseif ( score > 750 and score < 900 ) then
  27.             --whatever.....
  28.         end
  29.     end
  30.  
  31. Runtime:addEventListener( "enterFrame", checkScore )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement