View Poll Results: Is anyone is really good in lua language?
Need help in function addShip 0 0%
place ship randomly to the screen 0 0%
Multiple Choice Poll. Voters: 0. You may not vote on this poll

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
11-30-14, 05:57 PM   #1
analytical
A Defias Bandit
Join Date: Nov 2014
Posts: 3
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!
Attached Files
File Type: lua main.lua (1.5 KB, 175 views)
 
 

WoWInterface » Developer Discussions » Lua/XML Help » lua language


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off