如何使用transition.to随机移动一个对象?

我尝试将一个对象随机移动到不同的位置,于是我想到了以下代码:transition.to 将 x、y 和时间都随机生成,并在完成时运行另一个函数,该函数检查对象是否仍然存在并将其发送到另一个位置。

但我遇到了一个错误:

运行时错误
main.lua:352: stack overflow
stack traceback:
  main.lua:352: in function
 'toAnotherPlace'

看来 Corona 实际上并不等待转换完成,因此它会进入无限循环。

代码

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])
    })
点赞
用户1605727
用户1605727

你可以尝试这个方法,我添加了一个 onComplete = function() ... end 并在其中调用了 toAnotherPlace(object) 函数。

我认为如果你直接在 onComplete 中调用一个函数的话可能会出现 bug。

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
})

我已经尝试过了,没有错误。

如果你仍然遇到错误,请检查 bossess[boss] 是否有对你的对象的引用。

2013-07-16 17:17:45