Langusmon Posted February 15, 2018 Share Posted February 15, 2018 I am creating a game that is similar to a board game, and am somewhat new to phaser. I created a 'Players' group (players = game.add.group();) , created a sprite for each player and added them into the group. The sprites have an X,Y but no image assign to them, so nothing actually shows up on the game board for the players, but not I can account for them, assign and update properties, etc. For objects in the game that are not image based, is this the correct way of creating / managing these objects or is there a better way to go about this? Thanks Link to comment Share on other sites More sharing options...
samme Posted February 15, 2018 Share Posted February 15, 2018 If you're making things that will never be displayed or positioned at all, you probably don't need to use sprites. game.players = []; game.players.push({ name: 'Sally', color: 'green, score: 0, }); Link to comment Share on other sites More sharing options...
Langusmon Posted February 15, 2018 Author Share Posted February 15, 2018 @samme Thanks! How do I go about updating the score for a given player then? I was doing something like this before: current_player = players.iterate('name','Sally',Phaser.Return.Groups); //or whatever the exact syntax is current_player.score += 5 Link to comment Share on other sites More sharing options...
samme Posted February 15, 2018 Share Posted February 15, 2018 Oh, if you don't use Sprites, you can't use Groups either. Array still has a few useful methods, like forEach and map. Utility libraries like underscore and lodash provide more helpers. Here is underscore (findWhere): var current_player = _.findWhere(players, {name: 'Sally'}); But if you're often looking up players by name, you might just index them that way instead: game.players = { Sally: { color: 'green', score: 0 }, Ana: { color: 'blue', score: 0 }, // […] }; In that case you'd probably want to add a utility library, since Object has few useful iterators built in. Nonetheless, if you're familiar with the Group methods, there's really no harm in using them instead (and don't worry about the invisible sprites). Or is there some sprite you could associate with each player? Game markers? Link to comment Share on other sites More sharing options...
Langusmon Posted February 15, 2018 Author Share Posted February 15, 2018 @samme Thanks! I will maybe just stick with the invisible sprites and groups for now because it has been working well for me. I just didnt know if it created issues long term or not. Link to comment Share on other sites More sharing options...
Recommended Posts