在您赢得或失败游戏后,我无法让重新启动或主菜单按钮工作。谁能指出我正确的方向。我不想使用故事板,除非它是唯一的方法......(win fail的代码在最底部)
--[Colors Used]
--389bff (Blue)
--ff3938 (Red)
-- Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
-- Physics
local physics = require('physics')
physics.start()
physics.setGravity(0,0)
--physics.setDrawMode('hybrid')
-- Graphics
-- [Background]
local bg = display.newImage('bg.png')
-- [Title View]
local titleBg
local playBtn
local creditsBtn
local titleView
-- [Credits]
local creditsView
-- Game Background
local gameBg
-- Circles Group
local circles
-- Walls
local left
local right
local top
local bottom
-- Score TextField
local score
-- Variables
local lastY
local lastX
-- Functions
local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local gameListeners = {}
local onTouch = {}
local onCollision = {}
local alert = {}
-- Main Function
function Main()
titleBg = display.newImage('title.png', 0, 100)
playBtn = display.newImage('playBtn.png', 200, 240)
creditsBtn = display.newImage('creditsBtn.png', 200, 290)
titleView = display.newGroup(titleBg, playBtn, creditsBtn)
startButtonListeners('add')
end
function startButtonListeners(action)
if(action == 'add') then
playBtn:addEventListener('tap', showGameView)
creditsBtn:addEventListener('tap', showCredits)
else
playBtn:removeEventListener('tap', showGameView)
creditsBtn:removeEventListener('tap', showCredits)
end
end
function showCredits:tap(e)
playBtn.isVisible = false
creditsBtn.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)
lastY = titleBg.y
lastX = titleBg.x
transition.to(titleBg, {time = 300, y = (display.contentHeight * 0.5) - (titleBg.height - 15), x = (display.contentWidth * 0.5) - (titleBg.width * 0.5) + 55 })
transition.to(creditsView, {time = 300, y = (display.contentHeight * 0.5) + 35, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
function hideCredits:tap(e)
transition.to(creditsView, {time = 300, y = display.contentHeight + 25, onComplete = function() creditsBtn.isVisible = true playBtn.isVisible = true creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
transition.to(titleBg, {time = 300, y = lastY, x = lastX});
end
function showGameView:tap(e)
transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})
-- [Add GFX]
-- Game Background
display.remove(bg)
gameBg = display.newImage('gameBg.png')
-- Walls
left = display.newLine(0, 240, 0, 720)
right = display.newLine(320, 240, 320, 720)
top = display.newLine(160, 0, 480, 0)
bottom = display.newLine(160, 480, 480, 480)
-- Circles
circles = display.newGroup()
local color = 0
for i = 1, 5 do
local rx = 21 + math.floor(math.random() * (display.contentWidth - 42)) --was 21 and 42
local ry = 21 + math.floor(math.random() * (display.contentHeight - 42))--was 21 and 42
local cg = display.newCircle(rx, ry, 21)--was 21
local label = display.newText('20',cg.x-6.5, cg.y-6.2, native.systemFontBold, 13) --text was 0
cg.fillColor = color + (i*40)
cg:setFillColor(cg.fillColor)
local c = display.newGroup(cg, label)
c.pressed = false
c.name = 'c'
c.radius = 21
-- Circle Physics
physics.addBody(c, 'dynamic', {radius = 21, bounce = 1})--was 21
c:setLinearVelocity(50, 50)--was 100,100 = speed of balls on start
circles:insert(c)
end
-- Walls Physics
physics.addBody(left, 'static')
physics.addBody(right, 'static')
physics.addBody(top, 'static')
physics.addBody(bottom, 'static')
-- Score TextField
score = display.newText('100', 257, 4, native.systemFont, 15) -- score was 0 not 100
score:setTextColor(255, 252, 252)
local total = display.newText(' / 100', 267, 4, native.systemFont, 15)
total:setTextColor(255, 252, 252)
gameListeners('add')
end
function gameListeners(action)
if(action == 'add') then
for i = 1, 5 do
circles[i]:addEventListener('touch', onTouch)
circles[i]:addEventListener('collision', onCollision)
end
else
for i = 1, 5 do
circles[i]:removeEventListener('touch', onTouch)
circles[i]:removeEventListener('collision', onCollision)
end
end
end
function onTouch(e)
if(e.phase == 'began') then
e.target.pressed = true
-- Decrease Counter
score.text = tostring(tonumber(score.text) - 1)
-- Decrease size
e.target.radius = e.target.radius - 1
-- Change Color
e.target[1]:setFillColor(255,57,56)
end
if(e.phase == 'ended') then
e.target.pressed = false
-- Update physics
local number = tostring(tonumber(e.target[2].text)-1)
local r = e.target.radius
local cg = display.newCircle(e.target.x, e.target.y, r)
local label = display.newText(number ,cg.x-4.2, cg.y-6.2, native.systemFontBold, 13) -- was12.2
cg:setFillColor(e.target[1].fillColor)
cg.fillColor = e.target[1].fillColor
local c = display.newGroup(cg, label)
c.pressed = false
c.name = 'c'
c.radius = r
circles:remove(e.target)
physics.addBody(c, 'dynamic', {radius = r, bounce = 1})
c:setLinearVelocity(50, 50)--this changes the speed once touched was100,100
c:addEventListener('touch', onTouch)
c:addEventListener('collision', onCollision)
circles:insert(c)
-- Move Textfield when number is 2 digit
if(tonumber(number) > 9) then
label.x = label.x - 3
end
-- Check if score has reached 0
if(tonumber(score.text) <=0) then
local bg = display.newImage('gameBg.png')
transition.from(bg, {time = 500, alpha = 0, onComplete = alert('win')})
end
end
end
function onCollision(e)
if(e.target.pressed and e.other.name == 'c') then
-- Wait 0.1 seconds to stop physics
timer.performWithDelay(100, function() physics.stop() end, 1)
local r = e.target.radius
local c = display.newCircle(e.target.x, e.target.y, r)
c:setFillColor(255,57,56)
gameListeners('rmv')
transition.to(c, {time = 700, xScale = 500, yScale = 500, onComplete = alert('lost')}) --change from 25 to 50
end
end
function alert(action)
if(action == 'win') then
local alertView = display.newImage('won.png', 0, 105)
transition.from(alertView, {time = 300, y = -82, delay = 500})
local alertView = display.newImage('replayWinBtn.png', 200, 240)
transition.from(alertView, {time = 300, y = -82, delay = 500})
local alertView = display.newImage('replayWinMenuBtn.png', 200, 290)
transition.from(alertView, {time = 300, y = -82, delay = 500})
else
local alertView = display.newImage('lost.png', 0, 105)
transition.from(alertView, {time = 300, y = -82, delay = 500})
local alertView = display.newImage('replayLostBtn.png', 200, 240)
transition.from(alertView, {time = 300, y = -82, delay = 500})
local alertView = display.newImage('replayLostMenuBtn.png', 200, 290)
transition.from(alertView, {time = 300, y = -82, delay = 500})
end
end
Main()