【问题标题】:how do I create a loop between two functions如何在两个函数之间创建循环
【发布时间】:2015-05-20 01:54:12
【问题描述】:

我想制作一个游戏,我必须根据显示的图像点击图像。如果顶部的图像是红色的,我希望玩家点击红色字符,如果图像是蓝色,我希望玩家点击蓝色字符。如果这个人点击了错误的角色,我希望游戏结束。我的代码的问题是,每当顶部的图像改变颜色时,我希望该人必须点击的字符也改变。万一这没有任何意义......说当我开始游戏时,红色图像出现在顶部。因为红色图像在顶部,所以玩家应该点击红色字符。当顶部的图像从红色变为蓝色时,我希望该人点击蓝色字符而不是红色字符。问题是如果我在这种情况下点击蓝色字符,玩家将输掉游戏。我该如何解决这个问题?

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)

【问题讨论】:

    标签: function if-statement lua coronasdk


    【解决方案1】:

    根据我的理解,您使用 imageFile var 来表示用户将点击的类型。使用button1作为红框,button2作为蓝框。每次点击后,如果点击右键,重新定位button1和button2,并将imageFile重置为新的随机类型?

    如果是这样,您只需在 endGameendGame2 函数中删除 local 关键字。 imageFile var ref 是外部的,但是当您声明一个本地 imageFile 时,它​​会覆盖外部 var,并且不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2018-09-05
      相关资源
      最近更新 更多