setReferencePoint error with director (SOLVED!)

I got this function:

function printTime()  
 secondsEffect()  
 secondsPast = secondsPast+1  
  
 if secondsPast == 60 then  
 minutesEffect()  
 minutesPast = minutesPast+1  
 secondsPast = 0  
 end  
  
 seconds.text = secondsPast  
  
 seconds:setReferencePoint(display.CenterLeftReferencePoint)  
 seconds.x = display.contentWidth/2 + 12  
  
 minutes.text = minutesPast  
 minutes:setReferencePoint(display.CenterRightReferencePoint)  
 minutes.x = display.contentWidth/2 - 12  
  
  
 end  
 function startTimer()  
 timer.performWithDelay( 1000, printTime, 0)  
 end  
  
 startTimer()  
  

But when i change the screen I get this error:

attempt to call method 'setReferencePoint' (a nil value)  

[/code] [import]uid: 24111 topic_id: 12559 reply_id: 312559[/import]

I tried the below code am not getting any error on that

[lua] local secondsPast = 1
local minutesPast = 0
local seconds = display.newText(“0”,120,120,nil,25)
local minutes = display.newText(“0”,150,120,nil,25)

seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = display.contentWidth/2 + 12

minutes.text = minutesPast
minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = display.contentWidth/2 - 12
function printTime()
–secondsEffect()
secondsPast = secondsPast+1

if secondsPast == 60 then
– minutesEffect()
minutesPast = minutesPast+1
secondsPast = 0
end

seconds.text = secondsPast

seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = display.contentWidth/2 + 12

minutes.text = minutesPast
minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = display.contentWidth/2 - 12
end
function startTimer()
timer.performWithDelay( 1000, printTime, 0)
end

startTimer()[/lua]

so i believe the problem is not with the code that you send… it may be with some other part… [import]uid: 71210 topic_id: 12559 reply_id: 45917[/import]

[code]
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
kolon = display.newText(":", 50,50, system.defaultFont, 50)
kolon:setTextColor(250,250,250)
kolon:setReferencePoint(display.CenterReferencePoint)
kolon.x = display.contentWidth/2
kolon.y = display.contentHeight/1.24
localGroup:insert(kolon)

secondsPast = 0
minutesPast = 0

seconds = display.newText(secondsPast,50,50,system.defaultFont, 50)
seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = kolon.x + 12
seconds.y = display.contentHeight/1.24
localGroup:insert(seconds)

minutes = display.newText(minutesPast,50,50,system.defaultFont, 50)
minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = kolon.x - 12
minutes.y = display.contentHeight/1.24

localGroup:insert(minutes)

function secondsEffect()
transition.to(seconds,{time=100, y=seconds.y-40 })
transition.to(seconds,{time=100, delay=50, y=seconds.y})
end

function minutesEffect()
transition.to(minutes,{time=100, y=minutes.y-40 })
transition.to(minutes,{time=100, delay=50, y=minutes.y})
end

function printTime()
secondsEffect()
secondsPast = secondsPast+1

if secondsPast == 60 then
minutesEffect()
minutesPast = minutesPast+1
secondsPast = 0
end

seconds.text = secondsPast

seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = display.contentWidth/2 + 12

minutes.text = minutesPast
minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = display.contentWidth/2 - 12

end
function startTimer()
timer.performWithDelay( 1000, printTime, 0)
end

startTimer()
return localGroup
end
[/code] [import]uid: 24111 topic_id: 12559 reply_id: 45919[/import]

code looks fine…
can you do something…
put a print statement after function new() and see whether the program is reaching that scene… [import]uid: 71210 topic_id: 12559 reply_id: 45921[/import]

Tested it and it gets to next scene before I get the error. [import]uid: 24111 topic_id: 12559 reply_id: 45923[/import]

I can’t regenerate the error…
can you try commenting the lines with SetReferencePoint let’s see what happens. [import]uid: 71210 topic_id: 12559 reply_id: 45926[/import]

What do you mean? [import]uid: 24111 topic_id: 12559 reply_id: 45932[/import]

I commented the lines and now it doesn’t give an error but:

The reference point is wrong.

When it switches scene and you got back to it, the time text jumps away and it is there for more than 3 seconds.

[code]

module(…, package.seeall)
function new()
local localGroup = display.newGroup()
kolon = display.newText(":", 50,50, system.defaultFont, 50)
kolon:setTextColor(250,250,250)
kolon:setReferencePoint(display.CenterReferencePoint)
kolon.x = display.contentWidth/2
kolon.y = display.contentHeight/1.24
localGroup:insert(kolon)

secondsPast = 0
minutesPast = 0

seconds = display.newText(secondsPast,50,50,system.defaultFont, 50)
seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = kolon.x + 12
seconds.y = display.contentHeight/1.24
localGroup:insert(seconds)

minutes = display.newText(minutesPast,50,50,system.defaultFont, 50)
minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = kolon.x - 12
minutes.y = display.contentHeight/1.24

localGroup:insert(minutes)

function secondsEffect()
if seconds.y then
transition.to(seconds,{time=100, y=seconds.y-40 })
transition.to(seconds,{time=100, delay=50, y=seconds.y})
end
end

function minutesEffect()
if minutes.y then
transition.to(minutes,{time=100, y=minutes.y-40 })
transition.to(minutes,{time=100, delay=50, y=minutes.y})
end
end

function printTime()
secondsEffect()
secondsPast = secondsPast+1

if secondsPast == 60 then
minutesEffect()
minutesPast = minutesPast+1
secondsPast = 0
end

seconds.text = secondsPast

– seconds:setReferencePoint(display.CenterLeftReferencePoint)
seconds.x = display.contentWidth/2 + 12

minutes.text = minutesPast
– minutes:setReferencePoint(display.CenterRightReferencePoint)
minutes.x = display.contentWidth/2 - 12

end
function startTimer()
timer.performWithDelay( 1000, printTime, 0)
end

function onTimer()
director:changeScene(“mainmenu”)
end

timer.performWithDelay( 3000, onTimer,1)

startTimer()
return localGroup
end

[/code] [import]uid: 24111 topic_id: 12559 reply_id: 45942[/import]

I changed the text objects to labels instead:

  
module(..., package.seeall)  
function new()  
local localGroup = display.newGroup()  
local ui = require ("ui")  
--Kolon label  
local kolon = ui.newLabel {  
text = ":",  
 textColor = {255, 255, 255, 255},  
 size = 50,  
 font = "helvetica",  
 align = "center",  
 bounds = {0, 0, 0, 0},  
};  
kolon.x = display.contentWidth/2  
kolon.y = display.contentHeight/1.24  
localGroup:insert(kolon)  
secondsPast = 0  
minutesPast = 0  
  
--Seconds label  
local seconds = ui.newLabel {  
text = secondsPast,  
 textColor = {255, 255, 255, 255},  
 size = 50,  
 font = "helvetica",  
 align = "left",  
 bounds = {0, 0, 0, 0},  
};  
seconds.x = kolon.x + 12  
seconds.y = display.contentHeight/1.24  
localGroup:insert(seconds)  
local minutes = ui.newLabel {  
text = minutesPast,  
 textColor = {255, 255, 255, 255},  
 size = 50,  
 font = "helvetica",  
 align = "right",  
 bounds = {0, 0, 0, 0},  
};  
minutes.x = kolon.x - 12  
minutes.y = display.contentHeight/1.24  
localGroup:insert(minutes)  
  
function secondsEffect()  
 if seconds.y then  
 transition.to(seconds,{time=100, y=seconds.y-40 })  
 transition.to(seconds,{time=100, delay=50, y=seconds.y})  
 end  
end  
  
function minutesEffect()  
 if minutes.y then  
 transition.to(minutes,{time=100, y=minutes.y-40 })  
 transition.to(minutes,{time=100, delay=50, y=minutes.y})  
 end  
end  
  
function printTime()  
 secondsEffect()  
 secondsPast = secondsPast+1  
  
 if secondsPast == 60 then  
 minutesEffect()  
 minutesPast = minutesPast+1  
 secondsPast = 0  
 end  
  
 seconds:setText(secondsPast)  
  
 minutes:setText(minutesPast)  
  
  
 end  
 function startTimer()  
 timer.performWithDelay( 1000, printTime, 0)  
 end  
  
 function onTimer()  
 director:changeScene("mainmenu")  
 end  
  
 timer.performWithDelay( 3000, onTimer,1)  
  
 startTimer()  
 return localGroup  
end  
  

But I got a new error that says:

[code]

Runtime error
filepath/ui.lua:262: attempt to perform arithmetic on field ‘contentWidth’ (a nil value)
stack traceback:
[C]: ?
filepath/ui.lua:262: in function ‘setText’
filepath/timer2.lua:74: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>

[/code] [import]uid: 24111 topic_id: 12559 reply_id: 45944[/import]

The problem is that the timer doesn’t stop quickly enough.

Got it working with http://jonbeebe.tumblr.com/post/2439016279/how-to-use-the-new-time-functions

Now i got a game.cancelAllTimers() right before the director:changeScene(“mainmenu”)

Thank god for Jonathan Beebe :smiley: [import]uid: 24111 topic_id: 12559 reply_id: 45946[/import]