我正在学习一点电晕/ lua,并正在建立一个小游戏。在其中,有多个游戏中的汽车正在生成,玩家必须用他/她自己的游戏来避开它们。如果发生碰撞,游戏结束。我的问题是,有时候游戏中的汽车会彼此叠加,造成碰撞,从而过早地结束游戏/没有玩家的过错。如何修复碰撞代码/生成率以解决此问题?我知道代码很乱,但请耐心等待!
非常感谢任何帮助/代码评论。
--------------------------------------------------------------------------------------
-- Sean Lyons
-- main.lua
-- Game Programming project - vertical scroller
-----------------------------------------------------------------------------------
local physics = require "physics" -- Physics Engine
physics.start()
-----------------------------------------------------------------------------------
--globals
_W = display.contentWidth
_H = display.contentHeight
scrollSpeed = 6
carSpeed = 4
-----------------------------------------------------------------------------------
--variables
local compCollisionFilter = { categoryBits = 2, maskBits = 0 }
local playerCollisionFilter = { categoryBits = 2, maskBits = 0 }
local level2
local userPaddle
local background
local background2
local background3
local crashSound = audio.loadSound( "crash.wav" )
local engineSound = audio.loadStream("engine1n.wav")
local userCarSound = audio.play(engineSound, {channel = 1, loops =-1, fadein=5000} )
-----------------------------------------------------------------------------------
--spritesheet
local options =
{
width = 37,
height = 100,
numFrames = 5
}
local sheet = graphics.newImageSheet("images/carsprites.png", options )
-----------------------------------------------------------------------------------
--background images (Multiple for scrolling)
background = display.newImageRect("images/background.png", 320, 480)
background:setReferencePoint(display.CenterLeftReferencePoint)
background.x = 0
background.y = _H/2
background2 = display.newImageRect("images/background.png", 320, 480)
background2:setReferencePoint(display.CenterLeftReferencePoint)
background2.x = 0
background2.y = background.y+480
background3 = display.newImageRect("images/background.png", 320, 480)
background3:setReferencePoint(display.CenterLeftReferencePoint)
background3.x = 0
background3.y = background2.y+480
-----------------------------------------------------------------------------------
--player car
userPaddle = display.newImage("images/car.png", 100, 380)
local carGroup = display.newGroup()
---------------------------------------------------------------------------------------
--scrolling background
local function move( event )
background.y = background.y + scrollSpeed
background2.y = background2.y + scrollSpeed
background3.y = background3.y + scrollSpeed
if (background.y + background.contentWidth) > 1045 then
background:translate( 0, -960)
end
if (background2.y + background2.contentWidth) > 1045 then
background2:translate(0, -960)
end
if (background3.y + background3.contentWidth) > 1045 then
background3:translate(0, -960)
end
end
Runtime:addEventListener("enterFrame", move)
-----------------------------------------------------------------------------------
--sprite generator
function loadSprite()
frame1 = display.newImage( sheet, math.random(5))
frame1.x = 10 + math.random(100)
frame1.y = -110
frame1.y = frame1.y + carSpeed
frame2 = display.newImage( sheet, math.random(4))
frame2.x = frame1.x + 60
frame2.y = -170
frame2.y = frame2.y + carSpeed
frame3 = display.newImage( sheet, math.random(4))
frame3.x = frame2.x + 90
frame3.y = -230
frame3.y = frame3.y + carSpeed
carGroup:insert(frame1)
carGroup:insert(frame2)
carGroup:insert(frame3)
end
-----------------------------------------------------------------------------------
--car scrolling
local function carMove( event )
frame2.y = frame2.y + carSpeed
frame3.y = frame3.y + carSpeed
frame1.y = frame1.y + carSpeed
if (frame2.y + frame2.contentWidth) > 605 then
frame2:removeSelf()
frame2 = display.newImage(sheet, math.random(4))
frame2.x = 40 + math.random(100)
physics.addBody(frame2, "dynamic", {density = 5, friction = .05,
bounce = .05})
frame2:translate( 0, -1100)
end
if (frame3.y + frame3.contentWidth) > 605 then
frame3:removeSelf()
frame3 = display.newImage(sheet, math.random(4))
frame3.x = frame2.x + math.random(100)
physics.addBody(frame3, "dynamic", {density = 5, friction = .05,
bounce = .05})
frame3:translate(0, -1400)
end
if (frame1.y + frame1.contentWidth) > 600 then
frame1:removeSelf()
frame1 = display.newImage(sheet, math.random(4))
frame1.x = frame3.x + math.random(60)
physics.addBody(frame1, "dynamic", {density = 5, friction = .05,
bounce = .05})
frame1:translate(0, -1200)
end
end
Runtime:addEventListener("enterFrame", carMove)
---------------------------------------------------------------------------------------
-- Set up physics
function startPhysics()
physics.addBody(userPaddle, "dynamic", {density = 5, friction = .05,
bounce = .1, filter = {userCollisionFilter}})
physics.addBody(frame2, "dynamic", {density = 5, friction = .05,
bounce = .05, filter = {compCollisionFilter}})
physics.addBody(frame3, "dynamic", {density = 5, friction = .05,
bounce = .05, filter = {compCollisionFilter}})
physics.addBody(frame1, "dynamic", {density = 5, friction = .05,
bounce = .05, filter = {compCollisionFilter}})
physics.setGravity(0, 0)
end
---------------------------------------------------------------------------------------
--drag event
function userPaddle:touch( event )
if event.phase == "began" then
self.markX = self.x -- store x location of object
self.markY = self.y -- store y location of object
elseif event.phase =="moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- move object based on calculations above
end
return true
end
userPaddle:addEventListener( "touch", userPaddle )
---------------------------------------------------------------------------------------
local timeLimit = 0
timerUp = display.newText(timeLimit, 300, 500, native.systemFontBold, 14)
timerUp:setTextColor(0,0,0)
local function timeUP(event)
if (scrollSpeed ~= 0) then
timeLimit = timeLimit+1
timerUp.text = timeLimit
if (timeLimit == 3) then
level2()
end
elseif (scrollSpeed == 0) then
local gameOver = display.newText("Game Over!", 105, 40, native.systemFontBold, 24)
gameOver:setTextColor(255, 10, 0)
end
end
timer.performWithDelay(1000,timeUP,timeLimit)
---------------------------------------------------------------------------------------
--add increased spawn rate here
function level2()
frame4 = display.newImage( sheet, math.random(4))
frame4.x = frame3.x + 60
frame4.y = -310
carGroup:insert(frame4)
physics.addBody(frame4, "dynamic", {density = 5, friction = .05,
bounce = .05, filter = {compCollisionFilter}})
carSpeed= carSpeed + 1
scrollSpeed = scrollSpeed + 1
frame4.y = frame4.y + carSpeed
carGroup:insert(frame4)
end
function levelTwo(event)
if timeLimit >= 3 then
frame4.y = frame4.y + carSpeed
if (frame4.y + frame4.contentWidth) > 600 then
frame4:removeSelf()
frame4 = display.newImage(sheet, math.random(4))
frame4.x = frame3.x + math.random(60)
physics.addBody(frame4, "dynamic", {density = 5, friction = .05,
bounce = .05, filter = {compCollisionFilter}})
frame4:translate(0, -2000)
end
end
end
Runtime:addEventListener("enterFrame", levelTwo)
---------------------------------------------------------------------------------------
--collision event
local function onCollision( event )
if ( event.phase == "began") then
print( "began: collision")
scrollSpeed = 0
carSpeed = 0
userCarSound = audio.stop()
local crashSound = audio.play( crashSound, { channel=2, loops=0, fadein=0 } )
elseif ( event.phase =="ended" ) then
--outPutField:removeSelf()
if (outPutField ~= nil) then
outPutField:removeSelf()
end
print( "ended: collision")
end
end
Runtime:addEventListener( "collision", onCollision)
---------------------------------------------------------------------------------------
loadSprite()
startPhysics()
答案 0 :(得分:0)
在碰撞事件处理程序中,检查碰撞是否与用户的汽车有关。如果没有,删除新车并继续(忽略碰撞)。