Yea title says it all ( almost ),
here is my code:
local function collisionFunction( event ) if "began" == event.phase and dead == false then --print("eobj1"..event.object1.name.."// eobj2:"..event.object2.name.."") if event.object1 and event.object2 then if event.object1.name == "zombie" and event.object2.name == "player" then -- remove part of player's hp -- remove part of zombie's hp when player is touching zombie local function substractPlayerHp( event ) --if zombie hit player then remove portion of his/hers hp player.hp = player.hp - 1 playerHpText.text = "HP: "..player.hp.."" -- if player's hp is or goes below 0 then make the player diee if player.hp \<= 0 then playerDeath() player.hp = 0 playerHpText.text = "HP: "..player.hp.."" end end substractPlayerHp() timer2 = timer.performWithDelay( 1000, substractPlayerHp, 0 ) elseif event.object1.name == "zombie" and event.object2.name == "bullet" then event.object1.hp = event.object1.hp - bulletDamage display.remove( event.object2 ) -- if zombie's hp is equal or less than 0 then if event.object1.hp \<= 0 then totalZombies = totalZombies - 1 -- remove bullet and zombie display.remove( event.object1 ) end elseif event.object1.name == "ammobox" and event.object2.name == "player" then -- reset player's magazine & bullet situation bullets = maxBullets leftBullets = maxLeftBullets bulletText.text = ""..leftBullets.."┃"..bullets.."" display.remove( event.object1 ) end end elseif "ended" == event.phase then -- cancel timer2 only if it exists otherwise it will be error if timer2 then timer.cancel( timer2 ) end end end Runtime:addEventListener( "collision", collisionFunction )
so in my map I have 1 player, 30 zombies ( 1 spawn every 2 seconds ( 30 zombies is max ) ), and 1 ammo box spawn in a random place in map every 30 seconds.
When I have this function and I spread bullets to the zombies the game freezes. Here is video explanation:
https://www.youtube.com/watch?v=QuAAu7V_530&feature=youtu.be
So what I think happens here is that there is kind of some low memory thing, cause the collision function is enterFrame which means it udpate 60 times every second AND there is alot of if & elseif logic etc…
So if you have any ideas how to fix this please let me know.
Also I tried the game without the collision function and it worked out good without the collision function.
Coder101