需要幫助。我在這裏有我的整個代碼。我試圖將觸摸的對象(文本)與局部變量答案的值進行比較,但它總是打印文本的零值。我怎樣才能比較這兩個?如何將觸摸的文本對象與某個對象的值進行比較?
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