Corona SDK中的实时物理body缩放问题



我的目标是创建一个平台,从一个x,y坐标到触摸事件,通过这种方式,滑动手指可以实时移动和调整平台大小。然而,问题是,如果在所述平台上有一个球,然后我移动平台,球会冻结并停止像球一样的动作,直到平台被操纵完成。

有没有一种方法可以让球继续正常运行,即使它们所在的平台正在移动?

我已经包含了代码,所以你可以玩它,看看我的意思。看到我所说的最好的方法是点击创建一个平台,等待球落在上面,然后拖动平台的末端。

        local physics = require("physics");
physics.start();
physics.setDrawMode("hybrid");
W = display.contentWidth;
H = display.contentHeight;
balls = {};
local function createPlatform(event)
    if (event.phase == "began") then
        if(line) then
            line.parent:remove(line);
        end
        x = (event.x - (W/2 - 80));
        y = (event.y - (H/2));
        line = display.newLine(W/2 - 80, H/2, event.x, event.y)
        line.width = 5;
        physics.addBody(line, "static", {shape = {0, 0, x, y}});
        line.isBullet = true;
    end
    if (event.phase == "moved") then
        x = (event.x - (W/2 - 80));
        y = (event.y - (H/2));
        if (line) then
            line.parent:remove(line);
        end
        line = display.newLine(W/2 - 80, H/2, event.x, event.y)
        line.width = 5;
        physics.addBody(line, "static", {shape = {0, 0, x, y}});
        line.isBullet = true;
    end
    if (event.phase == "ended") then
    end
end
local function spawnBalls(event)
    i = 1;
    local function spawnb(event)
        rand = math.random(1, 4);
            if (rand == 1) then
                balls[i] = display.newCircle(math.random(140, 180), -30, 7.5);
                balls[i]:setFillColor(255,0,0);
                physics.addBody(balls[i], {radius = 7.5})
                balls[i].isBullet = true;
                i = i + 1;
                rand = math.random(1,4)
            elseif (rand == 2) then
                balls[i] = display.newCircle(math.random(140, 180), -30, 7.5);
                balls[i]:setFillColor(0, 255, 0);
                physics.addBody(balls[i], {radius = 7.5})
                balls[i].isBullet = true;
                i = i + 1;
                rand = math.random(1,4)
            elseif (rand == 3) then
                balls[i] = display.newCircle(math.random(140, 180), -30, 7.5);
                balls[i]:setFillColor(0, 0, 255)
                physics.addBody(balls[i], {radius = 7.5})
                balls[i].isBullet = true;
                i = i + 1;
                rand = math.random(1,4)
            elseif (rand == 4) then
                balls[i] = display.newCircle(math.random(140, 180), -30, 7.5);
                balls[i]:setFillColor(255, 255, 0);
                physics.addBody(balls[i], {radius = 7.5})
                balls[i].isBullet = true;
                i = i + 1;
                rand = math.random(1,4)
            end
        end
    timer.performWithDelay(1500, spawnb, -1);
end
spawnBalls();
Runtime:addEventListener("touch", createPlatform)

我认为发生这种情况是因为在Box2D有机会计算实体的新位置/速度(在屏幕上移动手指、鼠标时)之前删除了实体(线)。。。你可以尝试增加位置迭代和帧速率。。。但由于性能损失,因此不建议这样做
我建议在不去除身体的情况下旋转并去皮,然后添加一个新的。您可以使用.derotation和.xScale/.yScale.

我还没试过,但我会这样做的。我对科罗纳也有点陌生。。。所以没有保证。

最新更新