Hi All,
I have a series of blue squares placed in a row on the screen. When a blue square is selected (tap event), it should be replaced with a yellow square. Currently, when a square is tapped, it is replaced with a yellow square, but the x,y coordinates of the yellow square are set to the x,y location of the tap event, not the x,y coordinates of the blue square that was tapped. Thus, it doesn’t fit perfectly over the original square, and the blue square underneath can be seen. (See screenshot image). I’ve spent hours on this. Can anyone assist me in figuring this out? Thanks in advance! Tom
Here’s my current code…
display.setStatusBar( display.HiddenStatusBar ) numSquares = 5 numSpaces = 6 totalsquareWidth = 0.9 \* display.contentWidth totalSpaceWidth = 0.1 \* display.contentWidth oneSquareWidth = 80 oneSpaceWidth = 20 local myLocation local function squareTapped(event) square = display.newImageRect("../images/1024x1024-yellow-square.png", oneSquareWidth, oneSquareWidth ) square.x = event.x -- Need x coord of blue square square.y = event.y -- Need y coord of blue square end squares = {} -- declare a table that all the square objects will be saved in local squareIndex = 0 for horzCount = 1, numSquares do -- will loop until the number represented by numSquares is reached squareIndex = squareIndex + 1 local square = display.newImageRect("../images/1024x1024ice-blue-square.png", oneSquareWidth, oneSquareWidth ) square.name = squareIndex local function onSquareTap(self, event) print("Tap event on: " .. self.name) square = display.newImageRect("../images/1024x1024-yellow-square.png", oneSquareWidth, oneSquareWidth ) --[[\>\>\>\>\>\>\> To Do: Get x, y of blue square that was tapped, and update x, y of yellow square \<\<\<\<\<\<\< --]] square.x = event.x -- Need to set x,based on blue square, not Tap event square.y = event.y -- Need to set y based on blue square, not Tap event return true end square.tap = onSquareTap square:addEventListener("tap", square) if(horzCount % 10 == 1) -- 1st object in the row, requires a preceeding spacer amount of space before first object then square.x = oneSpaceWidth + (oneSquareWidth) square.y = 100 myLocation = {myLocationIndex = squareIndex, myLocationX = square.x, myLocationY = square.y} print( myLocation.myLocationIndex, myLocation.myLocationX, myLocation.myLocationY) else square.x = (oneSpaceWidth + (oneSquareWidth)) + ((horzCount - 1) \* (oneSpaceWidth + oneSquareWidth)) square.y = 100 squares[squareIndex] = square squares[squareIndex].squareIndex = squareIndex -- This table keeps track of each individual square printed to the screen myLocation = {myLocationIndex = squareIndex, myLocationX = square.x, myLocationY = square.y} print( myLocation.myLocationIndex, myLocation.myLocationX, myLocation.myLocationY) end -- end if count is modulus 1 end -- end horzCount
Here’s a screenshot…
Here’s the Corona Simulator Output text.
18:02:36.700 18:02:36.700 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 18:02:36.700 Version: 3.0.0 18:02:36.700 Build: 2016.2830 18:02:36.700 Platform: iPhone / x64 / 6.1 / GeForce 9500 GT/PCIe/SSE2 / 3.3.0 / 2016.2830 / en\_US | US | en\_US | en 18:02:36.700 Loading project from: C:\Users\Tom\Desktop\Mobile Development\swapImageOnTap 18:02:36.700 Project sandbox folder: C:\Users\Tom\AppData\Local\Corona Labs\Corona Simulator\Sandbox\swapimageontap-4AE556F91ED37EC4C5AE76ACC0AC017E\Documents 18:02:36.700 1 100 100 -- (index, x coord, y coord of blue squares) 18:02:36.700 2 200 100 18:02:36.700 3 300 100 18:02:36.710 4 400 100 18:02:36.710 5 500 100 18:02:36.710 ---------------------------------- 18:02:40.755 Tap event on: 1 18:02:47.335 Tap event on: 3
