Jump to content

Sprite Objects and Arrays


Shawn
 Share

Recommended Posts

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 world

Battle (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

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

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:

 

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

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

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 testAlly

sometimes - 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 here

hope I could help you :)

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

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