在Corona SDK中,生成一个物理对象,并将其放置在另一个对象相同的位置,移动生成的对象时要避免与第二个对象相撞。

我想在同一点上生成两个物理对象,然后通过施加力来移动一个物理对象,但是一个对象移动时会碰撞到另一个物理对象。我不能将另一个对象设置为非物理对象,因为后面这些对象将相互碰撞,但最初它们必须不碰撞。我已经完成的一些编码部分如下...请提出任何想法...谢谢

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 )
点赞
用户1870706
用户1870706

你应该在拖动物体时将它暂时改为运动学类型(kinematic)而不是动态类型(dynamic)。运动学物体不会产生碰撞。

2013-08-04 18:31:12