Thread: lua language
View Single Post
12-04-14, 11:25 AM   #8
analytical
A Defias Bandit
Join Date: Nov 2014
Posts: 3
lua language

Thank you all guys...

This function (addShip) need to decide randomly whether it will be horizontal or vertical rectangles. I am trying to change some rectangles from (ship) attributes to true in a line in order to add a ship. For example, if I try to call a ship of size 2 it will be 2 rectangles in a row or column with a ship attribute value of true.
This is where my last function is not doing anything...



local gridWidth = 9
local gridHeight = 14
local grid = {}


for i = 1, gridWidth do
grid[i] = {}
end

Touch function
local 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



local squareWidth = 25
local squareHeight = 25
local gap = 5


for i = 1, gridWidth do
for j = 1, gridHeight do


local xPos = (gap*i) + (i*squareWidth)
local yPos = (gap*j) + (j*squareHeight)
grid[i][j] = display.newRect(xPos, yPos, squareWidth, squareHeight)
grid[i][j]:addEventListener("tap", tappedRect)

end
end

--Add a ship
local function addShip( shipSize )

-- Set a random square location
local xPos = math.random(1, gridWidth)
local yPos = math.random(1, gridHeight)

for i = 1, shipSize-1, 1 do
if grid[x-i][y].ship then
grid[x-i][y]:setFillColor(255,0,0)


end
end
end



addShip(2)

If anyone has any idea please let me know

Thank you all...