Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

help with function
Started by dodi_games Dec 27 2018 03:52 PM

10 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

I have seen numerous times that functions, timers and transitions are used within functions. but I want to know if I'm doing things right

 

 

local function showObject( event )
  local alphaTmr
  if alphaTmr ~= nil then
    timer.cancel( alphaTmr )
    alphaTmr = nil
  end
  local function objectAlpa()
    object.alpha = 1
  end
  --------------------
  if (someValue == 0) then
    --do something
  elseif (someValue == 1) then
   --do something
  else
    --blink object when number is equal to 2
    if( someValue == 2 ) then
      alphaTmr = timer.performWithDelay(1050, function()
        transition.blink( object, { time=1000, onStart=objectAlpa } ) end )
    end
  end
  ---------------------
  transition.fadeIn( object, { time=1000 } )
end

I'm canceling all the transitions in the scene hide.

 

Function purpose: I have a display oibject created with alpha = 0. The function is to change its fill image with some values and if the value is equal to 2 make it blink. Every time I enter the scene the object fades in.

 

Thanks in advance

DoDI



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

Just commenting on the timers and transitions.

When you declare "local alphaTmr" it is nil until you assign a value to it. On the very next row, you perform a check to see if it is not nil, but with your current code alphaTmr will always be nil. If you declare alphaTmr outside of the function, then the function can still read it as long as it is within the function's scope and then alphaTmr may not be nil when you load your function.

When you are working with timers and transitions, the reason why you may want to assign them to variables, like your alphaTmr, is that it allows you to easily control them after you've started them. With transitions, you have other means of pausing or cancelling them, like the transition.cancel() that you do at scene hide, but with timers, you won't be able to control them anymore if you create a timer locally inside a function as you'd need an object to reference to.



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,372 posts
  • Enterprise

local function showObject( event )
  local alphaTmr
...
end

Because of the way scope works, the variable "alphaTmr" is only visible to the function showObject(). If you try to cancel it in scene:hide(), you won't be able to access that timer.

 

Rob



[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

Will there be any way to use and cancel that timer within the same function?

Thanks in advance
DoDi

[TOPIC: post.html]
#5

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

Well, I did say how to do it already. :P

Simply declare it before the function, i.e.
 

local alphaTmr

local function showObject( event )
   ...
end

  • dodi_games likes this

[TOPIC: post.html]
#6

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

[TOPIC: post.html]
#7

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

@XeduR @Spyric

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,372 posts
  • Enterprise

It really helps if you take the time and develop a solid understanding of "scope". We have a very good tutorial that helps you understand this:

 

http://docs.coronalabs.com/tutorial/basics/scope/index.html

 

You need to understand the concept of what a code block is. Along with that an understanding of why in general nesting is bad. It really helps to have solid code indention and variable naming.

 

While nesting has some value and you should use it when it makes sense, it generally makes for more complex and harder to manage code. If you're concerned about upValue limits and max # of locals, you can modularize your code and use tables as objects.

 

Rob


  • dodi_games likes this

[TOPIC: post.html]
#9

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

Thanks @Rob

[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,708 posts
  • Corona SDK

As Rob says, upvalue limits need never be an issue, because you could put all your functions in a table, and all your variables in another, and that would only count as two against your limit.
  • dodi_games likes this

[TOPIC: post.html]
#11

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

Thanks for your help, you're right. What I was trying to do was to avoid having to re-write 10 levels of my game but I realize that it needs to be done to avoid problems in the future.


[topic_controls]
[/topic_controls]