0

下面是我的主要文件。基本上发生的事情是,当“矩形”对象不移动时,碰撞正在起作用,但只要我移动矩形对象,碰撞就会停止生效。我认为它知道它正在发生碰撞,但它没有做任何事情(比如与对象的交互)。有没有人有任何想法,为什么会这样?以及如何解决?

local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ; 
physics.setDrawMode( "debug" )
display.setStatusBar( display.HiddenStatusBar )
math.randomseed( os.time() )

physics.setVelocityIterations(10);
physics.setPositionIterations(20);


--set up some references and other variables
local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
local cw, ch = display.contentWidth, display.contentHeight

--set up collision filters
local screenFilter = { categoryBits=2, maskBits=1 }
local objFilter = { categoryBits=1, maskBits=14 }
local fieldFilter = { categoryBits=4, maskBits=1 }
local magnetFilter = { categoryBits=8, maskBits=1 }

--set initial magnet pull
local magnetPull = 0.25

--set up world and background
local screenBounds = display.newRect( -ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy )
screenBounds.name = "screenBounds"
screenBounds.isVisible = false ; physics.addBody( screenBounds, "static", { isSensor=true,     filter=screenFilter } )


local function newPositionVelocity( object )
    local math_random = math.random    
    local side = math_random( 1,4 ) ; local posX ; local posY ; local velX ; local velY

if ( side == 1 or side == 3 ) then
    posX = math_random(0,display.pixelHeight)
    velX = math_random( -10,10 ) * 5
    if ( side == 1 ) then posY = -oy-40 ; velY = math_random( 8,18 ) * 16
    else posY = display.contentHeight+oy+40 ; velY = math_random( 8,16 ) * -16
    end
else
    posY = math_random(0,display.pixelWidth)
    velY = math_random( -10,10 ) * 5
    if ( side == 4 ) then posX = -ox-40 ; velX = math_random( 8,16 ) * 16
    else posX = display.contentWidth+ox+40 ; velX = math_random( 8,16 ) * -16
    end
end
object.x = posX ; object.y = posY
object:setLinearVelocity( velX, velY )
object.angularVelocity = math_random( -3,3 ) * 40
object.alpha = 1

end

local rect = display.newRect(100,100,100,60);
local offset = 250
rect.x = cw/2;
rect.y = ch/2-offset;
--rect.xReference = 40;
rect.yReference = offset;



local function objectCollide( self, event )
local otherName = event.other.name
print(event.other.name);

local function onDelay( event )
    local action = ""
    if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end

    if ( action == "makeJoint" ) then
        self.hasJoint = true
        self.touchJoint = physics.newJoint( "touch", self, self.x, self.y )
        self.touchJoint.frequency = magnetPull
        self.touchJoint.dampingRatio = 0.0
        self.touchJoint:setTarget( 512, 384 )
    elseif ( action == "leftField" ) then
        self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil
    else
        if ( self.hasJoint == true ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end
        newPositionVelocity( self )
    end
end

if ( event.phase == "ended" and otherName == "screenBounds" ) then
    local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftScreen"
elseif ( event.phase == "began" and otherName == "magnet" ) then
    transition.to( self, { time=400, alpha=0, onComplete=onDelay } )
elseif ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then
    local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint"
elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then
    local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField"
end


end



local function setupWorld()

for i=1, 20 do
    local obj = display.newCircle(0,0, 12 )
    physics.addBody( obj, "dynamic", { bounce=.3, radius=12, density = .2 --[[,filter=objFilter]] } )
    obj. isBullet = true;
            newPositionVelocity( obj )
    obj.hasJoint = false
    obj.collision = objectCollide ; obj:addEventListener( "collision", obj )
end

local field = display.newCircle(cw/2, ch/2, 320); 
    field.alpha = 0.2;
field.name = "field";
field.x = display.contentCenterX ; field.y = display.contentCenterY;
physics.addBody( field, "static", { isSensor=true, radius=320, filter=fieldFilter });

magnet = display.newCircle(cw/2, ch/2, 40 )
magnet.name = "magnet"
magnet.x = display.contentCenterX ; magnet.y = display.contentCenterY
physics.addBody( magnet, "static", { bounce=0, radius=40, filter=magnetFilter } )

  end

 setupWorld()
 physics.addBody(rect,"kinematic", { bounce = 1, density = 1})


 deg = 0.0;
 prevY = 0;
 local function onTouch(event)
 if event.phase == "began" then
  prevY = event.y
end
if (deg<=360) then
  deg=0;
end

if(event.y >= prevY) then
   deg = deg + 10;
elseif(event.y <= prevY) then
   deg = deg -10;
end  
prevY = event.y

rect:rotate(deg);

end


Runtime:addEventListener("touch", onTouch);
4

2 回答 2

0

听起来你的身体要睡觉了。

http://developer.coronalabs.com/content/game-edition-physics-bodies#body.isSleepingAllowed

尝试:

rect.isSleepingAllowed = false

于 2013-05-27T23:34:16.220 回答
0

运动物体不会产生碰撞。它需要是动态的。

于 2013-05-05T19:56:22.147 回答