Basically, I have a line of cars that all move along the y axis, with the same x coordinates. They all have different speeds. Say we have two cars, car A and car B. Car B is in front (so, below car A, as the cars move from top to bottom) and car A is behind car B (so, it has a smaller y coordinate). Car A is going faster than car B and it is supposed to slow down to car B’s speed when it reaches it (basically, when it collides with it) and carry on going at that speed.
Both cars are supposed to ‘reset’ when they reach a y value of some pixels greater than the screen’s bottom.
Problem is, I want the cars (or the collision detection code, if you prefer) to be able to tell the difference between a collision at the back of the car, and at any other part (because in the game, if a car collides with another head-on or at the sides, you are supposed to lose the game).
So far my options are a sensor object at the back of every car, which I’d rather not implement as it is a lot of code to add (I will be spawning more than 20 cars) and preCollision detection which I don’t know why it doesn’t work (sample code below).
car.lua
[lua]function new(params)
local newCar
newCar = display.newImage(“car.png”)
newCar.x = params.x
newCar.y = params.y
local OriginalX = newCar.x
local OriginalY = - 20
local moveCar
local moveOrderTime = 0
local moveOrderX = 0
local moveOrderY = 0
function newCar.resetCar()
newCar.x = OriginalX
newCar.y = OriginalY
setup.score = setup.score + 1
newCar.moveCar({time = moveOrderTime, x = moveOrderX, y = moveOrderY})
end
function newCar.moveCar(params)
moveOrderTime = params.time
moveOrderX = params.x
moveOrderY = params.y
transition.to(newCar,{time = moveOrderTime, x = moveOrderX, y = moveOrderY, onComplete = newCar.resetCar})
end
function newCar.onLocalPreCollision(event)
if event.phase==“began” then transition.pause(newCar) end
if event.phase==“ended” then transition.resume(newCar) end
end
return newCar
end[/lua]
main.lua:
[lua]local car1 = car.new( {x = 160, y = -80} )
cars[#cars + 1] = car1
physics.addBody(car1)
car1.isFixedRotation = true
car1.isSensor = true
car1.moveCar({time = 7000, x = car1.x, y = screenBottom})
car1:addEventListener(“preCollision”, car1.onLocalPreCollision)[/lua]
Any ideas on how to do this? I am swamped, stuck with this problem for days
[import]uid: 106739 topic_id: 19356 reply_id: 319356[/import]