Lua 电晕SDK游戏重启

Lua 电晕SDK游戏重启,lua,composer-php,coronasdk,restart,Lua,Composer Php,Coronasdk,Restart,我的game.lua文件的更新代码 local composer=require("composer") local physics=require("physics") local widget=require("widget") local scene=composer.newScene() physics.setDrawMode("normal") --erased system activate check later system.activate("multitouch")

我的game.lua文件的更新代码

local composer=require("composer")

local physics=require("physics")
local widget=require("widget")

local scene=composer.newScene()

physics.setDrawMode("normal")
--erased system activate check later
system.activate("multitouch")

--CHECK THIS _H AND _W
_H=display.contentHeight
_W=display.contentWidth

physics.start()
 physics.setGravity(0,-5)

 trumps=0

 numTrumps=100

 startTime=20

 totalTime=20

 timeLeft=true

 playerReady=false

Random=math.random



local function Restart(event)
    if "began"==event.phase then 

if(timeLeft~=false) then
    timer.cancel(gameTimer)

    end

end
        if "moved"==event.phase then

end
            if "ended"==event.phase then
            composer.gotoScene("restarttest")
end
        end





local function gameOver(condition)
    if (condition=="winner") then
        screenText.text="Adios Trump!"

        elseif (condition=="notbad") then
            screenText.text="Barely stopped Trump"

            elseif (condition=="loser") then
                screenText.text="Ready for That Wall!"
            end
end




local function removeTrumps(obj)
    obj:removeSelf()

    trumps=trumps-1

    if (timeLeft~=false) then 
        if(trumps==0) then
            timer.cancel(gameTimer)
            gameOver("winner")

        elseif(trumps<=40) then

            gameOver("notbad")
        elseif(trumps>=31) then

            gameOver("loser")
        end

    end
end


local function countDown(event)
    if (startTime==totalTime) then
        playerReady=true
        screenText.text="Hurry!"

    end
    startTime=startTime-1
    timeText.text="Time: "..startTime

    if (startTime==0) then
        timeLeft=false
    end

end
--[[local function spawn(params)
    local myTrump=display.newImage(params.image, 25, 25)
    myTrump.myTrumpTable=params.myTrumpTable
    myTrump.index=#myTrump.myTrumpTable+1
    myTrump.myName="Donald: ".. myTrump.index

    if params.hasBody then
      myTrump.density=.1
      myTrump.friction=0
      myTrump.bounce=.9
      myTrump.radius=9
      myTrump.bodyType="dynamic"

      physics.addBody(myTrump,"dynamic",{density=myTrump.density, friction=myTrump.friction, bounce=myTrump.bounce,radius=myTrump.radius})
    end
    myTrump.myTrumpTable[myTrump.index]=myTrump
    return myTrump
end

local spawnTable={}

for i=1, 100 do
    local spawns=spawn(
    {
image="tp.png",
myTrumpTable=spawnTable,
hasBody=true,
bounce=.9,
radius=9,
density=.1,
bodyType="dynamic",
}
)
end
--]]
local function startGame()
    myTrump=display.newImageRect("tp.png",25,25)
     myTrump.x=Random(50,_W-50)
     myTrump.y=(_H+10)
     physics.addBody(myTrump,"dynamic",{density=.1,friction=0,bounce=.9,radius=9})


   function myTrump:touch(event)

    if(timeLeft~=false) then

        if (playerReady==true) then
            if(event.phase=="ended") then
            removeTrumps(self)
        end

    end
end
end
        myTrump:addEventListener("touch",myTrump)

trumps=trumps+1

if(trumps==numTrumps) then
    gameTimer=timer.performWithDelay(1000,countDown,totalTime)
else

    playerReady=false
end

end


  local gameTimer=timer.performWithDelay(20,startGame,numTrumps)


function scene:create(event)

    local screenGroup=display.newGroup()


  background=display.newImage(screenGroup,"tflag.png")

  but1=widget.newButton{screenGroup,label="Restart",onEvent=Restart}
but1.x=_W/2+150
but1.y=_H-10

 screenText=display.newText(screenGroup,"...Waiting for Trump...",0,0,native.systemFont,16*2)
screenText.xScale=.5
screenText.yScale=.5
screenText.anchorX=(0)
screenText.anchorY=(0)
screenText.x=_W/2-210
screenText.y=_H-20


  timeText=display.newText(screenGroup,"Time:"..startTime,0,0,native.systemFont,16*2)
timeText.xScale=.5
timeText.yScale=.5
timeText.anchorY=0
timeText.anchorX=0
timeText.x=_W/2
timeText.y=_H-20

 leftWall=display.newRect(screenGroup,483,50,1,1000)
   leftWall:setFillColor(0,0,200)

  rightWall=display.newRect(screenGroup,0,0,1,1000)
   rightWall:setFillColor(0,0,200)

  ceiling=display.newRect(screenGroup,150,0,1000,1)
  ceiling:setFillColor(0,0,200)

physics.addBody(leftWall,"static",{bounce=.1})
physics.addBody(rightWall,"static",{bounce=.1})
physics.addBody(ceiling,"static",{bounce=.1})

end

function scene:show(event)
    local screenGroup=display.newGroup()
if event.phase=="will" then


  end
 if event.phase=="did" then 

 end 

end


function scene:hide(event)
    local screenGroup=display.newGroup()
    if event.phase=="will" then
    end
    if event.phase=="did" then
Runtime:removeEventListener("touch",myTrump)

end
end

function scene:destroy(event)
    if event.phase=="will" then
physics.removeBody(leftWall)
physics.removeBody(rightWall)
physics.removeBody(ceiling)
physics.removeBody(myTrump)
display.remove(myTrump)
display.remove(timeText)
display.remove(screenText)
display.remove(but1)
display.remove(background)
display.remove(screenGroup)


end
if event.phase=="did" then
end

    end


scene:addEventListener("create",scene)
scene:addEventListener("show",scene)
scene:addEventListener("hide",scene)
scene:addEventListener("destroy",scene)

return scene
我唯一的问题是,当我重新启动时,特朗普的图像不再出现在屏幕上,但仍然存在,但你可以说是看不见的。因此,当重新启动游戏后出现新图像时,它们不能像预期的那样进入屏幕顶部,因为它们被前一个游戏中看不见的“王牌”阻止

我的游戏基于这个游戏,但是没有气球,而是唐纳德·特朗普的照片。

我对使用composer也很陌生,但当您调用removeScene(“game1”)时,这将调用场景:销毁函数。在此函数中,需要删除已创建的显示对象。因为您没有这样做,所以对象很可能仍然存在于新创建的对象后面,这会导致奇怪的物理问题。让我知道这是否有帮助。

我对使用composer也很陌生,但当您调用removeScene(“game1”)时,这将调用场景:销毁功能。在此函数中,需要删除已创建的显示对象。因为您没有这样做,所以对象很可能仍然存在于新创建的对象后面,这会导致奇怪的物理问题。如果有任何帮助,请告诉我。

谢谢你,布雷特,但我还有一个问题。特朗普队现在没有登上巅峰,但计时员按计划工作。谢谢你!我的第二个问题是重启按钮只工作一次。我将physics.start()行改为开头,并在代码开头设置了重力。王牌是向上移动还是在某个点停止?此外,如果没有显示任何新内容,为什么要使用完全不同的场景重新启动?你不能让你的重启按钮直接重置王牌和定时器吗?重置问题可能是因为你没有破坏那个场景。您将再次创建它,但不会删除当前的。composer:gotoScene()不会调用scene:create()方法,除非它被销毁。它们在某个点停止,我重新启动了另一个场景,因为我稍后将向该场景添加对象。一旦游戏重新开始,王牌就会上升,直到稍微低于顶部,而不再上升。还有,我应该如何销毁游戏场景,因为我销毁了我非常确定的所有对象。你能告诉我如何编辑我的代码吗?我想我确实删除了破坏场景中的当前场景。我的东西现在几乎可以工作了。我唯一的问题是,当我重新启动场景时,我的特朗普没有出现在屏幕的顶部,就像天花板掉下来了一样。我想是因为_H和_W,你能检查一下吗?谢谢你,布雷特,但我还有一个问题。特朗普队现在没有登上巅峰,但计时员按计划工作。谢谢你!我的第二个问题是重启按钮只工作一次。我将physics.start()行改为开头,并在代码开头设置了重力。王牌是向上移动还是在某个点停止?此外,如果没有显示任何新内容,为什么要使用完全不同的场景重新启动?你不能让你的重启按钮直接重置王牌和定时器吗?重置问题可能是因为你没有破坏那个场景。您将再次创建它,但不会删除当前的。composer:gotoScene()不会调用scene:create()方法,除非它被销毁。它们在某个点停止,我重新启动了另一个场景,因为我稍后将向该场景添加对象。一旦游戏重新开始,王牌就会上升,直到稍微低于顶部,而不再上升。还有,我应该如何销毁游戏场景,因为我销毁了我非常确定的所有对象。你能告诉我如何编辑我的代码吗?我想我确实删除了破坏场景中的当前场景。我的东西现在几乎可以工作了。我唯一的问题是,当我重新启动场景时,我的特朗普没有出现在屏幕的顶部,就像天花板掉下来了一样。我想是因为_H和_W的关系。你能查一下吗?
  local composer=require("composer")
local scene=composer.newScene()


--background
function scene:create(event)


--background=display.newImage(screenGroup,"tflag.png")


end

function start(event)
    if event.phase=="began" then
        --composer.removeScene("game1")

end
  if event.phase=="ended" then
  --composer.gotoScene("game1")

end
end

function scene:show(event)

--background:addEventListener("touch",start)
if event.phase=="will" then
composer.removeScene("game1")


 else if event.phase=="did" then

composer.gotoScene("game1")
end
end
end

function scene:hide(event)
    if event.phase=="will" then 
end
if event.phase=="did" then 
    --background:removeEventListener("touch",start)

end
    end

function scene:destroy(event)


end


scene:addEventListener("create",scene)
scene:addEventListener("show",scene)
scene:addEventListener("hide",scene)
scene:addEventListener("destroy",scene)



return scene