0

For example i have a line that i can draw with a finger and i have a rectangle. i want my line to end drawing up when it collisions with the rectangle. How can i do it? For ex my function of a line:

local line = function()
if(e.phase == "began") then
--code for line
elseif(e.phase == "moved") then
--code for line to draw
elseif(e.phase == "ended") then
--code for line to stop draw
end

i guess that i can do that with collision smith like this

local function onCollision( event )
        if ( event.phase == "began" ) then


                if event.object1.myName == "top" and event.object2.myName == "line" then
                        line("ended")

                end

        end
end

    Runtime:addEventListener("collision", onCollision);

but it doesn't work...any ideas?

4

1 回答 1

0

我需要查看更多您的代码,特别是您如何创建行(或行,如果您经常创建/销毁它们),以给出您可能希望的答案。但是,如果我这样做,我可能会在每个手指移动时绘制/重画线(不添加物理体)并根据手指位置手动检查与矩形的交点。即,类似于以下内容:

local line = function()
    ...
    elseif(e.phase == "moved") then
        local cb = rect.contentBounds
        if event.x > cb.xMin and event.x < cb.xMax and event.y > cb.yMin and event.y < cb.yMax) then
            line("ended")
        end
    else
    ....
end

碰撞的问题在于,如果您正在创建和重新创建线条并且它们恰好相交,则由于它们的生命周期短(以及它们实际上并没有移动的事实),您可能不会收到事件。如果你真的想使用碰撞,我会在触摸开始(一个圆圈)时创建一个不可见的代理对象,并在移动时从起点画一条线到它。然后我会在代理对象上使用触摸关节并检测其上的碰撞。这可能比它的价值更麻烦。

于 2012-08-10T00:21:22.120 回答