Which component need for such task

Hi! I’am very new in Lua and Corona and i start to learn it for making games :slight_smile:
My first game would be the “Sea Battle” game. And i have a question for guru.
I’ve done with main menu already.
Now i need some “playing field” (2 actually, for 2 players), it should consist of 100 cells (tiles?) (10x10).
The requirement for object “playing field”:

  • each cell should be touchable
  • each cell must have x,y coordinates
  • each cell should have ability to change self background color
  • each cell should have ability to change self background to transparent
  • each cell should have ability to load self background image
  • “playing field” entirely should have ability to change self state - enabled\disabled (touchable\untouchable)

Please advice what component or object(s) i should choose to create such “playing field” in rigth way.

Thanks! [import]uid: 128952 topic_id: 22434 reply_id: 322434[/import]

You’re going to need to know about tables, since each cell is likely going to be a table entry.

You’re going to need to know about display.newRect() to create a colored background. It has the properties to change to transparent and set the x, y position.

You’re going to need to know about display.newImageRect() if you’re going to load in image files.

Once you have a display object from either newRect() or newImageRect() then you will use method :setFillColor() to change the color, the properties .isVisible, .isHitTestable (maybe)

Then you will need to know how to use addEventListener() and removeEventListener() for the touchable.

[import]uid: 19626 topic_id: 22434 reply_id: 89465[/import]

Thank you very much. So i need 10x10 table object, filled with the display objects from display.newRect() and display.newImageRect() right? [import]uid: 128952 topic_id: 22434 reply_id: 89467[/import]

Just a note… Your going to find cells of 10x10 really really hard to touch on a device.

Usually the minimum recommended size for a touch is 50x50 [import]uid: 84637 topic_id: 22434 reply_id: 89480[/import]

Thank you Danny for response. Cells size not limited by 10x10 pixels, it is table dimension i need, table should consist of 100 cells, 10 rows and 10 columns. [import]uid: 128952 topic_id: 22434 reply_id: 89496[/import]

Ah my bad :slight_smile:

In that case :

  
local myGrid = {}  
  
--Populate a table with 10 rows and 10 columns  
for i = 1, 10 do  
 myGrid[i] = {}  
 for j = 1, 10 do  
 myGrid[j] = 0  
 end  
end  
  

[import]uid: 84637 topic_id: 22434 reply_id: 89497[/import]

Thanks, it’s clear.
And another question, is there some display object that will serve as container for cells(newRect), so i can set container x,y and move entire field? [import]uid: 128952 topic_id: 22434 reply_id: 89509[/import]

Also if you did not want to do a 2D array like Danny Suggested, you could do it as a one dimensional array.

[code]X = 0
Y = 0
cells = {}

local function changeColor(event)
local target = event.target
target.color = target.color + 1
if target.color > 3 then target.color = 1 end
if target.color == 1 then
target:setFillColor(255,0,0)
elseif target.color == 2 then
target:setFillColor(0,255,0)
else
target:setFillColor(0,0,255)
end
return true
end

for i=1,100 do
cells[i] = display.newRect(0,0,60,60)
cells[i].color = math.random(3)
cells[i].strokeWidth = 2
cells[i]:setStrokeColor(255,255,255)
if cells[i].color == 1 then
cells[i]:setFillColor(255,0,0)
elseif cells[i].color == 2 then
cells[i]:setFillColor(0,255,0)
else
cells[i]:setFillColor(0,0,255)
end
cells[i]:addEventListener(“tap”,changeColor)
cells[i].x = X * 64 + 32 – center reference point
cells[i].y = Y * 64 + 32

if i % 10 == 0 then
X = 0
Y = Y + 1
else
X = X + 1
end
end
[/code] [import]uid: 19626 topic_id: 22434 reply_id: 89515[/import]

Lime by Graham should be great for such task. [import]uid: 12704 topic_id: 22434 reply_id: 89521[/import]

Thanks guys. I will go with robmiracle or danny solution.
And another quest. It is possible to determine what exactly cell(newRect) object was touched, in event listener function i can access event.target object, but how i can associate each newRect() ojb with it’s position in grid, f.e. 2,4(second row, fourth column)? Is there some property in newRect() obj that i can set with it’s position in grid? [import]uid: 128952 topic_id: 22434 reply_id: 89850[/import]

Ah, it seem i found a solution in Corona Docs - All instances of DisplayObject can be treated like normal Lua tables. This means you can add your own properties to the object as long as they don’t conflict with the names of DisplayObject’s predefined properties and method. The one exception is that you cannot index into a DisplayObject as an array using numerical indices.
So i can add my onw property to each instance of newRect() obj and then access them through event.target obj right? [import]uid: 128952 topic_id: 22434 reply_id: 89856[/import]