on tap - problem with text transition

Hi,

I am new here. I am facing problem with on tap  - “text transition”.

 On taping the button, i want to remove the previous number(n) which in on display & the new number (n+1) is displayed. currently I am just been able to overwrite on the previous number. 

Here is my code.

 local function handleButtonEvent(event)
    local phase = event.phase
    if “ended” == phase then
 

    print (“You have pressed and released the button”)
   
 i = i + 1
 print (i)
 
    t3 = display.newText(i, 200, 200, verdana, 28)
    t3:setFillColor(220,51,273)
    
    transition.to(t3, {alpha=1, time = 1000})
            
 end

Please help me out.

Thanks in advance!

instead of creating a new text, you can change the .text parameter of the previous one (:

 

local t3 = = display.newText(i, 200, 200, verdana, 28) t3:setFillColor(220,51,273) local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) t3.text = i; transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end

@hachisoft - Thanks! it was quite useful.

Can I also, do the same for a “multiple line” text which i want to run on a loop (using the tap function)?

Thanks Again

Yup, just take into account that once created (if I’m not wrong), you can’t change the width/height of the multiple line text object.
So you can also destroy and create a new text multiline, like 

 

local t3 = display.newText(i, 200, 200, 300, 0, verdana, 28) --0 height makes it as tall as the text requires t3:setFillColor(220,51,273) local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) t3:removeSelf(); t3 = display.newText(i, 200, 200, 300, 0, verdana, 28) transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end

Hi hachisoft 

Thanks for the help. Hopefully this would be my last question on this topic.

In the multi-text I am getting an unexpected error in local t2. Am i doing a mistake?

t3 (the number increment is working superb.    

– display text
    local t2 = display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20)
    t2: setFillColor(255,0,255)
    t2.x = _W/9
    t2.y =_H/3
    --t2.anchorY = 0
    
   print (#testvalue)
 
 --display number
 local t3 = display.newText(i, 300, 150, verdana, 28)
t3:setFillColor(220,51,273)

  – Function to handle

local function handleButtonEvent(event)
    local phase = event.phase
    if “ended” == phase then
 
    print (“You have pressed and released the button”)
   
 i = i + 1
 print (i)
 
    t2:removeSelf()
    t2= display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20)
    t2: setFillColor(255,0,255)
    t2.x = _W/9
    t2.y =_H/3
    
    transition.from(t2, {alpha=0, time = 1000})
    
    t3.text = i;

    transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it’s already at alpha = 1, let’s use a transition.from from alpha = 0.
            
 end
 
 end
 

could you please help out.

I haven’t tried out the code yet, but I assume it happens when you tap and the transition of t2 hasn’t completed yet.
I’ve put a version of the code that takes care of that, let me know if it fixes it (: This also makes sure that t3 cancels its transition before making a new one.

-- display text local t2 = display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20) t2: setFillColor(255,0,255) t2.x = \_W/9 t2.y =\_H/3 --t2.anchorY = 0 print (#testvalue) --display number local t3 = display.newText(i, 300, 150, verdana, 28) t3:setFillColor(220,51,273) -- Function to handle local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) if t2 then transition.cancel(t2.currentTransition); t2:removeSelf(); end t2= display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20) t2: setFillColor(255,0,255) t2.x = \_W/9 t2.y =\_H/3 t2.currentTransition = transition.from(t2, {alpha=0, time = 1000}) t3.text = i; transition.cancel(t3.currentTransition); t3.currentTransition = transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end end

Hi Hachisoft

It is working… thanks a ton.

Appreciate your help.

That’s great (; 
Good luck with your project!

instead of creating a new text, you can change the .text parameter of the previous one (:

 

local t3 = = display.newText(i, 200, 200, verdana, 28) t3:setFillColor(220,51,273) local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) t3.text = i; transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end

@hachisoft - Thanks! it was quite useful.

Can I also, do the same for a “multiple line” text which i want to run on a loop (using the tap function)?

Thanks Again

Yup, just take into account that once created (if I’m not wrong), you can’t change the width/height of the multiple line text object.
So you can also destroy and create a new text multiline, like 

 

local t3 = display.newText(i, 200, 200, 300, 0, verdana, 28) --0 height makes it as tall as the text requires t3:setFillColor(220,51,273) local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) t3:removeSelf(); t3 = display.newText(i, 200, 200, 300, 0, verdana, 28) transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end

Hi hachisoft 

Thanks for the help. Hopefully this would be my last question on this topic.

In the multi-text I am getting an unexpected error in local t2. Am i doing a mistake?

t3 (the number increment is working superb.    

– display text
    local t2 = display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20)
    t2: setFillColor(255,0,255)
    t2.x = _W/9
    t2.y =_H/3
    --t2.anchorY = 0
    
   print (#testvalue)
 
 --display number
 local t3 = display.newText(i, 300, 150, verdana, 28)
t3:setFillColor(220,51,273)

  – Function to handle

local function handleButtonEvent(event)
    local phase = event.phase
    if “ended” == phase then
 
    print (“You have pressed and released the button”)
   
 i = i + 1
 print (i)
 
    t2:removeSelf()
    t2= display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20)
    t2: setFillColor(255,0,255)
    t2.x = _W/9
    t2.y =_H/3
    
    transition.from(t2, {alpha=0, time = 1000})
    
    t3.text = i;

    transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it’s already at alpha = 1, let’s use a transition.from from alpha = 0.
            
 end
 
 end
 

could you please help out.

I haven’t tried out the code yet, but I assume it happens when you tap and the transition of t2 hasn’t completed yet.
I’ve put a version of the code that takes care of that, let me know if it fixes it (: This also makes sure that t3 cancels its transition before making a new one.

-- display text local t2 = display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20) t2: setFillColor(255,0,255) t2.x = \_W/9 t2.y =\_H/3 --t2.anchorY = 0 print (#testvalue) --display number local t3 = display.newText(i, 300, 150, verdana, 28) t3:setFillColor(220,51,273) -- Function to handle local function handleButtonEvent(event) local phase = event.phase if "ended" == phase then print ("You have pressed and released the button") i = i + 1 print (i) if t2 then transition.cancel(t2.currentTransition); t2:removeSelf(); end t2= display.newText (testvalue[i], 0, 0, 300, 200, native.systemFont, 20) t2: setFillColor(255,0,255) t2.x = \_W/9 t2.y =\_H/3 t2.currentTransition = transition.from(t2, {alpha=0, time = 1000}) t3.text = i; transition.cancel(t3.currentTransition); t3.currentTransition = transition.from(t3, {alpha=0, time = 1000}) --if you want the text to fade in again, since it's already at alpha = 1, let's use a transition.from from alpha = 0. end end

Hi Hachisoft

It is working… thanks a ton.

Appreciate your help.

That’s great (; 
Good luck with your project!