setInterval going too fast

后端 未结 2 1227
刺人心
刺人心 2021-01-22 19:26

I\'m new to JS, and decided to start of learning by a making a small game. I am using a setInterval to automate the enemy\'s attack. For their first attack the interval is corre

相关标签:
2条回答
  • 2021-01-22 19:32

    You keep calling 'setInterval' again again. Each call is running in parallel.

    If you have more than one warrior peer type (archer, knight, etc), create an array that will have a separate set interval for each.

    If, as seems the case, you only have one and they play at random each turn, add clearInterval before every setInterval

    0 讨论(0)
  • 2021-01-22 19:46

    you should never use setInterval for animations!

    Just use requestAnimationFrame instead!

    • https://developer.mozilla.org/en/docs/Web/API/window.requestAnimationFrame
    • http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
    • http://css-tricks.com/using-requestanimationframe/
    0 讨论(0)
提交回复
热议问题