Shawn Posted December 17, 2014 Share Posted December 17, 2014 Hi guys! I am having an issue with Sprite objects and arrays, and I bet there is a Phaser-fied way to do what I need, probably using groups, but I don't know what it is (maybe it's just "use groups, duh"): I am making a sample Final Fantasy-like RPG engine, with the following files/objects: Game - Defines player characters, etc.Play (state) - Defines the over worldBattle (state) - Defines the Final Fantasy-esque battle screen, this is where I create enemies Here is my issue, I create the players in Game - game.player. I create the enemies in Battle - battle.enemy. To make working with them easier, I add them to javascript arrays inside Battle. I have a players array, and an enemies array. But when I attempt to set players.health, it does not update the game.player health, as I would have expected. I suspect I should be adding them to groups or something, but I am unsure. Any help would be awesome, thanks! Shawn Link to comment Share on other sites More sharing options...
lewster32 Posted December 17, 2014 Share Posted December 17, 2014 Changing the health should work regardless of whether you use a group or an array, since you're accessing the health property of the Sprite. It'd be easier to work out what's going on if you could provide code for the relevant parts or an example of the problem in jsFiddle, CodePen or the like. Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 If this doesn't work, let me know, and I can try and put it up somewhere else. Here is the link to the sample game: http://shawnmann.com/games/samplerpg/ Links to the files:http://shawnmann.com/games/samplerpg/js/boot.jshttp://shawnmann.com/games/samplerpg/js/load.jshttp://shawnmann.com/games/samplerpg/js/menu.jshttp://shawnmann.com/games/samplerpg/js/play.jshttp://shawnmann.com/games/samplerpg/js/battle.jshttp://shawnmann.com/games/samplerpg/js/game.js To get to the relevant part in the game, go touch the door to get to the battle scene, then click "Attack" twice with the console open (that will complete a "round" of combat). You can see the health printouts in the console. If that's too much, I can try and replicate it in a simpler project, which might also lead me to a solution. Additionally, I know there is a bunch of superfluous code in the battle.js - most of it are attempts to solve this. Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 Trying to recreate in a standalone file with less "other stuff" going on. Link to comment Share on other sites More sharing options...
stauzs Posted December 17, 2014 Share Posted December 17, 2014 hi, I'm not sure what is wrong.. seems fine to me:battleState.players[0].stats.health100game.player.stats.health100battleState.players[0].stats.health -= 2080game.player.stats.health80Probably I didn't understand your problem completely Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 I am quite sure I am doing something silly. I was just beating my head against the wall for so long, I didn't want to find out there was some bizarre interaction when using an array (or something). Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 Hrm, yeah - when I do it from the console, you're right Stauzs - it looks like it's fine. But when I have the game print to the console, it appears wrong. Still looking. Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 I may have narrowed it down - I am not actually using battleState.players.stats.health, I am filtering an array to get the object (sprite) I want first:var target = battleState.actors.filter(function (actor) { return actor['gameID'] == targetGameID;});target[0].stats.health -= 15; Link to comment Share on other sites More sharing options...
Shawn Posted December 17, 2014 Author Share Posted December 17, 2014 Ok - I have made a simple one page version that illustrates the problem: http://shawnmann.com/games/samplerpg/test2.html Just have your console open, and it will show the issue. Thanks for your help, guys! Link to comment Share on other sites More sharing options...
stauzs Posted December 17, 2014 Share Posted December 17, 2014 you are actually hitting testAlly not player.. as game_ids are increasing .. so first actor has - gameID = 1..and first actor you have created is testAllysometimes - I find easier to debug with getters / setters - like in your case it would be somthing like that: var ActorStats = function(actor, health, mana, speed){ this.actor = actor; this.health = health; this.mana = mana; this.speed = speed;};ActorStats.prototype = { _health: 0, mana: 0, speed: 0, get health(){ return this._health; }, set health(val){ console.log("Actor with gameID",this.actor.gameID, "got hit by ",(this._health - val)); this._health = val; }};Actor = function (isEnemy, health, mana, speed) { this.stats = new ActorStats(this, health, mana, speed);/// rest code herehope I could help you Link to comment Share on other sites More sharing options...
Shawn Posted December 18, 2014 Author Share Posted December 18, 2014 Oh man, I knew it was going to be embarrassingly silly. I had been looking at this for so long, I knew it, haha. Thanks so much though - you're right. I just got worried I was doing something that would never work throwing the sprites into arrays. Thanks so much! Link to comment Share on other sites More sharing options...
Recommended Posts