Need to Reset Game

I’m trying to build a matching word game that when the wrong word is pressed the 3 button objects are randomized and reset to different positions. This is what I have currently. I am a newbee and not sure if this code is correct or not. I can’t seem to removeSelf for all 3 buttons (it return a nil value) and reset the buttonFlip function to run again. I would like it to run like that until the right matching button is chosen. Not sure what the best approach would be. Any advice would help
My Code:

–Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;

–Hide status bar
display.setStatusBar(display.HiddenStatusBar);

–Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0

–Declare variable to track button select
local secondSelect = 0
local checkForMatch = false

–Declare button, and buttonImages table
local button = {}
local buttonImages = {1, 2, 3}
–Declare and prime a last button selected variable
local lastButton = display.newImage(“1.png”);
lastButton.myName = 1;

–Set up simple off-white background
local myRectangle = display.newRect(0, 0, _W, _H)
myRectangle:setFillColor(235, 235, 235)

–Set starting point for button grid
x = -70

function main ()
appleButton()
applePix()
buttonFlip()
end

–Set up game function
function game(object, event)

if(event.phase == “began”) then
if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
object:scale(1.25,1.25);
lastButton = object
–if buttons doesn’t match then reset game
if(lastButton.myName ~= 1) then
print"Match Not Found!";
timer.performWithDelay(1250, function()
checkForMatch = false;
secondSelect = 0;
end, 1)
–If button does match, end game
elseif(lastButton.myName == 1) then
print “Match Found!”;
timer.performWithDelay(1250, function()
checkForMatch = false;
secondSelect = 0;
end, 1)
end
end
end
end

–Place buttons on screen
function appleButton()
–apple button placement and object details
appleButton = display.newImageRect(“1.png”, 200,150);
appleButton:setReferencePoint(display.CenterReferencePoint)
appleButton.x = _W - 386; appleButton.y = _H -860

end
function applePix()
–apple image placement
applePix = display.newImageRect(“apple.png”, 200,400);
applePix:setReferencePoint(display.CenterReferencePoint)
applePix.x = _W - 386; applePix.y = _H -500
end
function buttonFlip()

–location of click match button
for count = 1,3 do
x = x + 225
y = 900

–Assign each image a random location on grid
temp = math.random(1,#buttonImages)-- random 1 through #buttonImages(table number of button images)
button[count] = display.newImageRect(buttonImages[temp] … “.png”, 200,150);
–Position the button
button[count].x = x;
button[count].y = y;

–Give each a button a name
button[count].myName = buttonImages[temp]
button[count].number = totalButtons

–Remove button from buttonImages table so that it won’t repeat button
table.remove(buttonImages, temp)

–Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( “touch”, button[count] )
end
end

main() [import]uid: 190188 topic_id: 33481 reply_id: 333481[/import]

I don’t see where you’re trying to remove the buttons, so it’s going to be hard to see why you’re getting that error. Also, it’s really helpful when posting code to put <code> and </code> tags around the code so it will preserve your indentions.

I would take a little bit of a different approach.

I would create an array that holds your three buttons outside of buttonFlip and not delete them. Instead I would have your buttonFlip function shuffle the array with the buttons so they appear in a random order. Then buttonFlip could position them after shuffling them.

local function shuffle(t)  
 local iterations = #t  
 local j  
  
 for i = iterations, 2, -1 do  
 j = random(i)  
 t[i], t[j] = t[j], t[i]  
 end  
end  

Will shuffle a table nicely for you.
[import]uid: 19626 topic_id: 33481 reply_id: 133074[/import]

I don’t see where you’re trying to remove the buttons, so it’s going to be hard to see why you’re getting that error. Also, it’s really helpful when posting code to put <code> and </code> tags around the code so it will preserve your indentions.

I would take a little bit of a different approach.

I would create an array that holds your three buttons outside of buttonFlip and not delete them. Instead I would have your buttonFlip function shuffle the array with the buttons so they appear in a random order. Then buttonFlip could position them after shuffling them.

local function shuffle(t)  
 local iterations = #t  
 local j  
  
 for i = iterations, 2, -1 do  
 j = random(i)  
 t[i], t[j] = t[j], t[i]  
 end  
end  

Will shuffle a table nicely for you.
[import]uid: 19626 topic_id: 33481 reply_id: 133074[/import]

Thanks I will try it and post it once I do. [import]uid: 190188 topic_id: 33481 reply_id: 133293[/import]

Thanks I will try it and post it once I do. [import]uid: 190188 topic_id: 33481 reply_id: 133293[/import]