Hi guys, I’m pretty new to Corona and I’m starting to make some tests with it. My first exercise to get going with corona is/was to create a simple car games. Cars start showing up from the top of the screen and are moved to the bottom of the screen and the player has to change lines to avoid hitting the cars. It’s pretty simple. (this is pretty much what I’m trying to create https://itunes.apple.com/au/app/zigzag-racer-insanely-addictive!/id842010110?mt=8))
The problem is that I can’t understand why the cars movement is not fluent. It’s kind of laggy and I really don’t know why because I think that I’m doing it right. I’m adding here what I’m doing, because maybe my approach is not right.
Basically I have a “enterFrame” listener. For each frame I check if the latest car added is below a “y axis” coordinate, and if so I add a new car. The cars are stored in a table, so the first is always the latest added. Also inside the “enterFrame” listener I check if the car has left the screen and I remove it.
To move the cars I have a function that iterates over all the cars in the “oponentCars” and with a constant speed I move them.
Is this approach ok? Why am I seeing some lag?
local oponentCars = { display.newImage( "oponentcar.png", randomSide(), -carHeight) } local runtime = 0 local function getDeltaTime() local temp = system.getTimer() local dt = (temp-runtime) / (1000/60) runtime = temp return dt end function addNewCar( ) local oponentcar = display.newImage( "oponentcar.png", randomSide(), -carHeight) physics.addBody( oponentcar, { density = 0.0} ) table.insert( oponentCars, 1, oponentcar ) end function moveCars( dt ) for index,oponentCar in ipairs(oponentCars) do oponentCar:translate( 0, speed\*dt ) end end local function frameUpdate() moveCars(getDeltaTime()) if oponentCars[1].y \> 280 then addNewCar() end if oponentCars[#oponentCars].y \> 1700 then oponentCars[#oponentCars]:removeSelf() table.remove( oponentCars, #oponentCars ) end end Runtime:addEventListener( "enterFrame", frameUpdate )
Thanks a lot!!