需要帮助。我这里有完整的代码。我试图将触摸的对象(文本)与局部变量答案的值进行比较,但它总是打印文本的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
答案 0 :(得分:1)
尝试(应该工作)
reference.addListenerForSingleValueEvent ( .. )
在local function ulit()
local mRandom = math.random
local imagesNames = { "blue.png", "green.png", "red.png", "orange.png", "pink.png", "violet.png", "yellow.png" }
local lobo = display.newImage( imagesNames[mRandom(7)], mRandom(35,260), 600 )
...
local n = mRandom(0, 9)
local text = display.newText(n,50,30,"Arial",40)
lobo.number = n
...
local function balloonTouched(event)
...
-- here event.target is equal lobo
if event.target.number == answer then
...
函数中,对表对象和diplay对象使用相同的名称(ulit
)。那是错的。