mardi 17 mars 2015

how do I create a loop between two functions

I want to make a game where I have to tap an image based on an image that shows up. If the image at the top is red, I want the player to tap the red character and if the image is blue, I want the player to tap the blue character. If the person taps the wrong character, I want the game to be over. The problem with my code is that Whenever the image at the top changes color, I would want the character that the person had to tap change as well. In case that didn't make any sense... say that when I started the game, the red image showed up at the top. Because the red image is at the top, the player should tap the red character. When the image at the top changes from red to blue, I would want the person to tap the blue character instead of the red character. The problem is that If I tap the blue character in this case, The player would lose the game. How do I fix this problem?



function scene:createScene ( event )
local group = self.view

local tap = display.newText("Tap:", 0, 0, "Helvetica", 36)
tap.x = 100
tap.y = screenTop + 20
group:insert(tap)

local imageFiles = {"redbox.png", "bluebox.png"}
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)

local button1 = display.newImage("redbox.png")
button1.x = centerX
button1.y = centerY
group:insert(button1)

local button2 = display.newImage("bluebox.png")
button2.x = centerX
button2.y = centerY - 100
group:insert(button2)

local function endGame(event)
if imageFile == "redbox.png" then
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
else
storyboard.gotoScene( "restartEasy" )

end
end

local function endGame2(event)
if imageFile == "bluebox.png" then
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
else
storyboard.gotoScene("restartEasy")

end
end

button1:addEventListener("tap", endGame)
button2:addEventListener("tap", endGame2)

Aucun commentaire:

Enregistrer un commentaire