Hi all,
Hope you’re well.
I’m a complete beginner that has been stuck on a piece of tutorial code for the past few days. Basically, the code and image below are a modified version of the code posted here (Corona Labs); it’s the setup for a basic checkers-type game.
I have a quick question - the blue, yellow, and pink cards sit in a table and they’re distributed at random each time the game loads. However, for some reason I can’t work out, the code will only add 3-4 of them to the grid (there’s some kind of +/-1 function in there too).
All I want to do is retain the random element but fill the entire board with cards - so 16 (4x4) cards placed randomly. Which part of the code is responsible for the number of cards added? The checkerboard image itself isn’t important.
Thanks! And apologies if my code is a mess! I haven’t quite got the hang of anything yet …
----------------------------------------------------------------------------------------- -- -- -- ----------------------------------------------------------------------------------------- display.setStatusBar(display.HiddenStatusBar) centerX = display.contentWidth \* .5 centerY = display.contentHeight \* .5 local card local gridWidth = 4 local gridHeight = 4 local cellWidth = 80 local cellHeight = 80 local img = { "card-blue.png", "card-lblue.png", "card-pink.png", "card-yellow.png" } local grid = {} for i = 1, gridHeight do grid[i] = {} end --PLACES BACKGROUND-- local bg = display.newImageRect("Sky.png", 480, 700) bg.x = centerX bg.y = centerY --LOAD THE CHECKERBOARD-- local checkerBoard = display.newImageRect("table2.png", 320, 320) checkerBoard.x = display.contentCenterX checkerBoard.y = (300) local gbOffsetX = checkerBoard.x - ( checkerBoard.width \* checkerBoard.anchorX ) local gbOffsetY = checkerBoard.y - ( checkerBoard.height \* checkerBoard.anchorY ) --HANDLES TOUCH ON CARDS-- local function cardTouched(event) card = event.target if event.phase == "ended" then print("Touched!") transition.to ( event.target, { rotation=360, delta=true,} ) --card.isVisible = false end end --PLACES CARDS FROM TABLE, ADDS LISTENER TO EACH-- local function placeCards (xPos, yPos) local card = {} for y = 1, 4 do for x = 1, 4 do local r = math.random(1, #img); card = display.newImageRect(img[r], cellWidth, cellHeight ) if card then card:addEventListener ( "touch", cardTouched ) card.xPos = xPos card.yPos = yPos card:translate( 140, -27 ) card.x = (xPos - 1) \* cellWidth + (cellWidth \* 0.5) + gbOffsetX card.y = (yPos - 1) \* cellHeight + (cellHeight \* 0.5) + gbOffsetY end end end end local lastObject for i = 1, 4 do local xPos = math.random( 1, gridWidth ) local yPos = math.random( 1, gridHeight ) grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard) lastObject = grid[yPos][xPos] end