Jump to content

Unable to Access Object Properties in Phaser


LeDenzel
 Share

Recommended Posts

I'm pretty new to coding, and totally new to Phaser, so most of my knowledge comes from the Codecademy lessons.

For the snippet below, I copied the layout from one of the lesson materials and made adjustments as necessary.

//player information
gameState.player = {
   name: 'maleHero',                           //property for the name
   health: 100,                                //starting amount of health
   canAttack: true,                            //saves whether the player is able to attack
   frames: [{                                  //save animation frames
      key: 'idle',                                //custom key for the frame
      start: 0,                                   //custom start frame
      end: 3,                                     //custom end frame
      frameRate: 7,                               //custom frame rate
   },
   {
      key: 'move',
      start: 8,
      end: 15,
      frameRate: 15,
   },
   {
      key: 'attack',
      start: 16,
      end: 24,
      frameRate: 15,
   }],
};

This allows me to create the animations more cleanly, using this code:

//animation handler
gameState.player.frames.forEach(frame => {                           //pulls each frame
   this.anims.create({                                               //creates animations
      key: frame.key,                                                //pulls the given key
      frames: this.anims.generateFrameNumbers(gameState.player.name, { //generates the frames
         start: frame.start,                                         //pulls the given start frame
         end: frame.end,                                             //pulls the given end frame
      }),
      frameRate: frame.frameRate,                                    //pulls the given frame rate
      repeat: -1,                                                    //should repeat infinitely
      yoyo: false                                                    //should not repeat in reverse
   })
});

However, later in my code, when I am trying to Update the gameState with keypresses, it seems I can no longer access these properties. I am particularly interested in the 'canAttack' property of gameState.player. As you can see above, I declared its value as 'true' and I never change it. The following code fails to execute, but I'm not sure why.

//spacebar input should trigger attack animation if the player is able to attack
if (gameState.cursors.space.isDown && gameState.player.canAttack === true) {
   gameState.player.anims.play('attack', true);
}

It's not because of the gameState.cursors.space check, since this will execute if I delete the gameState.player.canAttack check. I tried to look at the Phaser lessons for insight, but they seem to be accessing properties without any trouble. What am I doing wrong? Thanks in advance for any help... I'm sure there's some simple explanation that I'm somehow missing. I just need to get this right or else I can't save data about characters.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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