Don't like ads? PRO users don't see any ads ;-)

Swiping

By: lavalevel on May 8th, 2012  |  syntax: Lua  |  size: 1.98 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. onScreenSwipe = function( event )
  2.     local t = event.target
  3.         local phase = event.phase
  4.         -- local howManyHits = event.numTaps  
  5.         local minmov = 12       -- Minimum movement that registers
  6.        
  7.         if "began" == phase then
  8.                 t.isFocus = true
  9.                 t.x0 = event.x
  10.                 t.y0 = event.y
  11.         elseif ("ended" == phase or "cancelled" == phase) and t.x0 ~= nil and t.y0 ~= nil then
  12.                 -- print ("Number of taps: " .. event.numTaps)
  13.  
  14.                 if (event.x - t.x0) > minmov then               -- Swipe right
  15.                                 io.write("\nSWIPE RIGHT!!!!     <-----I====================>")
  16.                                 Runtime:dispatchEvent{name='playerHasMoved'}
  17.                         if npcInFrontOf > 0 then
  18.                                 if currentMap.monsterList[npcInFrontOf].npcPurpose == "quest" or currentMap.monsterList[npcInFrontOf].npcPurpose == "talk" or currentMap.monsterList[npcInFrontOf].npcPurpose == "sell"  then
  19.                                                 talkToNpc()
  20.                                 else
  21.                                 swingSwordRight()
  22.                                 end
  23.                         else
  24.                                 swingSwordRight()
  25.                         end
  26.  
  27.                        
  28.                         --haltPlayerInput()
  29.  
  30.                 elseif (event.x - t.x0) < -minmov then  -- Swipe Left
  31.  
  32.                         io.write ("\nSWIPE LEFT!!!!     <-----I====================>")
  33.                         Runtime:dispatchEvent{name='playerHasMoved'}
  34.                         if npcInFrontOf > 0 then
  35.                                 if currentMap.monsterList[npcInFrontOf].npcPurpose == "quest" or currentMap.monsterList[npcInFrontOf].npcPurpose == "talk" or currentMap.monsterList[npcInFrontOf].npcPurpose == "sell"  then
  36.                                                 talkToNpc()
  37.                                 else
  38.                                 swingSwordLeft()
  39.                                 end
  40.                         else
  41.                                 swingSwordLeft()
  42.                         end
  43.                        
  44.  
  45.  
  46.                        
  47.  
  48.                 else           
  49.                         Runtime:dispatchEvent{name='playerHasMoved'}
  50.                         if npcInFrontOf > 0 then
  51.                                
  52.                                 print ("ScreenPressed")
  53.                                 if currentMap.monsterList[npcInFrontOf].npcPurpose == "quest" or currentMap.monsterList[npcInFrontOf].npcPurpose == "talk" or currentMap.monsterList[npcInFrontOf].npcPurpose == "sell"  then
  54.                                                 talkToNpc()
  55.                                 else
  56.                                 useMagicOrShield()
  57.                                 end
  58.                         else
  59.                                 useMagicOrShield()
  60.                         end
  61.                        
  62.                                
  63.                         -- TalkToNpc()
  64.  
  65.                 end
  66.                        
  67.                 display.getCurrentStage():setFocus( nil )
  68.  
  69.                 t.isFocus = false    
  70.  
  71.         end
  72.            
  73.         return true
  74. end