Jump to content

Game crashes when adding sprite to a group


Puflo
 Share

Recommended Posts

Game crashes with this error after adding a sprite to a group.

Quote

Uncaught Error: [object Object]addChildAt: The index 2 supplied is out of bounds 0
    at c.Group.d.DisplayObjectContainer.addChildAt (phaser.min.js:3)
    at c.Group.add (phaser.min.js:3)
    at game_name.Network.socketOnMessage (Network.js:172)

I need to add a sprite to specific index of a group.

var entity = game.add.sprite(x, y, (teamId == 1 ? "a" : (teamId == 2 ? "b" : "c"))+"_"+skinId);
entity.scale.set(teamId==0?1:0.5);
entity.anchor.set(0.5, 0.5);

game.physics.box2d.enable(entity);
entity.body.angle = angle;
if (teamId != 0){
	entity.body.setCollisionCategory(teamId == 1 ? 2 : 3);
}

if (name){
	entity.nametag = game.add.text(entity.x, entity.y - 35, name, { font: "16px Ubuntu", fontWeight:'bold', fill: "#ffffff", stroke:'#000000', strokeThickness:2.5, wordWrapWidth: entity.width, align: "center"});
	entity.nametag.anchor.set(0.5);
}

entity.data.entityId = entityId;
entity.data.teamId = teamId;

if (entityId == user.entityId){
	game.camera.follow(entity);
	if (teamId == 2){
		entity.body.setCategoryContactCallback(2, game.state.getCurrentState().onPlayerHit);
	}
}

game_name.entities.add(entity, false, entityId);

I even tried using addAt, it doesn't work either.

 

Is this a bug?

Link to comment
Share on other sites

I've found another problem, sprite became invisible when you add it to a group.

Game.js (create):

game_name.entities = this.add.group();

Network.js is the same as the first post (except group.add, i only call it with sprite now.)

 

Edit: It's because group was created in another state, i've fixed it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...