【问题标题】:Generating physics object at the same point of another object and move generated object without colliding with the second in corona sdk在另一个对象的同一点生成物理对象并移动生成的对象而不会与 corona sdk 中的第二个发生碰撞
【发布时间】:2013-08-04 20:01:38
【问题描述】:

我想在同一点生成两个物理对象,然后通过施加力移动一个物理对象,但是当一个对象移动时,它会与另一个物理对象发生碰撞。我不能将另一个对象设为非物理对象,因为这些对象会进一步相互碰撞,但最初它们不能碰撞。我所做的一些编码部分如下......请提出任何想法..谢谢

local obj2
local physicsbody1= { density=0.1, friction=0, bounce=0 }
local physicsbody2= {density=10.0, friction=0, bounce=0.1, radius=25 }

local function createobj2(_xpos,y_pos,_id)
obj2=display.newImage("obj2.png")
obj2.x=_xpos
obj2.y=_ypos
physics.addBody(obj2,physicsbody1)
end


local physicsObjStatic=display.newImage("img1.png")
physicsObjStatic.x=450
physicsObjStatic.y=200
physics.addBody(physicsObjStatic,physicsbody2)


    local function Shot( event )
        local t = event.target

        local phase = event.phase
        if "began" == phase then
            display.getCurrentStage():setFocus( t )
            t.isFocus = true
            t:setLinearVelocity( 0, 0 )
            t.angularVelocity = 0

            target.x = t.x
            target.y = t.y

            startRotation = function()
            target.rotation = target.rotation + 4
            end

            Runtime:addEventListener( "enterFrame", startRotation )

        local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
            myLine = nil

        elseif t.isFocus then
            if "moved" == phase then
                 if ( myLine ) then
                    myLine.parent:remove( myLine ) 
                end
                myLine = display.newLine( t.x,t.y, event.x,event.y )
                myLine:setColor( 255, 255, 255, 50 )
                myLine.width = 8

            elseif "ended" == phase or "cancelled" == phase then

                display.getCurrentStage():setFocus( nil )
                t.isFocus = false

                local stopRotation = function()
                    Runtime:removeEventListener( "enterFrame", startRotation )
                end

                local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )

                if ( myLine ) then
                    myLine.parent:remove( myLine )
                end
createobj2(t.x,t.y)
obj2:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
            end
        end
        return true
    end
physicsObjStatic:addEventListener( "touch", Shot )

【问题讨论】:

  • “请给我解决方案”不太可能奏效。向我们展示您尝试了什么(但只包括足以让我们运行代码的相关片段)并描述哪些不起作用。这也允许那些拥有 Corona 的人运行您的片段(如果他们愿意)来测试您所描述的内容。

标签: android iphone lua coronasdk


【解决方案1】:

您应该在拖动对象时将其临时更改为运动学而不是动态的。运动学对象不会产生碰撞。

【讨论】:

  • 有没有其他方法可以解决这个问题,因为这个解决方案不适合我的游戏情况。实际上,这两个对象都是从同一点生成的,如果我移动一个对象,它会与另一个对象发生碰撞,我不想从对象中删除物理或将其更改为运动学...感谢您的帮助
  • 您可以查看碰撞过滤器。有几个论坛帖子和社区创建的使用它们的教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多