2014-11-25 49 views
0

所以我有一種情況。我理解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創建應用程序。我相信你會通過閱讀代碼來發現這一點。任何幫助將不勝感激,謝謝。

回答

0

我假設一次只顯示一個形狀,定時器的代碼進入顯示該形狀的函數內部,當我讀取代碼時,那裏面的gameFunction() 其中back_button的代碼在那裏另外,這是錯誤的,我建議你將它分開。另一個建議是爲UI中的每個對象創建另一個顯示組,讓用戶界面包括:分數文本,敲擊文本等。爲該形狀創建另一個組,爲了充分了解設計層次結構把你的遊戲分開。 回到計時器代碼,像這樣做

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 

然後,當玩家移動的形狀無論是對還是錯,只是做當時間到了,取消它,你做了什麼,nill,然後重置時間。 你可以把它放在另一個功能,如:

local function resetTimer() 
    timer.cancel(gameTimer) 
    gameTimer = nill 
    timerLimit = 5 
end 

然後叫它出來。

還有一件事,如果你有一個很難去除的形狀,在屏幕上,我建議,即把它們放在一個單獨的組,只要疏通組,併爲事件監聽器,只需將該組中的eventListener也是,因爲只顯示一個形狀。如果你在屏幕上顯示很多形狀,這將會改變,因爲你必須在組堆棧內參考它

快樂編碼:)