我如何让一个物理机构否定另一个物体的碰撞

我如何让一个物理机构否定另一个物体的碰撞

本文介绍了我如何让一个物理机构否定另一个物体的碰撞。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕上有两个图像重叠称为A和B.我有第三个图像称为C,用户可以在屏幕上移动。两者中较大的一个(A)将C重置为碰撞后的重生点。我需要知道如何让C可以遍历图像B,而不是重置到重生点。如在图像B中重叠图像A并且否定碰撞功能。

i have two images on the screen that overlap called A and B. i have a third image called C that the user is able to move around the screen. the bigger one (A) of the two resets the C to the spawn point on collide. i need to know how to make it so that C can walk over image B and not be reset to the spawn point. as in image B overlaps image A and negates the collision function.

这里是代码:

here is the code:

--Hides status bar from top of page
display.setStatusBar( display.HiddenStatusBar )

--Set a test background image
local backgroundimg = display.newImageRect("testbackground.png", 320,576)
backgroundimg.x = display.contentWidth*0.5
backgroundimg.y = display.contentHeight*0.5

--Set the position and amount of score and lives

local score = 0
local lives = 5

local showscore = display.newText("Score: "..score,0,-36,native.systemFont,25)
showscore:setTextColor(0, 0, 0)

local showlives = display.newText("Lives: "..lives,230,-36,native.systemFont,25)
showlives:setTextColor(0, 0, 0)

--Set physics for collisions, etc.
physics = require("physics")
physics.start()
physics.setGravity(0,0)

--set water
local water = display.newImageRect("water.png",320,192)
water.x = display.contentWidth*0.5
water.y = 144
physics.addBody(water,"static")
water:addEventListener("collision", function()timer.performWithDelay(50,waterCollide)end)

function waterCollide(event)
    lives = lives - 1
    display.remove(frog)
    frog = display.newImageRect("FrogTest.png",32,48)
    frog.x = display.contentWidth*0.5
    frog.y = 504
    physics.addBody(frog, "dynamic")
    frog.isFixedRotation = true
end

--Sets buttons images and positions
local forward = display.newImageRect("Forward Button.png",106,100)
forward.x = 160
forward.y = 478

local left = display.newImageRect("Left Button.png",106,100)
left.x = 53
left.y = 478

local right = display.newImageRect("Right Button.png",106,100)
right.x = 267
right.y = 478

--Set log position and movement
local log1 = display.newImageRect("log1.png", 96, 48)
log1.x = 32
log1.y = 226
physics.addBody(log1,"kinematic")
transition.to(log1, {time = 3500, x = 288})


--Set a frog sprite on the screen
frog = display.newImageRect("FrogTest.png",32,48)
frog.x = display.contentWidth*0.5
frog.y = 504
physics.addBody(frog, "dynamic",{density = 1.0, friction = 1, bounce = -1})
frog.isFixedRotation = true

--Sets motion variables
local motionX = 0
local motionY = 0
local speed = 4

--Moving forward
function forward:touch()
    motionX = 0
    motionY = -speed
end
forward:addEventListener("touch",forward)

--Moving Right
function right:touch()
    motionX = speed
    motionY = 0
end
right:addEventListener("touch",right)

--Moving left
function left:touch()
    motionX = -speed
    motionY = 0
end
left:addEventListener("touch",left)

--Moves Frog each time frame is called
function movefrog (event)
    frog.x = frog.x + motionX
    frog.y = frog.y + motionY
end
Runtime:addEventListener("enterFrame", movefrog)

--Stops frog from moving continuously
local function stop (event)
    if event.phase == "ended" then
        motionX = 0
        motionY = 0
    end
end
Runtime:addEventListener("touch", stop)

--Making sure the frog does not go off the screen
local function stopfrog (event)
    if frog.x <= 16 then
        frog.x = 16
    end
    if frog.x >= 304 then
        frog.x = 304
    end
end
Runtime:addEventListener("enterFrame", stopfrog)


推荐答案

您需要在 waterCollide 函数下设置一个条件,以取消重生。

You need to set a condition under the waterCollide function in order to negate the respawning.

取决于你需要的复杂程度有多复杂,你可以简单地检查 frog 的位置是否在 log1 的范围内边界,或,或许你可以让 log1 和任何未来的日志它们自己的碰撞事件设置标志 strong> on collision to not trigger the respawn then clear the flag with collision with log to end ends。

Depending on how complex you'll need it to be, you could simply check if frog's position is within log1's boundaries, or perhaps you could have log1 and any future logs have their own collision event which sets a flag on collision to not trigger the respawn then clear the flag when collision with any log ends.

下面是一个例子:

Here's an example:

local onLog = 0

function frogDie()
    lives = lives - 1
    display.remove(frog)
    frog = display.newImageRect("FrogTest.png",32,48)
    frog.x = display.contentWidth*0.5
    frog.y = 504
    physics.addBody(frog, "dynamic")
    frog.isFixedRotation = true
end

function waterCollide(event)
    if onLog < 1 then frogDie() end
end

function logCollide(event)
    if event.phase == 'began' then
        onLog = onLog + 1
    else
        onLog = onLog - 1
    end
end

log1:addEventListener("collision", logCollide)
--log2:addEventListener("collision", logCollide)

使用数字来跟踪青蛙是否在日志中应该比安全一个布尔值,因为在多次碰撞过程中,日志可能最终重叠并清除标志,然后才能正确重置。

Using a number to track whether the frog is on a log should be safer than a boolean, since logs might end up overlapping and clearing the flag before it can be reset properly on multiple collision passes.

这篇关于我如何让一个物理机构否定另一个物体的碰撞。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:01