Advertisement
lavalevel

Finding the mapTable[#] from a touch

Dec 18th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. function placeNote() -- Select a Square !!!
  2.  
  3.  
  4.     -- This removes previous sparkler effects on where you touched.
  5.     for i= 1, # mapNotes do
  6.             sparkler[i]:removeEventListener( "touch", readNote )
  7.     end
  8.  
  9.     --atrace (mapNotes)
  10.  
  11.     mapIsDrawn = false -- probably ignore
  12.     mapNoteNumber = mapNoteNumber + 1 -- ingore
  13.  
  14.     -- This where they go when
  15.         -- someone touches the invisible overlay group.maps.elements.mapTouchArea
  16.  
  17.     touched = function( event )
  18.        
  19.         local cancelMapNote = false
  20.         local t = event.target
  21.         local phase = event.phase
  22.         local tempX = eventX
  23.         local tempY = eventY
  24.  
  25.         if ("began" == phase ) then
  26.  
  27.         t.isFocus = true
  28.         t.x0 = event.x
  29.         t.y0 = event.y
  30.  
  31.         local effect = display.newRect(1,1,16,16)
  32.         effect:setFillColor(tonumber("27", 16), tonumber("207", 16), tonumber("147", 16))
  33.  
  34.         -- BELOW:
  35.         -- find the middle X,Y for the place on
  36.         -- the screen to draw the nifty effect
  37.  
  38.         tempX =  (((math.floor ( (t.x0-33)  / 16 ) )  )  ) +1
  39.         tempY = ((math.floor ( (t.y0-97)  / 16 ) )  ) + 1
  40.  
  41.         --print ("tempx,y" .. tempX, tempY) -- always print stuff
  42.  
  43.         -- below, converts XY based on
  44.         -- the touched screen's X(thetempX),Y(the tempY) of where they touched
  45.         -- The reason I do this is because I use XY to find a place in a table map.
  46.         -- ie. if you are in the very top left hand corner XY=1. if your map is 20x20 and
  47.         -- you want to go down one, then XY=XY + 20. Up the mapTable is XY=XY-20.
  48.  
  49.         mapNotes[mapNoteNumber].MessageXY = (( (tempY-1) * currentMap.mapWidth ) + tempX )
  50. --      io.write ("->" .. (( (tempY-1) * currentMap.mapWidth ) + tempX ) )
  51. --      io.write ("\n\n**************************")
  52. --      io.write ("\nEventX,Y:" .. " X" .. event.x .. " / Y" .. event.y)  -- 33 , 97
  53. --      io.write ("\nMath Check: X " .. math.floor ( (event.x-33)  / 16 )+1 )
  54. --      io.write ("\nMath Check: Y " .. math.floor ( (event.y-97)  / 16 )+1 )
  55.         --print ("mapNote#:" .. mapNoteNumber)
  56.         --print ("tempX" .. tempX )
  57.         --print ("tempY" .. tempY )
  58.  
  59.         if mapNotes[1] ~= nil then
  60.            
  61.             local compare
  62.  
  63.             for i= 1, #mapNotes do
  64.  
  65.                 --print (" THIS MANY MAP NOTES! ")
  66.                
  67.                 if mapNotes[i].MessageXY == (( (tempY-1) * currentMap.mapWidth ) + tempX ) then
  68.                 --print (" Found a match")
  69.                 cancelMapNote = true
  70.                 mapNoteNumber = i
  71.                 end
  72.  
  73.  
  74.             end
  75.  
  76.  
  77.         end
  78.  
  79.  
  80.  
  81.         effect.x =  ( ( math.floor ( (event.x-33)  / 16 ) ) *16 ) + 32 + 8
  82.         effect.y =  ( ( math.floor ( (event.y-97)  / 16 ) ) *16 ) + 96 + 8
  83.  
  84.         group.maps.groups['ScrollMap']:insert( effect )
  85.         effect.alpha = .5
  86.  
  87.         local function effectAnimate(e)
  88.  
  89.             local finishedEffect = function()
  90.             effect:removeSelf()
  91.  
  92.             if cancelMapNote then
  93.  
  94.             else
  95.             table.insert ( mapNotes, { Message = "new Map Note", MessageXY = 0 } )
  96.             mapNotes[mapNoteNumber].MessageXY = (( (tempY-1) * currentMap.mapWidth ) + tempX )
  97.            
  98.             end
  99.             typeNote(mapNotes[mapNoteNumber].Message)
  100.            
  101.            
  102.             end
  103.  
  104.             local fadeDown = function()
  105.             transition.to ( e, { time = 200, alpha=1 , xScale= 1, yScale=1, onComplete = finishedEffect} )
  106.             end
  107.             effect.alpha = 0
  108.             transition.to ( e, { time = 200, alpha=.6 , xScale= 8, yScale=8, onComplete = fadeDown } )
  109.         end
  110.  
  111.         effectAnimate (effect)
  112.  
  113.         end
  114.  
  115.         local function EffectOver()
  116.  
  117.  
  118.         end
  119.  
  120.         if ("ended" ==phase) then
  121.            
  122.         end
  123.  
  124.         t.x0 = nil
  125.         t.y0 = nil
  126.     end
  127.  
  128.     group.maps.elements.btnSpellbook.isVisible = false
  129.     group.maps.elements.btnSpellbook:setEnabled(false, 'up')
  130.  
  131.     group.maps.elements.btnLeft.onTap = returnToGameFromList
  132.  
  133.     -- group.maps.elements.Text_Header.text = ( rosetta:getString("Quests") )
  134.  
  135.     group.maps.elements.Text_Header.text = rosetta:getString( "PLACE A NOTE" )
  136.     group.maps.elements.Text_Sub1.text = ( " " )
  137.    
  138.     group.maps.elements.mapTouchArea.isVisible = true
  139.     group.maps.elements.mapTouchArea.alpha = .3
  140.  
  141.     group.maps.elements.mapTouchArea:addEventListener("touch" , touched)
  142.  
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement