I start moving some game enemies by using a “for loop” and then a timer/transition inside the loop.
Now I see a lot of enemy movement lagging going on in the simulator and wonder if this is normal and how to avoid it?
Here is a sample snipped of the code for the enemy movement:
local moveEnemy for x=1,enemycount do local enemynow=Wave[level][wave][x] local enemyObj=Enemy[level][wave][x] enemyObj.isAttackable=true Wave[level][wave][x].isVisible=true -- we now start moving the enemies by looking at their move and spawn speed in the info array! moveEnemy=function(enemy,index) -- starting moving to waypoint one and then the next -- we have to calculate the moving speed, so it stays the same for all paths -- get distance between the first two waypoints -- move so long until the last waypoint is reached index=index+1 if index\<enemyWaypointCount and enemyObj.isAlive==true then local dt=getDeltaTime() local dist=getDistance(EnemyWayPoints[index][1],EnemyWayPoints[index][2], EnemyWayPoints[index+1][1],EnemyWayPoints[index+1][2]) local speed=dist\*5\*enemyspeed\*dt transitionStash[#transitionStash+1]=transition.to(enemy,{time=speed,x=EnemyWayPoints[index+1][1],y=EnemyWayPoints[index+1][2],onComplete=function() moveEnemy(enemy,index) end}) end end timerStash[#timerStash+1]=performWithDelay(enemyspawntime\*x,function() moveEnemy (enemynow,0) end,1) end
What is the best way to create a lot of moving objects on screen to get a good performance and movement of the objects?
