Puflo Posted September 15, 2017 Share Posted September 15, 2017 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 More sharing options...
samme Posted September 15, 2017 Share Posted September 15, 2017 3 hours ago, Puflo said: The index 2 supplied is out of bounds 0 You can't add outside of the last index. For an empty group, you can only add at the first index. Link to comment Share on other sites More sharing options...
Puflo Posted September 16, 2017 Author Share Posted September 16, 2017 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 More sharing options...
Recommended Posts