1

我遇到的问题很简单。我在屏幕中央有一个对象,然后是一个围绕该对象运行的圆圈。我想要做的是让对象围绕对象运行,当用户触摸屏幕时,它将围绕对象运行并停止面向用户点击屏幕的位置。

我在尝试完成这项工作时遇到了巨大的困难。但我想我会做的是尝试从物体的中心画一条线到使用点击的地方,然后围绕地球运行的球撞到它将停止的线(很简单)。我遇到的问题是线对象(在混合模式下查看时)更像是一个矩形,然后它是一条线(在物理引擎中,所以这与我的预期结果相混淆。有没有人知道我做错了什么在这里?或者电晕是如何处理这个的?以及如何解决它?或者任何其他解决方案也可以很好地工作。

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


--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.85

--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

require( "repeller");
local rect = createRepeller(15, "rect",cw/2, cw/2);

local function objectCollide( self, event )
local otherName = 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 == "rect" ) 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, 0 do
    local obj = display.newCircle(0,0, 12 )
    physics.addBody( obj, "dynamic", { bounce=.3, radius=12, density = 0.0 --[[,filter=objFilter]] } )
    obj. isBullet = true;
            newPositionVelocity( obj )
    obj.hasJoint = false;
            obj.name = "ball";
    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()

deg = 0.0;
local prevPosY = 0
local line = display.newLine(cw/2,ch/2,cw/2,ch/2);


local function onTouch(event)
   if line ~= nil then
      line:removeSelf();
   end

   if event.phase == "began" then
      prevPosY = event.y
   end

   if(event.y >= prevPosY) then
      myJoint.motorSpeed = -(1.5 * prevPosY - event.y);
   elseif(event.y <= prevPosY) then
      myJoint.motorSpeed =   1.5 * prevPosY - event.y;
   end  

   if(prevPosY - event.y == 0) then
      --myJoint.motorSpeed = 0;
   end
   prevPosY = event.y;
   line = display.newLine(cw/2,ch/2,event.x, event.y);
   line.width= 1;
   line.name = "line";
   physics.addBody(line, { isSensor = true })
   local function lineCol(self, event)
     if (event.other.name=="rect") then
        myJoint.motorSpeed = 0;
        print(event.other.name, event.name);
        --rect:removeSelf();
     end
   end
   line.collision = lineCol;
   line:addEventListener("collision", line); 
end
   line = display.newLine(cw/2,ch/2, 200 , 250);
   line.width= 1;
   line.name = "line";
   physics.addBody(line, { isSensor = true, shape=line })
--Runtime:addEventListener( "collision", lineCol)
physics.addBody(rect,"dynamic", {bounce = 0, density = 0})

myJoint = physics.newJoint( "pivot", rect, magnet, cw/2, ch/2);
myJoint.isMotorEnabled = true;
myJoint.motorSpeed = 0;
myJoint.maxMotorTorque = 100;

Runtime:addEventListener("touch", onTouch);

和我的“repeller.lua”

function createRepeller(size, name, locX, locY)
   local rep = display.newCircle(locX,locY, size);
   rep.name = name;
   return rep;
end 
4

1 回答 1

0

在 Corona 中,线条并不真正适用于物理实体……例如,您可以应用一个,但它可能不是您期望的形状。我的建议是创建一个非常薄的矩形(一个 newRect())对象,然后将该线旋转/定位到您需要的位置。或者,像其他用户建议的那样使用数学公式。

祝你好运,布伦特

于 2013-05-27T03:13:15.057 回答