Advertisement
Kozz

Barras

Apr 24th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. local widget = require( "widget" )
  2.  
  3.  
  4. local grupo = display.newGroup()  
  5. local hit = 0
  6. local bar = 0
  7. local grupo2 = display.newGroup()  
  8. local hit2 = 0
  9. local bar2 = 0
  10.  
  11. options = {
  12.     x=display.contentCenterX,
  13.     y=display.contentCenterY + display.contentCenterY / 2 + 50,
  14.     label="Restart",
  15.     fontSize=50,
  16.     width=500
  17. }
  18.  
  19.  
  20. local barra = display.newRect( display.contentCenterX/2, display.contentCenterY + display.contentCenterY/2, 100, 500 )
  21. barra:setFillColor( 0.5,0.5,0.5 )
  22. barra.anchorX = 0.5
  23. barra.anchorY = 1
  24. local fillBarra = display.newRect( barra.x, barra.y, 100, 0 )
  25. fillBarra:setFillColor( 0,0,1 )
  26. fillBarra.anchorX = 0.5
  27. fillBarra.anchorY = 1
  28.  
  29. grupo:insert( barra )
  30. grupo:insert( fillBarra )
  31.  
  32. local barra2 = display.newRect( display.contentCenterX + display.contentCenterX / 2, display.contentCenterY + display.contentCenterY/2, 100, 500 )
  33. barra2:setFillColor( 0.5,0.5,0.5 )
  34. barra2.anchorX = 0.5
  35. barra2.anchorY = 1
  36. local fillBarra2 = display.newRect( barra2.x, barra2.y, 100, 0 )
  37. fillBarra2:setFillColor( 1,0,0 )
  38. fillBarra2.anchorX = 0.5
  39. fillBarra2.anchorY = 1
  40.  
  41. grupo2:insert( barra2 )
  42. grupo2:insert( fillBarra2 )
  43.  
  44. function addHit( hit )
  45.     bar = bar + hit
  46.     if bar >= 100 then
  47.         print( "1 morreu" )
  48.         bar = 100
  49.         timer.pause( tempo1 )
  50.         timer.pause( tempo2 )
  51.         Runtime:removeEventListener("touch",bater)
  52.         Runtime:removeEventListener("touch",bater2)
  53.         options.label = "2 Ganhou"
  54.         btn = widget.newButton( options )
  55.         local function restart( event )
  56.             bar = 0
  57.             bar2 = 0
  58.             print( bar2 )
  59.             timer.resume( tempo1 )
  60.             timer.resume( tempo2 )
  61.             Runtime:addEventListener("touch",bater)
  62.             Runtime:addEventListener("touch",bater2)
  63.             btn:removeEventListener( "tap", restart )
  64.             display.remove( btn )
  65.             btn = nil
  66.         end
  67.         btn:addEventListener( "tap", restart)
  68.     end
  69.     transition.to( fillBarra, {height=barra.height / 100 * bar, time = 100} )
  70.    
  71. end
  72.  
  73. function addHit2( hit2 )
  74.     bar2 = bar2 + hit2
  75.     if bar2 >= 100 then
  76.         print( "2 morreu" )
  77.         bar2 = 100
  78.         timer.pause( tempo1 )
  79.         timer.pause( tempo2 )
  80.         Runtime:removeEventListener("touch",bater)
  81.         Runtime:removeEventListener("touch",bater2)
  82.         options.label = "1 Ganhou"
  83.         btn = widget.newButton( options )
  84.         local function restart( event )
  85.             bar = 0
  86.             bar2 = 0
  87.             timer.resume( tempo1 )
  88.             timer.resume( tempo2 )
  89.             Runtime:addEventListener("touch",bater)
  90.             Runtime:addEventListener("touch",bater2)
  91.             btn:removeEventListener( "tap", restart )
  92.             display.remove( btn )
  93.             btn = nil
  94.         end
  95.         btn:addEventListener( "tap", restart)
  96.     end
  97.     transition.to( fillBarra2, {height=barra2.height / 100 * bar2, time = 100} )
  98.    
  99. end
  100.  
  101. function bater( event )
  102.     if event.phase == "ended" then
  103.         if event.y < display.contentCenterY and event.x < display.contentCenterX then
  104.             addHit( 10 )
  105.         elseif event.y > display.contentCenterY and event.x < display.contentCenterX then
  106.             addHit( -10 )
  107.             addHit2( 10 )
  108.         end
  109.     end
  110. end
  111. function bater2( event )
  112.     if event.phase == "ended" then
  113.         if event.y < display.contentCenterY and event.x > display.contentCenterX then
  114.             addHit2( 10 )
  115.         elseif event.y > display.contentCenterY and event.x > display.contentCenterX then
  116.             addHit2( -10 )
  117.             addHit (10)
  118.         end
  119.     end
  120. end
  121.        
  122. Runtime:addEventListener( "touch", bater )
  123. Runtime:addEventListener( "touch", bater2 )
  124.  
  125. function subirBarra( event )
  126.     addHit(1)
  127. end
  128. function subirBarra2( event )
  129.     addHit2(1)
  130. end
  131.  
  132. tempo1 = timer.performWithDelay( 100, subirBarra , -1 )
  133. tempo2 = timer.performWithDelay( 100, subirBarra2 , -1 )
  134.  
  135. --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement