[英]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)
. 我正在尝试在曲棍球应用中为用户提供一个重置按钮,但我一直收到此错误,而且我不知道还有什么要解决的attempt to call method 'removeSelf' (a nil value)
它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 这是我定义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. 我认为您尝试titleScreenGroup
删除titleScreenGroup
组。 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. 我还注意到,当用户触摸播放按钮时, startGame
函数会被调用两次。 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 最好使用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.