所以我有一種情況。我理解timer.perforWithDelay,但它似乎得到指數更快,因爲我有一個循環階段的遞歸函數調用。所以我們的遊戲在屏幕上顯示一個對象,你必須將它滑動到正確的位置。除了一件事情之外,我有遊戲全部運行:計時器。每次在第一個對象階段開始後,形狀都會彈出,我想要一個5秒的定時器。這裏是我的代碼:Lua:遞歸函數中的階段計時器
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的練習之後,使用帶有Lua的Corona SDK創建應用程序。我相信你會通過閱讀代碼來發現這一點。任何幫助將不勝感激,謝謝。