画线和对象的碰撞(Corona SDK) [英] Collision of a drawed line and objects(Corona SDK)

查看:68
本文介绍了画线和对象的碰撞(Corona SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一条可以用手指画的线,并且有一个矩形.我希望我的线在与矩形碰撞时结束绘制.我该怎么做? 对于我的行功能:

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?

推荐答案

我需要查看更多代码,尤其是如何创建行(或者如果经常创建/销毁行)的代码,给出您可能希望的答案.但是,如果执行此操作,则可能会在每次手指移动时绘制/重新绘制线条(不添加物理物体),并根据手指位置手动检查与矩形的交点.即,类似于以下内容:

I'd need to see more of your code, particularly how you're creating the line (or lines if you're creating/destroying them often), to give the answer you're probably hoping for. However, if I were doing this I would probably draw/redraw the line on every finger move (without adding a physics body) and manually check for intersections with the rectangle based on finger position. Ie, something like the following:

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

碰撞的问题是,如果您正在创建和重新创建线条,并且它们确实相交,则由于它们的生命周期短(并且它们实际上并没有移动),您可能不会遇到任何事件.如果您真的想使用碰撞,我会在触摸开始(一个圆圈)时创建一个不可见的代理对象,并在运动中从起点到其绘制一条线.然后,我将在代理对象上使用触摸关节并在其上检测到碰撞.这可能比值得的还要麻烦.

The problem with collisions is that if you're creating and recreating the lines and they do happen to intersect you may not get an event due to their short lifecycle (and the fact that they aren't actually moving). If you REALLY want to use collisions, I would create an invisible proxy object on touch begin (a circle) and draw a line from the start point to it on movements. I'd then use a touch joint on the proxy object and detect collisions on that. That's probably more bother than it's worth.

这篇关于画线和对象的碰撞(Corona SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆