lua language
1 Attachment(s)
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! |
This is a forum for World of Warcraft AddOns. I don't even know what this is that you just posted, but it isn't that.
|
As you initialize your grid with
Lua Code:
Lua Code:
Try Lua Code:
|
First, to build a ship, you need some timber
|
First I had to clean up your code lol:
Lua Code:
Then I saw Lua Code:
Lua Code:
Then you try to perform an if check that will always be false as you never declared the event table: Lua Code:
More redundancy of for loops over writing table values Lua Code:
So, quite a bit of work (more than I posted) needs to be done to make this functional! |
Quote:
Without any clue as to the API being used, there could be many more errors than we can see because none of it has anything to do with the WoW API. |
Quote:
Code:
for i = 4, 9000, 2 do ... end |
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... |
1. Please use [code] tags around your code. Otherwise it becomes an unformatted mess and is difficult to read.
2. This site is for World of Warcraft UI addons. There is no "addShip" function or "display.newRect" function in the WoW API, nor in Lua itself, so any help you get here will be general Lua syntax help only, especially when you have not even specified what API you're working with. What program will your script be running in? Is there not a forum specific for that API you can post on where people might actually know what you are talking about? |
I bet a grand he is a freshman in some college and this is his homework.
|
Could be the code academy tutorial for Lua which does walk you throgh making a battleship game in python with a lua config.
|
Well, in any case, (s)he is still posting on the wrong site, and is still not explaining clearly what they are actually doing and why they're asking for help with it on a site for WoW addons.
|
It looks like it's for Love. If that's the case, you'd be better off going there.
|
I bet: a Corona mobile app. :)
|
Yeah, no, none of that is at all related to WoW AddOns, as others have already said. You might want to look into sites that are directly related to whatever it is you are working on. Good luck!
|
All times are GMT -6. The time now is 09:32 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI