所以我遇到了一个情况。我理解timer.perforWithDelay但它似乎以指数方式更快,因为我有一个递归函数调用来循环一个阶段。因此,我们的游戏在屏幕上显示一个对象,您必须将其滑动到正确的位置。我让游戏运行除了一件事:计时器。每次在第一个物体阶段开始后弹出一个形状时,我想要一个5秒的计时器。这是我的代码:
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
local game_sound = audio.loadStream( "GameScreenAudio.mp3" )
local sound = audio.loadStream( "splashMusic.mp3" )
audio.play(game_sound, {loops = -1} )
function scene:create( event )
local group = self.view
--LOAD IN BACKGROUND AND LOCATE IT TO FIT----------------------------------------------------
local background = display.newImage("game_background.png")
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
group:insert( background )
---------------------------------------------------------------------------------------------
--Screen GUI---------------------------------------------------------------------------------
local back_triangle = display.newImage("back_triangle.png")
back_triangle.x = display.contentWidth * .87
back_triangle.y = display.contentHeight * .5
back_triangle.xScale = .7; back_triangle.yScale = .7
back_triangle.alpha = .4
local back_diamond = display.newImage("back_diamond.png")
back_diamond.y = display.contentHeight * .5
back_diamond.x = display.contentWidth * .1
back_diamond.xScale = .7; back_diamond.yScale = .7
back_diamond.alpha = .4
local back_circle = display.newImage("back_circle.png")
back_circle.x = display.contentWidth * .5
back_circle.y = display.contentHeight * .1
back_circle.xScale = .7; back_circle.yScale = .7
back_circle.alpha = .35
local back_rectangle = display.newImage("back_rectangle.png")
back_rectangle.x = display.contentWidth * .5
back_rectangle.y = display.contentHeight * .9
back_rectangle.xScale = .7; back_rectangle.yScale = .7
back_rectangle.alpha = .4
group:insert( back_triangle )
group:insert( back_diamond )
group:insert( back_circle )
group:insert( back_rectangle )
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
--Load in Images-----------------------------------------------------------------------------
--GOOD
-- local excellent = display.newImage("excellent.png")
-- local incredible = display.newImage("incredible.png")
-- local wicked = display.newImage("wicked.png")
-- local good_for_you = display.newImage("good4you.png")
-- local amazing = display.newImage("amazing.png")
-- local spectacular = display.newImage("spectacular.png")
-- local groovy = display.newImage("groovy.png")
-- local stupendous = display.newImage("stupendous.png")
-- local class_act = display.newImage("classact.png")
-- local top_notch = display.newImage("topnotch.png")
-- local yea_man = display.newImage("yeaman.png")
-- local slayer = display.newImage("slayer.png")
-- --BAD
-- local wrecked = display.newImage("wrecked.png")
-- local atrocious = display.newImage("atrocious.png")
-- local bummer = display.newImage("bummer.png")
-- local unacceptable = display.newImage("unacceptable.png")
-- local wretched = display.newImage("wretched.png")
-- local despicable = display.newImage("despicable.png")
-- local revolting = display.newImage("revolting.png")
-- local fail = display.newImage("fail.png")
-- local wrong = display.newImage("wrong.png")
-- local nope = display.newImage("nope.png")
-- imageList_GOOD = {excellent, incredible, wicked, good_for_you, amazing, spectacular, groovy, stupendous, class_act, top_notch, yea_man, slayer}
-- imageList_BAD = {wrecked, atrocious, bummer, unacceptable, wretched, despicable, revolting, fail, wrong, nope}
imageList_GAME = {"triangle.png", "circle.png", "rectangle.png", "diamond.png"}
------------------------1--------------2---------------3----------------4-----------------------
local score = 0
local streak = 0
local strikes = 0
local status = nil
local neg_streak = 0
local timeLimit = 5
local score_text = display.newText("Score: ", display.contentWidth*.10, display.contentHeight*.98, native.systemFontBold, 20)
group:insert( score_text )
function gameFunction()
local game_number = math.random(1 , 4)
local shape = display.newImage(imageList_GAME[game_number])
shape.x = display.contentWidth * .5
shape.y = display.contentHeight * .5
local occupied = true
group:insert( shape )
--CREATE BACK BUTTON WITH FUNCTIONALITY---------------------------------------------------------
local back_button = display.newImage("back_button.png")
back_button.x = display.contentWidth * .93
back_button.y = display.contentHeight * .945
back_button.xScale = .25; back_button.yScale = .25
function back_button:tap(event)
if strikes < 3 then
shape:removeSelf()
composer.removeScene("game" )
local options =
{
effect = 'fromLeft',
time = 400
}
audio.pause(game_sound)
audio.play(sound)
composer.gotoScene( "splash" , options)
elseif strikes == 3 then
end
end
back_button:addEventListener( "tap", back_button )
group:insert( back_button )
local score_points = display.newText(score, display.contentWidth* .30, display.contentHeight*.98, native.systemFontBold, 18)
group:insert( score_points )
physics.addBody(shape, "dynamic")
local motion = nil
-- touch listener function
function shape:touch( event )
local shape = event.target
if event.phase == "began" then
shape.previousX = shape.x
shape.previousY = shape.y
elseif event.phase == "moved" then
shape.x = (event.x - event.xStart) + shape.previousX
shape.y = (event.y - event.yStart) + shape.previousY
elseif event.phase == "ended" and strikes < 3 then
getPositionStatus()
applyForce2Object()
back_button:removeSelf()
score_points:removeSelf()
timeLimit = 5
gameFunction()
end
end
shape:addEventListener( "touch", shape )
function getPositionStatus()
--SCORING-------------------------------------------------------------------------------------------
if game_number == 1 and shape.x > display.contentWidth * .6 then
status = "correct"
score = score + 100
streak = streak + 1
neg_streak = 0
elseif game_number == 2 and shape.y < display.contentHeight * .4 then
status = "correct"
score = score + 100
streak = streak + 1
neg_streak = 0
elseif game_number == 3 and shape.y > display.contentHeight * .6 then
status = "correct"
score = score + 100
streak = streak + 1
neg_streak = 0
elseif game_number == 4 and shape.x < display.contentWidth * .4 then
status = "correct"
score = score + 100
streak = streak + 1
neg_streak = 0
else
status = "incorrect"
score = score - 100
streak = 0
neg_streak = neg_streak - 1
strikes = strikes + 1
end
print("Status: " , status)
print("Score: " , score)
print("Strikes: " , strikes)
end
function applyForce2Object()
--What did user do?-------------------------------
if shape.x > display.contentWidth * .6 then
shape:applyForce(2000, 0, shape.x, shape.y)
elseif shape.y > display.contentHeight * .6 then
shape:applyForce(0, 2000, shape.x, shape.y)
elseif shape.y < display.contentHeight * .35 then
shape:applyForce(0, -2000, shape.x, shape.y)
elseif shape.x < display.contentWidth * .35 then
shape:applyForce(-2000, 0, shape.x, shape.y)
end
end
if strikes == 3 then
shape:removeSelf()
print("*********GAME OVER************")
end
end
gameFunction()
end --CREATE scene end
scene:addEventListener( "create", scene )
return scene
因此,使用我的代码和它调用的递归函数,每次对象出现在屏幕上时,如何让计时器正常工作5秒钟。如果物体没有及时刷过,我需要添加一个打击并移除100个点。请记住,我是初学者。对于我们在大学的信息技术课程介绍,我们获得了一项任务,即在使用Lua进行一点练习之后,使用Corona SDK和Lua创建一个应用程序。我确定你会通过阅读代码来发现这一点。任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
我假设一次只显示一个形状,定时器的代码进入显示该形状的函数内部,当我通过您的代码读取时,gameFunction()
内部的形状
其中back_button的代码也存在,这是错误的,我建议你把它分开。另一个建议是为UI中的每个对象创建另一个显示组,假设UI包括:Score文本,Strikes文本等。为形状创建另一个组,您必须完全了解设计层次结构你的游戏,将它们分开。
回到计时器代码,像这样做
local gameTimer -- forward reference
local timeLimit = 5
local function = timerListener()
--handles timer event
timeLimit = timeLimit - 1
--here you can print the time
if (timeLimit == 0) then
--time's up
timer.cancel(gameTimer)
gameTimer = nill --we clear it out
timerLimit = 5 --reset the timer back to 5
--in here you can print that the player has strike.
--anything you want to do when the player failed to move the shape
end
end
gameFunction()
--code to show the shape
gameTimer = timer.performWithDelay( 1000, timerListener , 5)
end --end of gameFunction
然后,当玩家移动形状正确或错误时,只需执行时间到时所做的操作,取消它,取消,然后重置时间。 你可以把它放在另一个函数中:
local function resetTimer()
timer.cancel(gameTimer)
gameTimer = nill
timerLimit = 5
end
然后就把它叫出来。
还有一件事,如果你很难在屏幕上移除形状,就像我建议的那样,将它们放在一个单独的组中,只需清除组,对于事件监听器,只需要事件监视器在该组中也是如此,因为只显示了一个形状。如果您在屏幕中显示许多形状,这将会改变,因为您必须在组堆栈中引用它
快乐编码:)