Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Kxen

Hi,

The following code crashes both Chrome and Firefox, halting at row 106 in impact/game.js.


		if( ig.input.pressed('plant') ) {
		
			ig.game.spawnEntity( EntityFlower, this.pos.x, this.pos.y - 10 );
			ig.game.sortEntities();

		}


The flower entity is nothing really except zIndex: -1 as a property + animation sheet.

In Chrome the first two flowers are planted successfully but always crashes at third. In Firefox it crashes straight away when I press the plant key.

I've tried putting ig.game.sortEntities(); in the init function of the flower entity but it gives me the same error.

Any idea?

1 decade ago by Hareesun

take it out of the pressed and try having it run in the update function within game.js

Works fine for me.

1 decade ago by Kxen

Thanks a lot. That actually fixed the problem although I'm a bit worried about running the sort function all the time when it really should only be necessary once for every new entity you spawn... or have I misunderstood something?

1 decade ago by fugufish

i would run a sort function outside of the update(). Perhaps design the game in a way that minimal sorting is required?

1 decade ago by dominic

What exactly do you mean with "crashing" and "halting"? Do you get an error message, does the browser just "close unexpectedly" or does it end in an infinite loop?

Where is the code in your initial example located exactly? Is it in an entity?

If yes, here&039;s what I think is happening: the entity is spawned from within the #update() method of one of your entities and the sortEntities() method is executed. This reorders the Game&039;s entities array, causing the loop that calls #update() on all entities to call the update() method again on the entity that just spawned the Flower and sorted the entities, causing it to spawn another Flower and sort the entities again -> continue forever.

I never thought about it, but you shouldn&039;t call #.sortEntities() in the midst of a Game&039;s #update() cycle. The problem is very similar to that of the .loadLevel() method. I will introduce a .sortEntitiesDeferred() method in the next version to fix this.

In the meantime, if you don&039;t want to re-sort the entities array for each frame, you could introduce a flag on your game class, #.needsSorting. Then just set that flag after spawning an entity, call sortEntities() when that flag is set and unset it afterwards.

1 decade ago by Kxen

Thanks for the reply, dominic. In Chrome the game just freezes and in Firefox it locks the whole browser (need to ctrl+alt+delete) but also gives me an error that "a script could be occupied/busy, or stopped responding" and refers to 106 and 109 in impact/game.js:106 (it's in Swedish, else I would paste it whole).

The initial example is in the update method of the player entity so what you say seem to be the issue indeed, especially since I noticed it spawns several flowers stacked on top of each other right before it crashes.

Didn't think of using a flag but that solves all my concerns actually although a .sortEntitiesDeferred() would be great. :) Thanks again!
Page 1 of 1
« first « previous next › last »