I have a table of "board" in my game.I can display a square when i have an touch event called but how can I display an image instead?Coz doing so image does appear at the required location .Can anybody suggest me with a solution?I have pointed out the problem area with the comment line..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | module(..., package.seeall) function new() local localGroup = display.newGroup() display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") board = {} counter = 1 local img3 = display.newImage("home.jpg") img3.x = 512 img3.y = 384 localGroup:insert(img3) local img1 = display.newImage("water01.png") img1.x = 500 img1.y = 550 local img2 = display.newImage("water02.png") img2.x = 500 img2.y = 30 setup = function() counter = 1 board = {} for i = 1, 10 do board[i] = {} for j = 1, 10 do board[i][j] = {} board[i][j].square = display.newRect((i-1) * 102, (j-1) * 102, 105, 105) board[i][j].square:setFillColor(0, 0, 0, 0) end end end addObstacle = function(event) if event.phase == "ended" and event.y < 820 then x = math.ceil(event.x/102) print(x) y = math.ceil(event.y/102) print(y) board[x][y].isObstacle = 1 if CalcMoves(board, 1, 1, 10, 10) then --board[x][y].square:setFillColor(100, 0, 0)--This is woking fine --board[x][y]=display.newImage("path.png")--this is not displaying at the specified location.Any alternate solution for doing it??please Help --board[x][y].x=x --board[x][y].y=y end end return true end Runtime:addEventListener("touch", addObstacle) function CalcMoves(board, startX, startY, targetX, targetY) local openlist={} local closedlist={} local listk=1 local closedk=0 local tempH = math.abs(startX-targetX) + math.abs(startY-targetY) local tempG = 0 openlist[1] = {x = startX, y = startY, g = 0, h = tempH, f = 0 + tempH ,par = 1} local xsize = table.getn(board[1]) local ysize = table.getn(board) local curSquare = {} local curSquareIndex = 1 while listk > 0 do local lowestF = openlist[listk].f curSquareIndex = listk for k = listk, 1, -1 do if openlist[k].f < lowestF then lowestF = openlist[k].f curSquareIndex = k end end closedk = closedk + 1 table.insert(closedlist,closedk,openlist[curSquareIndex]) curSquare = closedlist[closedk] local rightOK = true local leftOK = true local downOK = true local upOK = true if rightOK then listk=listk+1 tempH=math.abs((curSquare.x+1)-targetX)+math.abs(curSquare.y-targetY) table.insert(openlist,listk,{x=curSquare.x+1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk}) end if leftOK then listk=listk+1 tempH=math.abs((curSquare.x-1)-targetX)+math.abs(curSquare.y-targetY) table.insert(openlist,listk,{x=curSquare.x-1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk}) end if downOK then listk=listk+1 tempH=math.abs(curSquare.x-targetX)+math.abs((curSquare.y+1)-targetY) table.insert(openlist,listk,{x=curSquare.x, y=curSquare.y+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk}) end if upOK then listk=listk+1 tempH=math.abs(curSquare.x-targetX)+math.abs((curSquare.y-1)-targetY) table.insert(openlist,listk,{x=curSquare.x, y=curSquare.y-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk}) end table.remove(openlist,curSquareIndex) listk=listk-1 if closedlist[closedk].x==targetX and closedlist[closedk].y==targetY then return closedlist end end return nil end setup(); return localGroup end |
Look into setReferencePoint() for your image, see if that helps.
Disclaimer, i haven't run your sample code. That said, Tony's suggestion to use setReferencePoint() sounds like a good one to me. The reason for this is that Corona image display objects draw from their center by default and if you're anything like me you're expecting it to be from top left corner.