Is there a way to save the CountUp timer after finishing the scene as a text to other lua file? But im having an error with SaveTimer.lua on text=currentTime it said bad argument on newText. What will happen is after using timer.lua it will save its counUpText on my SaveTimer.lua
SaveTimer.lua
local time_label = display.newText({parent=uiGroup, text=currenTime, font=native.systemFontBold, fontSize=128, align="center"})
time_label.x = 540
time_label.y = 750
time_label:setFillColor(0.9,0.9,0)
Timer.lua
currentTime
local gameTime = 0
function startTimer()
clockTimer = timer.performWithDelay(1000,doCountUp,gameTime)
end
function doCountUp()
currentTime = countUpText.text
currentTime = currentTime +1
countUpText.text = currentTime
if(currentTime == 0) then
countUpText.text = currentTime
startTimer()
end
end
countUpText = display.newText(gameTime,259,50,native.systemFontBold,100)
countDownText:setTextColor("#000000")
I suggest you to try it this way:
local time_label = display.newText(""..currentTime.."", display.contentCenterX, display.contentCenterY, font=native.systemFontBold, fontSize=128)
time_label.x = 540
time_label.y = 750
time_label:setFillColor(0.9,0.9,0)
Sorry, I could not format that code to code field because I am on my smartphone.
Related
Corona SDK timer countUp speeds up when button is touch? I do have 15 question in my game and everytime the answer is being touch the timer countUp speeds up everytime it goes to the another question..
Here is my button touch event
buttonTouched = function(event)
local t = event.target
local id = t.id
if event.phase == "began" and touchEnabled == true then
display.getCurrentStage():setFocus( t )
t.isFocus = true
if id == "answer" then
t.alpha = 0.6
else
t.xScale = 0.9
t.yScale = 0.9
end
elseif t.isFocus then
if event.phase == "ended" then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if id == "answer" then
t.alpha = 1
else
t.xScale = 1
t.yScale = 1
end
-- Check that touch finished in the button.
local b = t.contentBounds
if event.x >= b.xMin and event.x <= b.xMax and event.y >= b.yMin and event.y <= b.yMax then
utils.playSound("select")
if id == "answer" then
if timer_trans ~= nil then
transition.cancel(timer_trans)
timer_trans = nil
end
if result_trans ~= nil then
transition.cancel(result_trans)
result_trans = nil
end
if label_result ~= nil then
display.remove(label_result)
label_result = nil
end
-- Show some text that we can transition
label_result = display.newText({parent=uiGroup, text="", font=easyFont, fontSize=75})
label_result.anchorX = 0
label_result.x = label_question.x - 540
label_result.y = label_question.y + 400
if t.index == questions[onQuestion].answer then
label_result.text = "Correct!"
label_result:setFillColor(0,0.6,0)
utils.playSound("score")
updateScore(1)
else
label_result.text = "Incorrect..."
label_result:setFillColor(0.8,0,0)
utils.playSound("incorrect")
end
result_trans = transition.to(label_result, {time=1600, alpha=0.1, y=label_result.y-18,tag="transTag", onComplete=function()
display.remove(label_result)
label_result = nil
end})
-- Now create the next quesiton
createQuestion()
end
end
end
end
return true
end
function startTimer()
clockTimer = timer.performWithDelay(1000,doCountUp,gameTime)
end
function doCountUp()
currentTime = countUpText.text
currentTime = currentTime +1
countUpText.text = currentTime
if(currentTime == 0) then
countUpText.text = currentTime
startTimer()
end
end
The timer is "speeding up" because instead of resetting the current timer you are creating a new timer when you click the question.
You call startTimer() every time you create a new question
(assuming you set countUpText.text = "-1" in createQuestion(). Every time you touch the answer you create another timer to update the countUpText.text. You have multiple timers updating the text because you don't delete the previously created timers you are just creating new ones.
The easiest way to fix this is to cancel the timer and start a new one if a timer has been created:
local clockTimer
function startTimer()
if (clockTimer ~= nil) then
timer.cancel(clockTimer)
clockTimer = nil
end
clockTimer = timer.performWithDelay(1000,doCountUp,gameTime)
end
So update your startTimer() function to the above and then add local clockTimer to the top of your Lua file.
so while building a mobile game with Corona SDK i am encountering some problems now and then. One of them I didn't seem to solve :
When spawning display objects in a loop, there seems to randomly appear a positional difference between two of the objects in a row.
At first, I thought this was due to large chunks of code that were executed between the actual spawning and the start of the transition, but then I managed to reproduce the same problem in few lines :
local rectangleLoopTimer;
local counter = 0;
local rectangleArray = {}
local function rectangleLoop()
counter = counter + 1
local thisRectangle = display.newRect(1, 1, 216, 400)
thisRectangle.anchorX = 0
table.insert(rectangleArray, thisRectangle)
transition.to(
thisRectangle,
{
time = 5000,
x = thisRectangle.x + 1080,
onComplete = function()
display.remove(thisRectangle)
table.remove(rectangleArray, counter)
end
}
)
end
rectangleLoopTimer = timer.performWithDelay(985, rectangleLoop, 0)
If one executes this, then one sees what I mean, so what do you think why this happens? I appreciate every answer!
Greetings, Nils
EDIT:
This also produces the same problem :
local rectangleLoopTimer;
local counter = 0
local rectangleArray = {}
local thisRectangle
local function rectangleLoop()
counter = counter + 1
thisRectangle = display.newRect(1, 1, 216, 400)
thisRectangle.anchorX = 0
thisRectangle.lastTime = 0
thisRectangle.rate = 216
table.insert(rectangleArray, thisRectangle)
thisRectangle.lastTime = system.getTimer()
thisRectangle.enterFrame = function(self, event)
local curTime = system.getTimer()
local dt = curTime - self.lastTime
self.lastTime = curTime
local dx = self.rate * dt / 1000
self.x = self.x + dx
end
Runtime:addEventListener("enterFrame", thisRectangle)
end
rectangleLoopTimer = timer.performWithDelay(1000, rectangleLoop, 0)
RE-EDIT:
This code also produces the same problem, albeit using framerate independent animation. The issue is getting emphasized when increasing the speed of the loop as in the code below :
local loopSpeed = 306
local loopTimerSpeed = 1000
local gapTable = {}
local gapLoopTimer
local frameTime
local gap
--enterFrame for time only
local function frameTime(event)
frameTime = system.getTimer()
end
--enterFrame
local function enterFrame(self, event)
local deltaTime = frameTime - self.time
print(deltaTime/1000)
self.time = frameTime
local speed = self.rate * deltaTime / 1000
self:translate(speed, 0)
end
--loop speed function
local function setLoopSpeed(factor)
loopSpeed = loopSpeed * factor
loopTimerSpeed = loopTimerSpeed / factor
end
--set the loop speed
setLoopSpeed(3)
--loop to create gaps
local function createGap()
gap = display.newRect(1, 1, 308, 442)
gap.time = system.getTimer()
gap.anchorX = 1
gap.anchorY = 0
--animation
gap.rate = loopSpeed
gap.enterFrame = enterFrame
Runtime:addEventListener("enterFrame", gap)
--fill table for cleaning up
table.insert(gapTable, gap)
--cleaning up
for i = #gapTable, 1, -1 do
local thisGap = gapTable[i]
if thisGap.x > display.contentWidth + 500 then
display.remove(thisGap)
table.remove(gapTable, i)
Runtime:removeEventListener("enterFrame", thisGap)
end
thisGap = nil
end
end
Runtime:addEventListener("enterFrame", frameTime)
gapLoopTimer = timer.performWithDelay(
loopTimerSpeed,
createGap,
0
)
This is a very common problem with transitions, and [to me] a bug in Corona SDK.
The important thing to note is how transitions work.
Transitions are nothing else than a table with references to objects and information about what should be done to them each frame.
Each frame such object is retrieved and current time is used to calculate the difference that should be applies to the values of the object, as specified in the transition itself.
This basically means that if you ask Corona to move an object from x = 0 to x = 100 in time = 100. Each frame, Corona will take that information, take current time, and will calculate the x value of your object.
The issue here is, that the current time taken, is current time at a time of calculation, and not time of the frame. It means, that if you have a lot of transitions, it could be quite a few milliseconds between first and last of the transitions within one frame. This will result in different positions within same frame.
If Corona would take frame time [so time at the beginning of the frame] it would use the same value to calculate everything, and no matter how many objects you would be transitioning from A to B, all of them would appear in the same place in all of the frames.
The easiest way to fix this, would be to handle transitions manually in enterFrame or use a library which does it for you, for example: AKTween.
Hope this helps.
EDIT:
Based on your additional code and comments, I think this should work as you wanted. Please forgive me the code quality, I wrote it from memory and didn't test it in Corona.
local rectangleLoopTimer;
local allRectangles = display.newGroup()
local lastTime = system.getTimer()
local function enterFrame()
local curTime = system.getTimer()
local dt = curTime - lastTime
lastTime = curTime
for i = allRectangles.numChildren, 1 do
local rect = allRectangles[i]
local dx = rect.rate * dt / 1000
rect.x = rect.x + dx
end
end
Runtime:addEventListener("enterFrame", enterFrame)
local function createRectangle()
local thisRectangle = display.newRect(1, 1, 216, 400)
thisRectangle.anchorX = 0
thisRectangle.lastTime = 0
thisRectangle.rate = 216
allRectangles:insert(thisRectangle)
end
timer.performWithDelay(1000, createRectangle, 0)
EDIT AFTER RE-EDIT of the post:
You have time set in enterFrame listener, but you don't actually know when it's going to be called. I would not count on the order of functions called during enterFrame stage.
If you don't need futher reference to rects use code below
local rand = math.random
local function rectangleLoop()
local thisRectangle = display.newRect(1, 1, 216, 400)
thisRectangle.anchorX = 0
thisRectangle:setFillColor(rand(), rand(), rand())
transition.to(thisRectangle, {time=5000,x=thisRectangle.x + 1080, onComplete=display.remove})
end
rectangleLoopTimer = timer.performWithDelay(985, rectangleLoop, 0)
Do you need use table to store rects?
So while building a mobile game with Corona SDK I have recently been facing a problem, that I couldn't solve, which are positional differences when spawning display objects in a loop. I got some help and found out this must have something to do with framerate independent animation. But now im facing this :
Albeit I'm using framerate independent animation here, this also produces the same problem. This gets emphasized by increasing the speed of the loop, as in the code below. What are your thoughts on this?
local loopSpeed = 306
local loopTimerSpeed = 1000
local gapTable = {}
local gapLoopTimer
local frameTime
local gap
--enterFrame for time only
local function frameTime(event)
frameTime = system.getTimer()
end
--enterFrame
local function enterFrame(self, event)
local deltaTime = frameTime - self.time
print(deltaTime/1000)
self.time = frameTime
local speed = self.rate * deltaTime / 1000
self:translate(speed, 0)
end
--loop speed function
local function setLoopSpeed(factor)
loopSpeed = loopSpeed * factor
loopTimerSpeed = loopTimerSpeed / factor
end
--set the loop speed
setLoopSpeed(3)
--loop to create gaps
local function createGap()
gap = display.newRect(1, 1, 308, 442)
gap.time = system.getTimer()
gap.anchorX = 1
gap.anchorY = 0
--animation
gap.rate = loopSpeed
gap.enterFrame = enterFrame
Runtime:addEventListener("enterFrame", gap)
--fill table for cleaning up
table.insert(gapTable, gap)
--cleaning up
for i = #gapTable, 1, -1 do
local thisGap = gapTable[i]
if thisGap.x > display.contentWidth + 500 then
display.remove(thisGap)
table.remove(gapTable, i)
Runtime:removeEventListener("enterFrame", thisGap)
end
thisGap = nil
end
end
Runtime:addEventListener("enterFrame", frameTime)
gapLoopTimer = timer.performWithDelay(
loopTimerSpeed,
createGap,
0
)
If you can set up size of gap between rects try use code below
local gapTable = {}
local gapWidth = 50
local runtime = 0
local speed = 20
local gap
local function getDeltaTime()
local temp = system.getTimer() -- Get current game time in ms
local dt = (temp-runtime) / (1000/display.fps) -- 60 fps or 30 fps as base
runtime = temp -- Store game time
return dt
end
local function enterFrame()
local dt = getDeltaTime()
for i=1, #gapTable do
gapTable[i]:translate(speed * dt, 0)
end
end
local function createGap()
local startX = 0
if #gapTable > 0 then
startX = gapTable[#gapTable].x - gapWidth - gapTable[#gapTable].width
end
gap = display.newRect(startX, 1, 308, 442)
gap.anchorX, gap.anchorY = 1, 0
table.insert(gapTable, gap)
--cleaning up
for i=#gapTable, 1, -1 do
if gapTable[i].x > display.contentWidth + 500 then
local rect = table.remove(gapTable, i)
if rect ~= nil then
display.remove(rect)
rect = nil
end
end
end
end
timer.performWithDelay(100, createGap, 0)
Runtime:addEventListener("enterFrame", enterFrame)
Hope this helps:)
I am implementing a function to have a countdown in Angular form current time - existing time in future. If the time has elapsed then display a message. Timer ran out in ..... HH:MM:SS
The end time. Lets call it endTime eg:
9/15/2016 9:16:00 PM
Current time. Time current moment we live.
Lets call it currentTime.
The goal is to get a timer that is Current time - end time. Save it to a Variable TotalHours.
Then calculate the time remaining for NOW to total hours. For example TotalHours = 5. And NOW is 9/14/2016 1:16:00 PM then FinalCountDown = 6:16:00 PM. That is the timer I want running...
Here is how I am doing it...
if (info.endTime) {
var CurrentTime = new Date().toLocaleString('en-US');
moment.locale(); // en
var TotalHours = moment.utc(moment(info.diffTime, "DD/MM/YYYY HH:mm:ss").diff(moment(CurrentTime, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");
info.finalCountDown= TotalHours;
};
The issue here is the following:
Case 1:
endTime = 9/15/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
But... if its after next 2 days...
Case 2:
endTime = 9/17/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
Total hours is still the same...
I need it to add 24hours + 24 hours + extra time = 48 + 4:05:00 = 52:05:00
also I want it to display as: 52h:05m:00s
Please let me know how to solve this...
A quick and dirty solution would be to simply convert the difference between the two date/time objects to milliseconds and then do some math on the milliseconds and format the output as follows:
var currentTime = new Date("9-15-2016 13:21:00");
var endTime = new Date("9-17-2016 09:16:00");
var ms = (endTime - currentTime); // ms of difference
var days = Math.round(ms/ 86400000);
var hrs = Math.round((ms% 86400000) / 3600000);
var mins = Math.round(((ms% 86400000) % 3600000) / 60000);
$scope.finalCountdown = (days + "d:" + hrs + " h:" + mins + "m left");
You could add in a calculation for the seconds if you needed and you can do some formatting of the numbers to have leading zeros.
However, doing this doesn't account for issues such as leap-years and other data and time anomalies. A better suggestion would be to use angular-moment which utilizes Moment.js as it can handle differences and formatting with ease.
I have a newCircle that when clicked changes the color of the circle and starts a timer it it. i want to be able to click the circle again and change it back to black and completely remove the timer from the scene.
delta = 0
local function tapListener( event )
if (delta == 0) then
c1:setFillColor(1,1,0)
local timeLimit = 20
timeLeft = display.newText(timeLimit, c1.x, c1.y, native.systemFontBold, 14)
timeLeft:setTextColor(255,0,0)
local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
print("Time Out") -- or do your code for time out
end
end
aTimer = timer.performWithDelay(1000,timerDown,timeLimit)
delta = delta + 1
else
c1:setFillColor(0,0,0)
delta = delta - 1
end
answer:
else
c1:setFillColor(0,0,0)
timer.cancel( aTimer )
timeLeft.alpha = 0
delta = delta - 1
end
the timer.cancal( aTimer ) stops the timer and the timeLeft.aplha = 0 hides the text that is displaying the timer