meth-meth-method / super-mario

Create a Super Mario game in vanilla JS from scratch.
http://bit.ly/2i7u5ks
652 stars 155 forks source link

Undefined behavior in loop: #9

Open AFE-GmdG opened 6 years ago

AFE-GmdG commented 6 years ago

https://github.com/meth-meth-method/super-mario/blob/dc5ab340f1aff68f80ee120ee3653fad7b6274d5/public/js/traits/Killable.js#L24

You delete an entity in the collection of all entities in the level while you are looping over all entities in the level. This causes a undefined behavior within the loop.

You should mark the entity for removing and remove it after the loop.

pomle commented 6 years ago

Thanks, you are correct. I will see if I can fit this correction into the tutorial in a way that the failure can be showcased.

pomle commented 6 years ago

This was fixed by a feature added in https://github.com/meth-meth-method/super-mario/pull/13 and finally cleaned up here: https://github.com/meth-meth-method/super-mario/commit/85e56eaca5ca703f446d9d07d7c4dc1ff8db1006#diff-f072f71399370b241866616f9ec0e019L24

Will be in episode 13.

Thank you @AFE-GmdG!

pomle commented 6 years ago

Realized that this just moved the problem to the finalize stage.