Lua 为什么在没有与其他物体接触的情况下发生碰撞?(作曲家)
这是我的密码。我正在做一个三车道的赛车游戏。这是屏幕截图 当红色汽车与任何其他汽车相撞时,我将进入restart.lua文件,点击屏幕,我将返回game.lua文件(代码如下所示)。当控制从restart.lua切换回game.lua时,就会出现问题 发生的情况是,碰撞事件无缘无故地发生,导致游戏不断出现故障,并在重新启动和game.lua之间切换。有人知道为什么会发生这种情况,以及如何阻止这种情况吗Lua 为什么在没有与其他物体接触的情况下发生碰撞?(作曲家),lua,coronasdk,corona-storyboard,Lua,Coronasdk,Corona Storyboard,这是我的密码。我正在做一个三车道的赛车游戏。这是屏幕截图 当红色汽车与任何其他汽车相撞时,我将进入restart.lua文件,点击屏幕,我将返回game.lua文件(代码如下所示)。当控制从restart.lua切换回game.lua时,就会出现问题 发生的情况是,碰撞事件无缘无故地发生,导致游戏不断出现故障,并在重新启动和game.lua之间切换。有人知道为什么会发生这种情况,以及如何阻止这种情况吗 local composer = require( "composer" ) l
local composer = require( "composer" )
local scene = composer.newScene()
local pix=0
local no=0
local physics=require "physics"
physics.start()
local x=2000
local prevP=0
local prevS=5
local count=1
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
-- Local forward references should go here
-- -------------------------------------------------------------------------------
local opp1
local opp
local sceneGroup
local roadCopy
local road
local car
function removeAllListeners(self)
self._functionListeners = nil
self._tableListeners = nil
end
local function touchScreen(event)
print("touchScreen")
if event.x < dw/2 then
if car.current==car.posB then
car.x=car.posA
car.current=car.posA
elseif car.current == car.posC then
car.x=car.posB
car.current=car.posB
end
else
if car.current==car.posB then
car.x=car.posC
car.current=car.posC
elseif car.current == car.posA then
car.x=car.posB
car.current=car.posB
end
end
end
local function removeListeners()
removeAllListeners(road)
removeAllListeners(roadCopy)
removeAllListeners(opp)
removeAllListeners(car)
road:removeEventListener("tap",touchScreen)
roadCopy:removeEventListener("tap",touchScreen)
car:removeEventListener( "collision",onLocalCollision)
composer.removeScene( "game")
composer.gotoScene( "restart")
end
function onLocalCollision( event )
print( "collision" )
removeListeners()
end
local function moveRoad(self,event)
if self.y > dh-20 then
self.y=20
pix=pix+10
if pix > 100 then
road.speed=road.speed+1
roadCopy.speed=roadCopy.speed+1
pix=0
else
end
else
self.y=self.y+self.speed
pix=pix+1
if pix > 100 then
road.speed=road.speed+1
roadCopy.speed=roadCopy.speed+1
pix=0
else
end
end
if road.speed>30 or roadCopy.speed>30 then
road.speed=30
roadCopy.speed=30
end
end
local function moveoppcar(self, event)
--to move the incoming cars
if(self.y==nil)then
return
end
if(self.y>dh) then
Runtime:removeEventListener( "enterFrame", self )
self:removeSelf()
self=nil
randomobject1()
else
self.y=self.y+self.speed+roadCopy.speed
end
end
function randomobject1(event)
--to randomly generate the incoming cars
math.randomseed( os.time() )
opp=display.newRect( center-dw/4.75, 30, dw/8, dw/4.5 )
local position = math.random(1, 3)
local cartype = math.random(1, 3)
if(position==prevP) then
position=(prevP+2)%3
end
if position==1 then
opp.x=center-dw/4.75
end
if position == 2 then
opp.x=center
end
if position == 3 then
opp.x=center+dw/4.75
end
if cartype == 1 then
opp.fill={type="image", filename="car_green_1.png"}
end
if cartype == 2 then
opp.fill={type="image", filename="car_blue_1.png"}
end
if cartype == 3 then
opp.fill={type="image", filename="car_red_1.png"}
end
opp.speed=math.random(1,10)
sceneGroup:insert(opp)
prevP=position
prevS=opp.speed
local r=opp.width
physics.addBody(opp,"static",{density=1, bounce=0.1, friction=0.2,radius=5})
opp.gravityScale=0
opp.enterFrame=moveoppcar
Runtime:addEventListener("enterFrame", opp)
end
-- "scene:create()"
function scene:create( event )
print("Scene:create")
sceneGroup = self.view
---------ROAD---------
road=display.newRect( 0, 0, dw, dh )
road.fill={type="image",filename="road.png"}
road.anchorX,road.anchorY=0,0
road.contentHeight=dh
sceneGroup:insert(road)
road.speed=5
roadCopy=display.newRect( 0, 0, dw, dh )
roadCopy.fill={type="image",filename="road.png"}
roadCopy.anchorX,roadCopy.anchorY=0,1
roadCopy.contentHeight=dh
sceneGroup:insert(roadCopy)
roadCopy.speed=5
----PLAYER CAR--------
car=display.newRect( dw/2, dh-10, dw/8, dw/4.5 )
car.fill={type="image", filename="player.png"}
car.anchorY=1
center=dw/2
car.posA=center-dw/4.75
car.posB=center
car.posC=center+dw/4.75
car.current=car.posB
sceneGroup:insert(car)
physics.addBody(car,"dynamic",{density=1, bounce=0.1, friction=0.2})
car.gravityScale=0
--[[randomobject1()
randomobject1()]]
end
-- "scene:show()"
function scene:show( event )
if(event.phase=="did") then
print("Scene:show")
addList()
randomobject1()
randomobject1()
end
end
function addList()
--adding event listeners
car.collision = onLocalCollision
car:addEventListener( "collision", car )
road.enterFrame=moveRoad
Runtime:addEventListener( "enterFrame", road)
roadCopy.enterFrame=moveRoad
Runtime:addEventListener( "enterFrame", roadCopy)
road:addEventListener("tap",touchScreen)
roadCopy:addEventListener("tap",touchScreen)
end
-- "scene:hide()"
function scene:hide( event )
print("Scene:hide")
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen)
-- Insert code here to "pause" the scene
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen
end
end
-- "scene:destroy()"
function scene:destroy( event )
print("Scene:destroy")
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- Insert code here to clean up the scene
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
localcomposer=require(“composer”)
本地场景=composer.newScene()
局部pix=0
本地编号=0
本地物理=需要“物理”
物理开始
本地x=2000
本地prevP=0
本地prevS=5
本地计数=1
-- -----------------------------------------------------------------------------------------------------------------
--除非调用“composer.removese()”,否则侦听器函数之外的所有代码将只执行一次
-- -----------------------------------------------------------------------------------------------------------------
--本地转发参考应该在这里
-- -------------------------------------------------------------------------------
本地opp1
本地opp
本地场景组
本地路考
地方公路
本地车
函数removeAllListeners(self)
self.\u functionListeners=nil
self.\u tableListeners=nil
结束
本地功能触摸屏(事件)
打印(“触摸屏”)
如果事件xdh-20,则
self.y=20
pix=pix+10
如果pix>100,则
道路速度=道路速度+1
roadCopy.speed=roadCopy.speed+1
pix=0
其他的
结束
其他的
self.y=self.y+self.speed
pix=pix+1
如果pix>100,则
道路速度=道路速度+1
roadCopy.speed=roadCopy.speed+1
pix=0
其他的
结束
结束
如果道路速度>30或道路复制速度>30,则
道路。速度=30
道路复制。速度=30
结束
结束
本地功能(自身、事件)
--移动即将到来的车辆
如果(self.y==nil),则
返回
结束
如果(self.y>dh),则
运行时:removeEventListener(“enterFrame”,self)
self:removeSelf()
self=nil
随机对象1()
其他的
self.y=self.y+self.speed+roadCopy.speed
结束
结束
函数randomobject1(事件)
--随机生成进入的车辆
math.randomseed(os.time())
opp=display.newRect(中心dw/4.75,30,dw/8,dw/4.5)
局部位置=数学随机(1,3)
本地cartype=math.random(1,3)
如果(位置==prevP),则
位置=(上一个P+2)%3
结束
如果位置==1,则
opp.x=中心dw/4.75
结束
如果位置==2,则
opp.x=中心
结束
如果位置==3,则
opp.x=中心+dw/4.75
结束
如果cartype==1,则
opp.fill={type=“image”,filename=“car\u green\u 1.png”}
结束
如果cartype==2,则
opp.fill={type=“image”,filename=“car\u blue\u 1.png”}
结束
如果cartype==3,则
opp.fill={type=“image”,filename=“car\u red\u 1.png”}
结束
opp.speed=数学随机(1,10)
场景组:插入(opp)
prevP=位置
上速度=上速度
局部r=相对宽度
物理学.addBody(opp,static,{密度=1,反弹=0.1,摩擦力=0.2,半径=5})
相对重力标度=0
opp.enterFrame=moveoppcar
运行时:addEventListener(“enterFrame”,opp)
结束
--“场景:创建()”
功能场景:创建(事件)
打印(“场景:创建”)
sceneGroup=self.view
---------道路---------
道路=显示.newRect(0,0,dw,dh)
road.fill={type=“image”,filename=“road.png”}
道路锚定,道路锚定=0,0
道路高度=dh
场景组:插入(道路)
道路。速度=5
roadCopy=display.newRect(0,0,dw,dh)
roadCopy.fill={type=“image”,filename=“road.png”}
roadCopy.anchorX,roadCopy.anchorY=0,1
roadCopy.contentHeight=dh
场景组:插入(道路复制)
道路复制。速度=5
----游戏者汽车--------
car=显示.newRect(dw/2、dh-10、dw/8、dw/4.5)
car.fill={type=“image”,filename=“player.png”}
car.anchorY=1
中心=dw/2
car.posA=中心dw/4.75
car.posB=center
car.posC=中心+dw/4.75
car.current=car.posB
场景组:插入(汽车)
addBody(汽车,动态,{密度=1,反弹=0.1,摩擦力=0.2})
汽车.重力标度=0
--[[randomobject1()
randomobject1()]]
结束
--“场景:show()”
功能场景:演出(活动)
如果(event.phase==“did”),则
打印(“场景:显示”)
地址列表()
随机对象1()
随机对象1()
结束
结束
函数addList()
--添加事件侦听器
汽车碰撞=仅局部碰撞
车辆:添加车辆列表(“碰撞”,车辆)
道路。enterFrame=moveRoad
运行时:addEventListener(“enterFrame”,road)
roadCopy.enterFrame=moveRoad
运行时:addEventListener(“enterFrame”,roadCopy)
道路:加法