简体   繁体   中英

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

I am trying to give the user a reset button in my hockey app but I keep getting this error and I don't know what else to try to fix it attempt to call method 'removeSelf' (a nil value) . I tried moving things around but that didn't help either . How can I fix this error ? Please help me out.

my code :

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

EDIT!! This is where I defined the 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

I think you try remove titleScreenGroup group more than one time. This should help:

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

I noticed also that startGame function is called twice when user touch play button. Quick fix:

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

Better use display.remove( object ) and then 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM