1
votes

I'm trying to move an object randomly to different locations, so I came out with the following: transition.to generates the x,y randomly as well as the time, and on finish runs another function which checks if the object is still there and sends it to a different location.

but I'm getting an error:

Runtime error
main.lua:352: stack overflow
stack traceback:
  main.lua:352: in function
 'toAnotherPlace'

looks like corona doesn't really waits for transition complete, so it goes on infinite loop

code

function toAnotherPlace(object) 
    if object ~= nil then
        transition.to( object,
            {
                time=math.random(1500,6000),
                alpha=1,
                x=(math.random(10, 310)),
                y=(math.random(10, 400)),
                onComplete=toAnotherPlace(object)
            })
    end
end

transition.to( bossess[boss],
    {
        time=math.random(1500,6000),
        alpha=1,
        x=(math.random(10, 310)),
        y=(math.random(10, 400)),
        onComplete=toAnotherPlace(bossess[boss])
    })
1

1 Answers

1
votes

You can try this, I added an onComplete = function() ... end and I call the toAnotherPlace(object) function inside it.

I think it's a bug if you directly call a function on onComplete

function toAnotherPlace(object)
    print(object.width)
    if object ~= nil then
        transition.to( object,
        {
            time = math.random(1500,6000),
            alpha = 1,
            x = math.random(10, 310),
            y = math.random(10, 400),
            onComplete = function()
                toAnotherPlace(object)
            end
        })
    end
end

transition.to(bossess[boss],
{
    time = math.random(1500,6000),
    alpha = 1,
    x = math.random(10, 310),
    y = math.random(10, 400),
    onComplete = function()
        toAnotherPlace(bossess[boss])
    end
})

I tried this and is working fine, no errors.

If you still getting errors, check the bossess[boss] if there is a reference to your object