【问题标题】:have one image negate the collision detection of another image让一张图像否定另一张图像的碰撞检测
【发布时间】:2013-12-08 07:27:25
【问题描述】:

我有一个水图像,它在碰撞时杀死用户精灵,从生命变量中取 1 并重新生成精灵到开始。我有另一个木筏的图像,它不断产生在屏幕上移动的新图像。我试图得到它,以便用户可以移动到木筏上,即在水上图像上方,而不是重新生成回到开始。

--sets the function for the death sequence.
function waterCollide(event)
    frog.x = display.contentWidth/2
    frog.y = 504
    isOnRaft = 0

    lives = lives - 1
    showlives.text = "Lives: "..lives,230,-36,native.systemFont,25

    lose()
end

--sets water
water = display.newRect(0,0,320,150)
water.x = display.contentWidth*0.5
water.y = 144
water.alpha = 0
physics.addBody(water,"static", {isSensor = true})
water:addEventListener("collision", function ()timer.performWithDelay(50,waterCollide)end)

--Set log position and movement
local mRandom = math.random
local raft = {"Raft1" ,"Raft2"}
local objectTag = 0
local object = {}

function spawnlogleft()
    objectTag = objectTag + 1
    local objIdx = mRandom(#raft)
    local objName = raft[objIdx]
    object[objectTag]  = display.newImage(objName..".png")
    object[objectTag].x = -96
    object[objectTag].y = 216
    object[objectTag].name = objectTag
    transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
    physics.addBody( object[objectTag], "static",{isSensor = true})
end
spawnlogleft()
timer.performWithDelay(3500,spawnlogleft,0)

--user sprite details
frog = display.newSprite(frogSheet, sequenceData)
frog.x = display.contentWidth/2
frog.y = 504
physics.addBody( frog, "dynamic", physicsData:get("FrogSheetData"))
frog.isFixedRotation = true

用户精灵是一个三帧动画,时间为 300。任何帮助将不胜感激。如果需要,我会提供更多详细信息。

谢谢你

【问题讨论】:

  • 这和你问的other question有什么不同?
  • 因为我在没有动画的情况下工作,而当我只有固定的日志时。如果我出于某种原因在筏上使用 isOnRaft = 1,则它等于 24。

标签: lua collision-detection coronasdk physics


【解决方案1】:

Corona 有这些巧妙的东西,称为 CollisionFilters。它们向您展示了如何在 /Physics/CollisionFilter 中使用它们。

您基本上设置位以使某些对象与其他对象不可碰撞。查看这个 main.lua 示例:

local physics = require("physics")
physics.start()

physics.setScale( 60 )

display.setStatusBar( display.HiddenStatusBar )

local bkg = display.newImage( "stripes.png" )

borderCollisionFilter = { categoryBits = 1, maskBits = 6 } -- collides with (4 & 2) only
borderBodyElement = { friction=0.4, bounce=0.8, filter=borderCollisionFilter }

local borderTop = display.newRect( 0, 0, 320, 1 )
borderTop:setFillColor( 0, 0, 0, 0)     -- make invisible
physics.addBody( borderTop, "static", borderBodyElement )

local borderBottom = display.newRect( 0, 479, 320, 1 )
borderBottom:setFillColor( 0, 0, 0, 0)      -- make invisible
physics.addBody( borderBottom, "static", borderBodyElement )

local borderLeft = display.newRect( 0, 1, 1, 480 )
borderLeft:setFillColor( 0, 0, 0, 0)        -- make invisible
physics.addBody( borderLeft, "static", borderBodyElement )

local borderRight = display.newRect( 319, 1, 1, 480 )
borderRight:setFillColor( 0, 0, 0, 0)       -- make invisible
physics.addBody( borderRight, "static", borderBodyElement )


local red = {}
local blue = {}

local redCollisionFilter = { categoryBits = 2, maskBits = 3 } -- collides with (2 & 1) only
local blueCollisionFilter = { categoryBits = 4, maskBits = 5 } -- collides with (4 & 1) only

local redBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=redCollisionFilter }
local blueBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=blueCollisionFilter }


for i = 1,4 do
    red[i] = display.newImage( "red_balloon.png", (80*i)-60, 50 + math.random(20) )
    physics.addBody( red[i], redBody )
    red[i].isFixedRotation = true

    blue[i] = display.newImage( "blue_balloon.png", (80*i)-60, 250 + math.random(20) )
    physics.addBody( blue[i], blueBody )
    blue[i].isFixedRotation = true
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2020-04-30
    • 2013-09-04
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多