LUA - 计时器结束后无法重启游戏

问题描述 投票:1回答:1

我必须为我的大学工作制作一个小游戏,并且在游戏结束时我会喜欢它,所以如果你单击游戏结束按钮它会重新启动它,但是每次我尝试这样做时它都没有用错误或只是游戏重新启动和游戏结束图像没有被删除。

我知道当由于closeDownApp函数点击gameover标志时,当前代码退出应用程序(这是我尝试添加重启能力之前的代码)

任何有关如何做的帮助将不胜感激

--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( )
lua corona
1个回答
0
投票

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

我相信你可以通过在那里寻求帮助来更快地解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.