lua language
Hi!
I am trying to create a function with a single argument. Then this function has to pick random square from the grid to start building the ship. There is no error, I don't know what's wrong. function addship can't build any ship...
gridWidth = 9
gridHeight = 14
grid = {}
for i=1, gridWidth, 1 do
grid[i] = {}
end
function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(255, 0, 0)
else
event.target.ship = false
event.target:setFillColor(0,255, 0)
end
end
squareWidth = 25
squareHeight = 25
gap = 5
for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do
grid[i][j] = display.newRect((gap* i) + (i*squareWidth) , (gap* j) + (j* squareHeight), squareWidth, squareHeight)
end
end
for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do
grid[i][j].ship = false
grid[i][j]:addEventListener("tap", tappedRect)
end
end
function addShip(shipSize)
x = math.random(1, gridWidth)
y = math.random(1, gridHeight)
for i = 1, shipSize-1, 1 do
if grid[x-1][y].ship then
grid[x-i][y]:setFillColor(255,0,0)
end
end
end
addShip(2)
Please Help!
|