도움이 필요하십니까? 나는 여기 전체 코드를 가지고있다. 나는 터치 된 객체 (텍스트)를 로컬 변수 응답의 값과 비교하려고 시도하지만 항상 텍스트의 nil 값을 인쇄합니다. 이 두 가지를 어떻게 비교할 수 있습니까?터치 된 텍스트 개체를 특정 개체의 값과 비교하는 방법은 무엇입니까?
function scene:create(event)
local sceneGroup = self.view
local temp1 = math.random(0,10)
local temp2 = math.random(0,10)
local answer = temp1 + temp2
local background = display.newImage("bg0.png")
background.x = display.contentWidth*0.5
background.y = display.contentHeight*0.5
local text2 = display.newText(temp1,90,45,"Arial",55)
text2:setFillColor(0, 0, 0)
local plus = display.newText(" + ",140,45,"Arial",55)
plus:setFillColor(0, 0, 0)
local text3 = display.newText(temp2,180,45,"Arial",55)
text3:setFillColor(0, 0, 0)
local equals = display.newText(" = ",235,45,"Arial",55)
equals:setFillColor(0, 0, 0)
local secondsLeft = 02 * 60
local clockText = display.newText("02:00", 270, -7, "Arial", 35)
clockText:setFillColor(0,0,0)
local function updateTime()
secondsLeft = secondsLeft - 1
local minutes = math.floor(secondsLeft/60)
local seconds = secondsLeft % 60
local timeDisplay = string.format("%02d:%02d", minutes, seconds)
clockText.text = timeDisplay
if timeDisplay == "00:00" then
GameOver()
print "Game Over!!!"
end
end
local countDowntimer = timer.performWithDelay(1000,updateTime,secondsLeft)
local function offscreen(self, event)
if(self.y == nil) then
return
end
if(self.y > display.contentHeight - 100) then
Runtime:removeEventListener("enterFrame", self)
self:removeSelf()
end
end
local function balloonTouched(event)
if (event.phase == "began") then
print("object touched =", event.target)
print (answer)
if event.target == answer then
print "Good"
local temp1 = math.random(0,10)
local temp2 = math.random(0,10)
local text2 = display.newText(temp1,90,45,"Arial",55)
text2:setFillColor(0, 0, 0)
local text3 = display.newText(temp2,180,45,"Arial",55)
text3:setFillColor(0, 0, 0)
secondsLeft = secondsLeft + 5
else
print "Wrong Answer"
secondsLeft = secondsLeft - 3
end
Runtime:removeEventListener("enterFrame", event.self)
event.target:removeSelf()
end
return true
end
local function ulit()
lobo = { ("blue.png"), ("green.png"), ("red.png"), ("orange.png"), ("pink.png"), ("violet.png"), ("yellow.png") }
local lobo = display.newImage(lobo[math.random(7)], math.random(35,260), 600)
physics.addBody(lobo, { density=0.1, friction=2.0, bounce=0.0, velocity=-40, isSensor=true });
lobo.gravityScale = -0.1111111115
sceneGroup:insert(lobo)
lobo.enterFrame = offscreen
local text = display.newText(math.random(0,9),50,30,"Arial",40)
text.id = "sagot"
sceneGroup:insert(text)
text.enterFrame = offscreen
function lobo:enterFrame()
text.x, text.y = lobo.x, lobo.y;
end
Runtime:addEventListener("enterFrame", lobo)
lobo:addEventListener("touch", balloonTouched)
end
timer.performWithDelay(300,ulit,0)
local backBtn = widget.newButton
{
labelColor = { default={255}, over={128} },
defaultFile= "home.png",
overFile= "home.png",
width=50, height=50,
onRelease = onBackBtnRelease
}
backBtn.x = 20
backBtn.y = -7
sceneGroup:insert(background)
sceneGroup:insert(clockText)
sceneGroup:insert(backBtn)
sceneGroup:insert(text2)
sceneGroup:insert(plus)
sceneGroup:insert(text3)
sceneGroup:insert(equals)
end
'balloonTouched' 함수에서'answer' 변수를 비교하고 싶습니까? ['event.target'] (https://docs.coronalabs.com/api/event/touch/target.html)은 표시 객체입니다. 문자열이나 숫자가 아닙니다. – ldurniat