Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Transition & onComplete
Started by ken_shek Apr 05 2019 02:08 AM

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

ken_shek

[GLOBAL: userInfoPane.html]
ken_shek
  • Observer

  • 3 posts
  • Corona SDK

Hi, Shouldn't the code below move the circle back and forward 10 times then stop? All it does is just move it once and finishes. Isn't the function MovingCircle called once the transition completes? It seems to do all 10 in one go.

 

local Switch = 1

local Stop = 0

local myCircle = display.newCircle (300,300,200)

 

local function MovingCircle( Switch, Stop )

 Switch = Switch * -1

 Stop = Stop + 1

 if (Stop > 10) then

  transition.cancel()

 else

  print (Stop .." " .. Switch)

  transition.to( myCircle, { time=3000, x=300*Switch ,y=300*Switch, delta=true, onComplete = MovingCircle(Switch, Stop)} )

 end

end

 

transition.to( myCircle, { time=3000, x=300 ,y=300, delta=true, onComplete = MovingCircle(Switch, Stop)} )

 

Thanks Ken



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,779 posts
  • Enterprise

You're running into one of the classical points of confusion about using Corona and Lua.

 

onComplete parameters expect an address to a function, not the results of the function.

 

So let's looks at a very basic example:

local function MovingCircle( Switch, Stop )
    -- some code
    return someValue
end

creates a block of runnable code at some location in memory and assigns the memory address of that function to the variable MovingCircle.

 

This is key because you have to thing of names as either holding values or addresses to complex things like tables, functions, etc.

 

As your programming, you might want to execute the function to do the work or you might want to get that memory address so the function can be executed at some point in the future or by some other function. With this is mind:

onComplete = MovingCircle( Status, Stop )

executes the function MovingCircle immediately and returns the return value. Since you don't return anything, it returns nil. This means your onComplete is really:

 

MovingCircle runs immediately

onComplete = nil

transition.to has no work to do since onComplete is nil

 

To make matters worse, if your function actually returned something like say the number 10, onComplete would be 10, which is an invalid memory address and onComplete will try to run the code at memory location 10, which it can't and it will create a segment violation which can cause a crash.

 

If you want the address of the function, you cannot put () after the name. The () tells Lua to run the function. Without the () you get the address, so setting this:

onComplete = MovingCircle

 -- no () assigns the memory address of MovingCircle to onComplete and now when the transition.to completes there is a valid function value for onComplete so at that time, MovingCircle will be called.

 

You cannot pass parameters without the () so you now need to learn about anonymous functions or closures to allow you to pass parameters to the function that onComplete will call.

onComplete = function()
          MovingCircle( Switch, Stop )
end

This little trick creates an unnamed function, but returns an address to a function since onComplete wants an address. That nameless function then gets called at the end of the transition and will calls MovingCircle with whatever values Switch and Stop have in them at the moment. 

 

Rob


  • edualc likes this


[topic_controls]
[/topic_controls]