Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 如何让一个物理体否定另一个物理体的碰撞。_Function_Lua_Collision Detection_Coronasdk_Physics - Fatal编程技术网

Function 如何让一个物理体否定另一个物理体的碰撞。

Function 如何让一个物理体否定另一个物理体的碰撞。,function,lua,collision-detection,coronasdk,physics,Function,Lua,Collision Detection,Coronasdk,Physics,我在屏幕上有两个重叠的图像,称为A和B。我有第三个图像,称为C,用户可以在屏幕上移动。两个中较大的一个(A)会在碰撞时将C重置为繁殖点。我需要知道如何使它,使C可以走过图像B,而不是重置为繁殖点。如图B所示,与图A重叠并抵消碰撞函数 代码如下: --Hides status bar from top of page display.setStatusBar( display.HiddenStatusBar ) --Set a test background image local backg

我在屏幕上有两个重叠的图像,称为A和B。我有第三个图像,称为C,用户可以在屏幕上移动。两个中较大的一个(A)会在碰撞时将C重置为繁殖点。我需要知道如何使它,使C可以走过图像B,而不是重置为繁殖点。如图B所示,与图A重叠并抵消碰撞函数

代码如下:

--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)
--从页面顶部隐藏状态栏
display.setStatusBar(display.HiddenStatusBar)
--设置测试背景图像
localbackgroundimg=display.newImageRect(“testbackground.png”,320576)
backgroundimg.x=display.contentWidth*0.5
backgroundimg.y=display.contentHeight*0.5
--设置分数和寿命的位置和数量
本地分数=0
当地生活=5
本地showscore=display.newText(“分数:…分数,0,-36,本机.systemFont,25)
showscore:setTextColor(0,0,0)
本地showlifes=display.newText(“生活:…生活,230,-36,原生.systemFont,25)
ShowLifes:setTextColor(0,0,0)
--为碰撞等设置物理。
物理=要求(“物理”)
物理开始
物理学.重力(0,0)
--落水
本地水=display.newImageRect(“water.png”,320192)
water.x=display.contentWidth*0.5
水。y=144
物理学。addBody(水,“静态”)
水:addEventListener(“碰撞”,函数()计时器。性能带延迟(50,水彩画)结束)
功能水彩(活动)
生命=生命-1
显示。移除(青蛙)
frog=display.newImageRect(“FrogTest.png”,32,48)
frog.x=display.contentWidth*0.5
青蛙y=504
物理学。addBody(青蛙,“动态”)
frog.isFixedRotation=真
结束
--设置按钮、图像和位置
本地转发=display.newImageRect(“forward Button.png”,106100)
正向x=160
向前。y=478
local left=display.newImageRect(“left Button.png”,106100)
左x=53
左。y=478
local right=display.newImageRect(“right Button.png”,106100)
对。x=267
对,y=478
--设置日志位置和移动
本地log1=display.newImageRect(“log1.png”,96,48)
log1.x=32
log1.y=226
物理学。addBody(log1,“运动学”)
转换到(log1,{time=3500,x=288})
--在屏幕上设置青蛙精灵
frog=display.newImageRect(“FrogTest.png”,32,48)
frog.x=display.contentWidth*0.5
青蛙y=504
addBody(青蛙,动态,{密度=1.0,摩擦=1,反弹=1})
frog.isFixedRotation=真
--设置运动变量
局部运动X=0
局部运动Y=0
本地速度=4
--前进
功能前进:触摸()
motionX=0
运动=-速度
结束
转发:添加EventListener(“触摸”,转发)
--向右移动
功能权限:touch()
运动X=速度
运动=0
结束
右:addEventListener(“触摸”,右)
--向左移动
功能左:触摸()
motionX=-速度
运动=0
结束
左:addEventListener(“触摸”,左)
--每次调用时间帧时移动青蛙
功能(事件)
青蛙x=青蛙x+运动x
青蛙.y=青蛙.y+运动
结束
运行时:addEventListener(“enterFrame”,movefrog)
--阻止青蛙继续移动
本地功能停止(事件)
如果event.phase==“结束”,则
motionX=0
运动=0
结束
结束
运行时:addEventListener(“触摸”,停止)
--确保青蛙不会离开屏幕
本地功能stopfrog(事件)
如果青蛙x=304,则
青蛙x=304
结束
结束
运行时:addEventListener(“enterFrame”,stopfrog)

您需要在
水彩画
功能下设置一个条件,以否定重生

根据需要的复杂程度,您可以简单地检查
frog
的位置是否在
log1
的边界内,或者,您可能有
log1
,任何未来的日志都有自己的冲突事件,该事件在冲突时设置标志,以不触发重新启动,然后在与任何日志的冲突结束时清除该标志

下面是一个例子:

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)
local onLog=0
函数frogDie()
生命=生命-1
显示。移除(青蛙)
frog=display.newImageRect(“FrogTest.png”,32,48)
frog.x=display.contentWidth*0.5
青蛙y=504
物理学。addBody(青蛙,“动态”)
frog.isFixedRotation=真
结束
功能水彩(活动)
如果onLog<1,则frogDie()结束
结束
函数logCollide(事件)
如果event.phase==“开始”,则
onLog=onLog+1
其他的
onLog=onLog-1
结束
结束
log1:addEventListener(“碰撞”,logCollide)
--log2:addEventListener(“碰撞”,logCollide)

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

您需要在
waterCollide
函数下设置一个条件,以否定重生

根据需要的复杂程度,您可以简单地检查
frog
的位置是否在
log1
的边界内,或者,您可能有
log1
,任何未来的日志都有自己的冲突事件,该事件在冲突时设置标志,以不触发重新启动,然后在与任何日志的冲突结束时清除该标志

下面是一个例子:

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)
local onLog=0
函数frogDie()
生命=生命-1
显示。移除(青蛙)
frog=display.newImageRect(“FrogTest.png”,32,48)
frog.x=display.contentWidth*0.5
青蛙y=504
物理学。addBody(青蛙,“动态”)
frog.isFixedRotation=真
结束
功能水彩(活动)
如果onLog<1,则frogDie()结束
结束
函数logCollide(事件)
如果event.phase==“开始”,则
onLog=onLog+1
其他的
onLog=onLog-1
结束
结束
log1:addEventListener(“碰撞”,logCollide)
--log2:addEventListener(“碰撞”,logCollide)
使用一个数字来跟踪青蛙是否在日志上应该比布尔值更安全,因为日志可能会重叠并清除标志,然后才能在多次冲突中正确重置