簡體   English   中英

Corona SDK嘗試調用方法“ removeSelf”(nil值)

[英]Corona sdk attempt to call method 'removeSelf' (a nil value)

我正在嘗試在曲棍球應用中為用戶提供一個重置按鈕,但我一直收到此錯誤,而且我不知道還有什么要解決的attempt to call method 'removeSelf' (a nil value)attempt to call method 'removeSelf' (a nil value) 我試圖搬動東西,但是還是沒有幫助。 如何解決此錯誤? 請幫幫我。

我的代碼:

function startGame(event)
titleScreenGroup:removeSelf()

resetScore() -- in the case that this is a rematch
placePlayerOnePaddle()
placePlayerTwoPaddle()
placePuck(puckAvalLocation.center)  
Runtime:addEventListener( "postCollision", onPostCollision )
Runtime:addEventListener( "collision", onCollision )

appodeal.show("interstitial")    --<-------- move this to scene:show()'s "did" phase

end

local function gameRestart(event)
if ("ended" == event.phase) then
    --code here when touch begin
    startGame()
end
end

function setUpGroundGraphics()

local graphic = display.newImageRect( "bg.png", 768, 1024 )
graphic.x = display.contentWidth / 2
graphic.y = display.contentHeight / 2

local graphic = display.newImageRect( "score.png", 122, 144 )
graphic.x = topLeft.x + 90
graphic.y = display.contentHeight / 2

graphic = display.newImageRect( "centerLine.png", 768, 9 )
graphic.x = display.contentWidth / 2
graphic.y = display.contentHeight / 2

graphic = display.newImageRect( "centerCircle.png", 198, 198 )
graphic.x = display.contentWidth / 2
graphic.y = display.contentHeight / 2

-- top goal line
graphic = display.newImageRect( "goalLine.png", 497, 203 )
graphic.x = display.contentWidth / 2
graphic.y = topLeft.y + wallThickness * 2 + 70

-- bottom goal line
graphic = display.newImageRect( "goalLine.png", 497, 203 )
graphic.x = display.contentWidth / 5
graphic.y = bottomRight.y - wallThickness * 2 -70
graphic.rotation = 180

resetButton = display.newImageRect( "reset.png", 508, 224 )
resetButton.x = display.contentWidth / 4.1
resetButton.y = display.contentHeight - display.contentHeight / 2.5
resetButton:scale( 0.4, 0.4 )
resetButton:addEventListener("touch", gameRestart)

end

編輯!! 這是我定義titlescreengroup的地方

local lastForce -- Used to calculate the volume of a the collision sound effects
local titleScreenGroup

function main()
display.setStatusBar( display.HiddenStatusBar )

setUpTable()

setUpTitleScreen()  
end

function setUpTable()
setUpGroundGraphics()
setUpPaddleWalls()
setUpPuckWalls()
setUpScoreText();

end

function setUpTitleScreen()

titleScreenGroup = display.newGroup()

local background = 
display.newImageRect("rink.jpg",display.contentWidth,display.contentHeight)
background.x = display.contentCenterX
background.y = display.contentCenterY
titleScreenGroup:insert(background)

title = display.newImageRect( "title.png", 530, 196 )
title.x = display.contentWidth / 2
title.y = display.contentHeight / 4
titleScreenGroup:insert(title)

playButton = display.newImageRect( "play.png", 508, 224 )
playButton.x = display.contentWidth / 2
playButton.y = display.contentHeight - display.contentHeight / 4
titleScreenGroup:insert(playButton)

display.getCurrentStage():insert(titleScreenGroup)

playButton:addEventListener("touch", startGame)

end

我認為您嘗試titleScreenGroup刪除titleScreenGroup組。 這應該有助於:

function startGame(event)
    if titleScreenGroup then
        titleScreenGroup:removeSelf()
        titleScreenGroup = nil
    end
    resetScore() -- in the case that this is a rematch
    placePlayerOnePaddle() 
    placePlayerTwoPaddle()
    placePuck(puckAvalLocation.center)  
    Runtime:addEventListener( "postCollision", onPostCollision )
    Runtime:addEventListener( "collision", onCollision )

    appodeal.show("interstitial")    --<-------- move this to scene:show()'s "did" phase
end

我還注意到,當用戶觸摸播放按鈕時, startGame函數會被調用兩次。 快速解決:

function setUpTitleScreen()
    ...
    playButton:addEventListener("touch", gameRestart)
end

最好使用display.remove(object) ,然后使用titleScreenGroup = nil https://docs.coronalabs.com/api/library/display/remove.html

function startGame(event)
    --titleScreenGroup:removeSelf()
    display.remove( titleScreenGroup )
    titleScreenGroup = nil

    resetScore() -- in the case that this is a rematch
    placePlayerOnePaddle()
    placePlayerTwoPaddle()
    placePuck(puckAvalLocation.center)  
    Runtime:addEventListener( "postCollision", onPostCollision )
    Runtime:addEventListener( "collision", onCollision )

    appodeal.show("interstitial")    --<-------- move this to scene:show()'s "did" 
    phase

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM