If statement 如何将触摸文本对象与特定对象的值进行比较?

If statement 如何将触摸文本对象与特定对象的值进行比较?,if-statement,lua,coronasdk,addeventlistener,If Statement,Lua,Coronasdk,Addeventlistener,我需要帮助。我这里有我的全部代码。我试图将触摸对象(文本)与局部变量答案的值进行比较,但它总是打印文本的零值。我如何比较这两者 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

我需要帮助。我这里有我的全部代码。我试图将触摸对象(文本)与局部变量答案的值进行比较,但它总是打印文本的零值。我如何比较这两者

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
尝试(应该有效)


ulit
函数中,表对象和diplay对象使用相同的名称(
lobo
)。这是错误的。

函数中比较
答案
变量时,您想要什么?这是一个显示对象。不是字符串或数字。嘿@Idurniat:)。。。非常感谢你!它起作用了,我一直在把它的值传递给另一个变量,我很紧张。谢谢你的帮助。现在我正在研究如何删除一个文本并在其中放置另一个文本。:)我很高兴能帮助你:)
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
    ...