2017-03-20 1 views
0

누군가가 여기에 무슨 문제가 있다고 설명해 주시겠습니까? 내가에서 addEventListener를 추가하는 즉시 잘못AddEventListener : 청취자가 무효가 될 수 없습니다.

newBalloon:addEventListener("tap", pushBalloon) 

전체 코드 :

local composer = require("composer") 

local scene = composer.newScene() 


local physics = require("physics") 
physics.start() 



-- Configure image sheet 
local positioninsheetOptions = 144.1 
local sheetOptions = 
{ 
    frames = 
    { 
     { 
      x = 0, 
      y = 0, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*2, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*3, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*4, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*5, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*6, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*7, 
      width = 112, 
      height = 142 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*8, 
      width = 112, 
      height = 142 
     }, 

     { 
      x = 0, 
      y = positioninsheetOptions*9, 
      width = 112, 
      height = 142 
     }, 

      { 
      x = 0, 
      y = positioninsheetOptions*10, 
      width = 112, 
      height = 142 
     }, 




    }, 
} 
local objectSheet = graphics.newImageSheet("gameObjects.png", sheetOptions) 






local tapCount = 0 
local platform 

local tapText 
local balloonsTable = {} 
local leftBorder 
local rightBorder 

local backGroup 
local mainGroup 
local uiGroup 

local platform 
local platform2 




local function createBalloon() 
local randomBalloon = math.random(10) 


    local newBalloon = display.newImageRect(objectSheet, randomBalloon, 112, 142) 

    if newBalloon then 


     table.insert(balloonsTable, newBalloon) 
    physics.addBody(newBalloon, "dynamic", { radius=70, bounce=0 }) 
    newBalloon.myName = "bigBalloon" 
newBalloon.alpha = 0.75 
newBalloon.gravityScale = randomBalloon/-150 
     newBalloon:addEventListener("tap", pushBalloon) 

    end 


local whereFrom = math.random(3) 

    if (whereFrom == 1) then 
     -- From the left 
     newBalloon.x = 100 
     newBalloon.y = display.contentHeight+300 

    elseif (whereFrom == 2) then 
     -- From the top 
     newBalloon.x = 160 
     newBalloon.y = display.contentHeight+300 
    elseif (whereFrom == 3) then 
     -- From the right 
     newBalloon.x = 220 
     newBalloon.y = display.contentHeight+300 

end 
end 

local function gameLoop() 
    -- Create new balloon 
    createBalloon() 
    -- Remove balloons which have drifted off screen 
    for i = #balloonsTable, 1, -1 do 
    local thisBalloon = balloonsTable[i] 

     if (thisBalloon.x < -100 or 
      thisBalloon.x > display.contentWidth + 100 or 
      thisBalloon.y < -100 ) 
     then 
      display.remove(thisBalloon) 
      table.remove(balloonsTable, i) 
     end 

    end 

end 



local function pushBalloon(event) 
local tappedBalloon = event.target 
    -- balloon:applyLinearImpulse(0.2, -2, balloon.x, balloon.y) 
    -- tapCount = tapCount + 1 
    -- tapText.text = tapCount 
    if event.phase == "began" then 
    tappedBalloon.gravityScale = 10 
    end 

end 


local function pushBalloon2() 
    -- balloon:applyLinearImpulse(0.2, -2, balloon.x, balloon.y) 
    -- tapCount = tapCount + 1 
    -- tapText.text = tapCount 
    balloon2.gravityScale = 10 
    balloon2:applyLinearImpulse(0.1, 0, balloon2.x, balloon2.y) 
end 


local function pushBalloon3() 
    -- balloon:applyLinearImpulse(0.2, -2, balloon.x, balloon.y) 
    -- tapCount = tapCount + 1 
    -- tapText.text = tapCount 
    balloon3.gravityScale = 10 
    balloon3:applyLinearImpulse(-0.1, 0, balloon3.x, balloon3.y) 
end 



-- ----------------------------------------------------------------------------------- 
-- Scene event functions 
-- ----------------------------------------------------------------------------------- 

-- create() 
function scene:create(event) 

    local sceneGroup = self.view 
    -- Code here runs when the scene is first created but has not yet appeared on screen 
physics.pause() 
local background = display.newImageRect("background.png", 360, 570) 
background.x = display.contentCenterX 
background.y = display.contentCenterY 



platform = display.newImageRect("platform.png", 10, display.contentHeight*5) 
platform.x = -5 
platform.y = 0 
physics.addBody(platform, "static", { friction=0.5, bounce=0.3 }) 

platform2 = display.newImageRect("platform.png", 10, display.contentHeight*5) 
platform2.x = display.contentWidth+5 
platform2.y = 0 
physics.addBody(platform2, "static", { friction=0.5, bounce=0.3 }) 


createBalloon() 




end 


-- show() 
function scene:show(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 

     -- Code here runs when the scene is still off screen (but is about to come on screen) 

    elseif (phase == "did") then 
gameLoopTimer = timer.performWithDelay(1250, gameLoop, 0) 

     -- Code here runs when the scene is entirely on screen 
physics.start() 

    end 
end 


-- hide() 
function scene:hide(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 
     -- Code here runs when the scene is on screen (but is about to go off screen) 

    elseif (phase == "did") then 
     -- Code here runs immediately after the scene goes entirely off screen 
physics.pause() 
    end 
end 


-- destroy() 
function scene:destroy(event) 

    local sceneGroup = self.view 
    -- Code here runs prior to the removal of scene's view 

end 


-- ----------------------------------------------------------------------------------- 
-- Scene event function listeners 
-- ----------------------------------------------------------------------------------- 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 
-- ----------------------------------------------------------------------------------- 

return scene 

감사합니다.

답변

0

청취자를 추가하는 파일의 지점에서 pushBalloon()이 정의되지 않았습니다. 당신은 파일의 앞부분에서 앞으로 선언을 추가해야합니다 :

local scene = ... 
local pushBalloon 

... 
-- createBalloon() and other existing code 
... 

pushBalloon = function (event) 
-- your function code 

끝에 당신은 기능 (pushBalloon)를 참조

1

이벤트 리스너를 추가 할 때 아직 존재하지 않습니다. 그래서 위의 함수의 정의를 위와 같이 넣으면 그처럼 이벤트 리스너를 추가합니다.

local function pushBalloon(event) 
    local tappedBalloon = event.target 
     -- balloon:applyLinearImpulse(0.2, -2, balloon.x, balloon.y) 
     -- tapCount = tapCount + 1 
     -- tapText.text = tapCount 
     if event.phase == "began" then 
     tappedBalloon.gravityScale = 10 
     end 

end 

    ... 

newBalloon:addEventListener("tap", pushBalloon) 
관련 문제