Mr. E
Mr. E

Reputation: 75

How to stop a function in another function in corona sdk

I have two functions:

local function first()
    transition.to(ball, {x=100, y=200, time = 200}     
end





local function stop()
if(score == 0)then 

  --stop the first function

end
end

How can I stop the first function in another function?

Upvotes: 0

Views: 392

Answers (1)

Mike Corcoran
Mike Corcoran

Reputation: 14565

the transition function returns a reference to the transition, that you can later pass to transition.cancel to cancel the transition.

local currentTransition = nil
local function first()
    currentTransition = transition.to(ball, {x=100, y=200, time = 200}     
end

local function stop()
    if (score == 0 and currentTransition ~= nil) then 
        transition.cancel(currentTransition)
    end
end

more details are here

edit -

to handle this manually in functions you implement, you would need to have any function that supports some type of cancellation check for some flag or state to determine whether or not to continue operating. this is how multi-threaded apps support cancellation now, you create a cancellation token up front and pass it around to anything doing long/intensive work and that code occasionally checks the flag and stops if a cancellation has occurred. since pure lua doesn't support multi-threading, here is a basic and contrived example:

local token = { cancelled = false }    

local function bar(cancellationToken)
    print("Hi, from bar!")
    -- simulate user cancellation
    cancellationToken.cancelled = true
end

local function foo(cancellationToken)
    for i=0, 10 do
        if (cancellationToken.cancelled) then
            print("Cancelling operation...")
            return
        end
        print(i)
        bar(cancellationToken)
    end
end

foo(token)

Upvotes: 2

Related Questions