Performance problems

Hi, guys. I need a help. That’s a summary of my code and I have performance problems when gameloop spawns some enemies. I think could be timer.performWithDelay that I use for each enemy movement. Am I right? Is there another way to create enemy and move it? Thanks.

local lastEnemy = 0

function createEnemy_1()

         local enemy

         function moveEnemy(event)

                   enemy.y = enemy.y + gameMoveSpeed

                    if enemy.y > display.contentHeight + enemy.height*.5 then

                              timer.cancel(event.source)

                              enemy:removeSelf()

                    end

         end 

          timer.performWithDelay(1, moveEnemy, 0)

end

 

function createEnemy_2()

         local enemy

         function moveEnemy(event)

                   enemy.y = enemy.y + gameMoveSpeed

                    if enemy.y > display.contentHeight + enemy.height*.5 then

                             timer.cancel(event.source)

                             enemy:removeSelf()

                    end

         end 

          timer.performWithDelay(1, moveEnemy, 0)

end

.

.

.

function createEnemy_n()

         local enemy

         function moveEnemy(event)

                  enemy.y = enemy.y + gameMoveSpeed

                  if enemy.y > display.contentHeight + enemy.height*.5 then

                             timer.cancel(event.source)

                             enemy:removeSelf()

                  end

         end 

          timer.performWithDelay(1, moveEnemy, 0)

end

 

function spawnEnemies(params)

         local e = params.e

         if e==1 then createEnemy_1()

          if e==2 then createEnemy_2()

          .

          .

          .

          if e==n then createEnemy_n()

end

 

function gameLoop(event) –EnterFrame

         if event.time - lastEnemy >= math.random(500, 3000) then

                   spawnEnemies({e=math.random(1,n)})

         lastEnemy = event.time

         end

.

.

.

end
[import]uid: 40281 topic_id: 13009 reply_id: 313009[/import]

Yeah, that isn’t a great set up - a timer set to “1” is very fast, as “1000” is 1 second.

Could you not use transitions or the like rather than such a mad timer? Or maybe look at my D pad tutorial on Techority - it’s aimed at moving a main character but how it is set up would help you learn to move your enemies in a more efficient way.

In future please post your code in < lua > tags - the above was a real headache to read. (If you haven’t already, you need to read the forum rules.

Peach :slight_smile: [import]uid: 52491 topic_id: 13009 reply_id: 47844[/import]

First of all, my apologies about LUA tags and thank you for your reply. About the problem, I will see your tutorial. On first way, I used transition.to to move my enemies but it is controlled by time and I would like to control the enemies’ moviment like that: enemy.y = enemy.y + gameMoveSpeed (gameMoveSpeed is global variable that increase the speed of all objects). I saw the Tilt Monster by Beebegames but the same kind of enemy only can display once per cycle. I would like to spawn the same enemy more than once like three equals enemies in sequence. My game is run’n’jump style (portrait position). The hero is in bottom of screen and he jumps from left to right and right to left to escape from enemies which appear from the top. If you have any idea how to spawn enemies please tell me. Thanks again. [import]uid: 40281 topic_id: 13009 reply_id: 47859[/import]