2017-02-05 2 views
1

도움이 필요하십니까? 나는 여기 전체 코드를 가지고있다. 나는 터치 된 객체 (텍스트)를 로컬 변수 응답의 값과 비교하려고 시도하지만 항상 텍스트의 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

'balloonTouched' 함수에서'answer' 변수를 비교하고 싶습니까? ['event.target'] (https://docs.coronalabs.com/api/event/touch/target.html)은 표시 객체입니다. 문자열이나 숫자가 아닙니다. – ldurniat

답변

1

시도하면 테이블 개체 및으로 모니터 객체의 같은 이름 (lobo)를 사용 ulit 함수에서

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 
    ... 

을 (작동합니다). 그건 잘못된 것입니다.

+0

안녕하세요 @ 아이디 ... :) 고마워요! 그것은 효과적이었습니다. 저는이 모든 시간을 다른 변수로 전달하면서 노력해 왔습니다. 그래서 저는 너무 압박감을 느낍니다. 당신의 도움을 주셔서 감사합니다. 이제는 텍스트를 제거하고 다른 텍스트를 배치하는 방법을 모색 중입니다. :) – int21h

+0

내가 당신을 도울 수있어서 기쁩니다 :) – ldurniat

관련 문제