LUA-计时器结束后无法使游戏重新开始

时间:2019-02-12 13:08:54

标签: lua corona

我必须为上大学做一个小游戏,并且在游戏结束时我希望它能够运行,因此,如果单击游戏结束按钮,它将重新启动它,但是每次我尝试这样做时,它并没有出现错误或只是重新启动游戏而无法清除游戏结束图像。

我知道由于closeDownApp函数而导致单击了游戏结束符时,当前代码退出了应用程序(这是我尝试添加重新启动功能之前的代码)

任何帮助您的帮助

--Define the variables
    rotateAmt   = 5
    rotateMin   = -90
    rotateMax   = 90
    cannonRotation  = 0
    cannonForce = 1200
    playerPoints    = 0
    targetHit   = false
    timeLeft    = 600


--Define the functions
function closeDownApp(event)
    os.exit ( )
end

function update( )
    --decrease the time counter
    timeLeft = timeLeft - 1
    scoreDisplay.text = 'Kills: ' .. playerPoints .. '   Time: ' .. timeLeft
    --check if the time has run out
    if (timeLeft <= 0) then
        --display the final score
        scoreDisplay.text = 'Your score was  ' .. playerPoints
        --remove all of the screen objects
        display.remove(cannonBarrel)
        display.remove(target)
        display.remove(upButton)
        display.remove(downButton)
        display.remove(fireButton)
        --display the 'game over' sign
        gameOver = display.newImage('gameover.png', 150, 75)
        gameOver:addEventListener('tap', closeDownApp)
        -- make a group 
    end

    --check if the target has been hit
    if (targetHit == true) then
        targetHit = false
        playerPoints = playerPoints + 1
        Hit = display.newImage('Hit.png', target.x, target.y)
        --replace the target with the hit picture
        transition.dissolve(Hit, target, 1000, 0)
        display.remove(bullet)
        --put the target back to a random position
        target.x = math.random(280, 500 )
        target.y = math.random(100, 290 )
    end
end

function onCollide(event)
    targetHit=true
end

function fire(event)
    --only fire at the beginning of a touch event
    if (event.phase == 'began') then
        media.playSound('bang.wav')
        bullet = display.newImage('bullet.png')
        --move the image
        bullet.x, bullet.y = cannonBarrel:localToContent(70, 0)
        bullet.rotation = cannonRotation
        --apply physics to the cannonball
        physics.addBody( bullet, { density=2.0, friction=0.2, radius=15 } )
        --determine the appropriate ratio of horizontal to vertical force
        force_x = math.cos(math.rad(cannonRotation)) * cannonForce
        force_y = math.sin(math.rad(cannonRotation)) * cannonForce
        --fire the cannonball
        bullet:applyForce( force_x, force_y, bullet.x, bullet.y )
    end
end

function moveDown(event)
    --only move the barrel if the touch event started
    if (event.phase == 'began') then
        cannonRotation = cannonRotation + rotateAmt
        if (cannonRotation >= rotateMax) then
            cannonRotation = rotateMax
        end
        cannonBarrel.rotation = cannonRotation
        fireButton.rotation = cannonRotation
    end
end

function moveUp(event)
    --only move the barrel if the touch event started
    if (event.phase == 'began') then
        cannonRotation = cannonRotation - rotateAmt
        if (cannonRotation <= rotateMin) then
            cannonRotation = rotateMin
        end
        cannonBarrel.rotation = cannonRotation
        fireButton.rotation = cannonRotation
    end
end

function makeTarget( )
    target = display.newImage('target.png')
    target.x = math.random(280, 450)
    target.y = math.random(100, 290)
    physics.addBody(target,{density=1.0, friction=0.5, bounce=0.05, radius=15})
    target.bodyType = 'static'
    target:addEventListener('collision', onCollide)
end

function makeInterface( )
    --up button
    upButton = display.newImage('up_button.png')
    upButton:translate(3, 37)
    upButton:addEventListener('touch', moveUp) 
    --down button
    downButton = display.newImage('down_button.png')
    downButton:translate(3, 176)    
    downButton:addEventListener('touch', moveDown)  
    --fire button
    fireButton = display.newImage('fire_button.png')
    fireButton:translate(19, 124)
    fireButton:addEventListener('touch', fire)
    --display cannon parts
    cannonBarrel = display.newImage('cannon_barrel.png')
    cannonBarrel:translate(73, 109)     
    --display score
    scoreDisplay = display.newText( ('Points: ' .. playerPoints), 70, -18, native.systemFont, 20 )
    scoreDisplay:setTextColor( 255,255,255 )
    scoreDisplay:translate(display.contentHeight /2, 30)
end

--Define control structure
function init( )
    audio.stop (1)
    display.setStatusBar(display.HiddenStatusBar)
    background = display.newImage('bg.jpg')
    physics = require('physics')
    physics.setDrawMode('normal')
    physics.start( )
    makeInterface( )
    makeTarget( )
    Runtime:addEventListener('enterFrame', update)
    local backgroundMusic = audio.loadStream("theme.mp3")
    local backgroundMusicChannel = audio.play( backgroundMusic,{channel=2, loops=-1, fadein=10 })
    audio.setVolume( 0.5, { channel=backgroundMusicChannel } )
end

function introScreen( )
    local backgroundMusic1 = audio.loadStream("intro.mp3")
    local backgroundMusicChannel1 = audio.play( backgroundMusic1,{channel=1, loops=1, fadein=10 })
    -- Display the background
    background = display.newImage ("IntroScreen.png")
    -- Display the play button at the top right
    playBtn = display.newImage ("playBtn.png", 350, 10)
    playBtn:addEventListener('tap', init)
end

--Call the code
introScreen( )

1 个答案:

答案 0 :(得分:0)

这似乎是为Corona SDK编写的。他们的论坛(https://forums.coronalabs.com/)看起来非常活跃。

我敢肯定,您可以在那儿寻求帮助,从而更快地解决此问题。