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

我的目标是创建一个平台,从一个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)

原文链接 https://stackoverflow.com/questions/9263295

点赞
stackoverflow用户650052
stackoverflow用户650052

我认为这是因为您在Box2D计算新的物体位置/速度之前删除了身体(线)(当您在屏幕上移动您的手指、鼠标时)……您可以尝试增加位置迭代和帧速率……但由于性能损失,这并不是真正建议的。

我建议您在不删除它并添加新物体的情况下旋转和缩放身体。您可以使用.rotation和.xScale/.yScale。

我没有尝试过,但这就是我会做的。 我也是Corona的新手……所以不能保证。

2012-02-13 16:22:43